Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to automatically close a Dialog when clicked outside?

How to automatically close a Dialog when clicked outside?

Scheduled Pinned Locked Moved QML and Qt Quick
automatically cclose dialogclicked outside
9 Posts 6 Posters 8.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tezine
    wrote on 10 Mar 2015, 15:47 last edited by
    #1

    Hi,
    Does anybody know how to close a qml Dialog when user clicks outside of it?
    This is a common behavior for Android/iOS apps, but I couldn't figure out how to do it with QML Dialog component.
    Thank you,
    Bruno

    ? 1 Reply Last reply 10 Mar 2015, 21:25
    0
    • J Offline
      J Offline
      jalomic
      wrote on 10 Mar 2015, 18:47 last edited by jalomic 3 Oct 2015, 18:49
      #2

      Good question. I also want to know the answer to this question
      May be using focusChanged can help?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        benlau
        Qt Champions 2016
        wrote on 10 Mar 2015, 18:49 last edited by benlau 3 Oct 2015, 18:49
        #3

        One of the solution is InverseMouseArea from Ubuntu SDK. It capture the mouse event outside of your component. However, Qt did not bundle this component. You may find the C++ implementation from Ubuntu or got a QML version from this page:

        https://github.com/benlau/quickandroid/blob/master/QuickAndroid/InverseMouseArea.qml

        (Remember to destroy the component after closed the dialog.)

        1 Reply Last reply
        0
        • T tezine
          10 Mar 2015, 15:47

          Hi,
          Does anybody know how to close a qml Dialog when user clicks outside of it?
          This is a common behavior for Android/iOS apps, but I couldn't figure out how to do it with QML Dialog component.
          Thank you,
          Bruno

          ? Offline
          ? Offline
          A Former User
          wrote on 10 Mar 2015, 21:25 last edited by
          #4

          @tezine You could use a QtQuick Window with the Window.Popup flag set. Unfortunately this feature is buggy in the current Qt version. I faced the same problem and was able to work around it. See this thread: http://forum.qt.io/topic/51393/window-with-qt-popup-not-closed/2
          Cheers!

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Buttink
            wrote on 11 Mar 2015, 02:42 last edited by Buttink 3 Nov 2015, 02:43
            #5

            @benlau I thought that a dialog/window didn't get mouse events outside the dialog from the protected event() function making the inverse mouse area only work inside of the screen? The reason I say this is because I thought those events were passed to Qt from the windowing system. Which I don't think would tell you the mouse position if you didn't set some special flag or were the window that actually contained the mouse.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              benlau
              Qt Champions 2016
              wrote on 11 Mar 2015, 04:55 last edited by benlau 3 Nov 2015, 04:56
              #6

              @Buttink I assume tezine means iOS and Android platform. I think the implementation of Dialog class in those platform do not create a new window. It is still a QQuickItem. So the hijacking by MouseArea works.

              But there has two problems. The first , Dialog is not a QQuickItem. And the QML version of InverseMouseArea do not working well in this situation.

              It could be fixed by this experimental InverseMouseArea:
              https://gist.github.com/benlau/a0d57b865fe9eaf03c10

              And hijack the dialog by

                FileDialog {
                  id: fileDialog
               }
              
              InverseMouseArea {
                  id: inverseMouseArea
                  width: parent.width
                  height: parent.height
                  enabled: fileDialog.visible
                  parent : fileDialog.contentItem
              
                  onPressed: {
                      fileDialog.close();
                  }
              
              1 Reply Last reply
              0
              • T Offline
                T Offline
                tezine
                wrote on 11 Mar 2015, 15:18 last edited by
                #7

                Hi all,
                @benlau it worked with your experimental InverseMouseArea.
                I had to comment the line parent: fileDialog.contentItem and the dialog must have: modality: Qt.NonModal
                Besides that, it worked perfectly!
                Thank you,
                Bruno.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tezine
                  wrote on 11 Mar 2015, 16:35 last edited by
                  #8

                  Hi @benlau , I forgot to mention, I commented the line inside InverseMouseArea: //if (!_inBound(pt)). Just sends component.pressed();
                  Here is an example of use:
                  Dialog {
                  id: sharedDlg
                  modality: Qt.NonModal
                  property alias inverseParent: inverseMouseArea.parent
                  InverseMouseArea {
                  id: inverseMouseArea
                  width: 400
                  height: 600
                  enabled: sharedDlg.visible
                  onPressed: sharedDlg.close()
                  }
                  }

                  //extending Dialog with InverseMouseArea....
                  MyDialog {
                  id: dlg
                  contentItem: Rectangle {
                  implicitWidth:400
                  implicitHeight:600
                  //add content here
                  }
                  }

                  //using MyDialog....
                  MyDialog{
                  inverseParent: topWindow
                  }

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    michaelL
                    wrote on 25 Jul 2017, 11:29 last edited by
                    #9

                    My solution for automatically close a Dialog in qml by clicking outside the window was to change the modality of the window to non modal:

                    Dialog
                        {
                            id: myDialog
                            height: 300
                            width: 600
                            modality: Qt.NonModal
                    ...
                    

                    My window has a 1280x800 size and the dialog appears in the middle. Once its open i click outside and the Dialog closes by itself.

                    1 Reply Last reply
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved