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. QML focus specific window.
Forum Updated to NodeBB v4.3 + New Features

QML focus specific window.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
14 Posts 2 Posters 6.7k 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.
  • S Offline
    S Offline
    Slei
    wrote on last edited by Slei
    #3

    @Diracsbracket

    As childs directly in qml

    MainUI
    ---------> Settings UI
    -----------------------> Addintional UI

    DiracsbracketD 1 Reply Last reply
    0
    • S Slei

      @Diracsbracket

      As childs directly in qml

      MainUI
      ---------> Settings UI
      -----------------------> Addintional UI

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #4

      @Slei
      My question implied which method you were using to create the additional windows.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Slei
        wrote on last edited by
        #5

        @Diracsbracket

        They are instantiated in qml as childs and therefore created when the mainui is created.
        and i display them by using show()

        DiracsbracketD 1 Reply Last reply
        0
        • S Slei

          @Diracsbracket

          They are instantiated in qml as childs and therefore created when the mainui is created.
          and i display them by using show()

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by Diracsbracket
          #6

          @Slei
          You mean something like:

          ApplicationWindow {
              id: mainWindow
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              Row {
                  Button {
                      text: "show window 1"
                      onClicked: {
                          if (!window1.visible)
                              window1.show()
          
                          window1.requestActivate()
                      }
                  }
                  Button {
                      text: "show window 2"
                      onClicked: {
                          if (!window2.visible)
                              window2.show()
          
                          window2.requestActivate()
                      }
                  }
              }
          
              Window {
                  id: window1
                  height: 300
                  width: 600
          
                  Rectangle {
                      anchors.fill: parent
                      color: "red"
                  }
                  TextField {
                      id: textField1
                      focus: true
                      placeholderText: "text 1"
                  }
              }
          
              Window {
                  id: window2
                  height: 300
                  width: 600
          
                  Rectangle {
                      anchors.fill: parent
                      color: "blue"
                  }
          
                  TextField {
                      focus: true
                      id: textField2
                      placeholderText: "text 2"
                  }
              }
          }
          

          If so, it seems to work correctly? If you want to get the parent window on top, that is not possible here.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Slei
            wrote on last edited by Slei
            #7

            @Diracsbracket
            yeah something like this, but the Windows {} stuff is in a seperate qml and i just instatiate it in the main ui qml
            I actually don't want the parent on top, it good taht the child is on top, but i currently need to find a way to focus the top window without using show().
            The problem is when i set the ui to visible=false and back to true nothing is focused, and when i use requestActivate only the main ui ( parent) gets focused even though i use modality, so the user can't use the parent while the child is open.

            DiracsbracketD 1 Reply Last reply
            0
            • S Slei

              @Diracsbracket
              yeah something like this, but the Windows {} stuff is in a seperate qml and i just instatiate it in the main ui qml
              I actually don't want the parent on top, it good taht the child is on top, but i currently need to find a way to focus the top window without using show().
              The problem is when i set the ui to visible=false and back to true nothing is focused, and when i use requestActivate only the main ui ( parent) gets focused even though i use modality, so the user can't use the parent while the child is open.

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by
              #8

              @Slei said in QML focus specific window.:

              i currently need to find a way to focus the top window without using show().

              The problem is when i set the ui to visible=false and back to true nothing is focused

              Wouldn't a focus or forceActiveFocus in the onVisibleChanged() handler of each of your windows help? When the Window is created and not hidden and no other window created after that, does the intended element in the window correctly get the focus?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Slei
                wrote on last edited by
                #9

                @Diracsbracket said in QML focus specific window.:

                forceActiveFocus

                when the windows gets visible=true after being visible=false it gets focused correctly
                the main issue currently is, when the window is visible but not focused, for example the user uses the windows explorer window and therefore my ui isn't focused anymore.

                But i provide a systemtray icon "Show UI" which brings the window back to the top with the qml function "raise()", but it doesn't focus my ui.

                I've tried the following command:

                		raise();
                		forceActiveFocus();
                		requestActivate();
                

                and none of those work for focusing my ui when bringing it back to the top when being visible.

                DiracsbracketD 1 Reply Last reply
                0
                • S Slei

                  @Diracsbracket said in QML focus specific window.:

                  forceActiveFocus

                  when the windows gets visible=true after being visible=false it gets focused correctly
                  the main issue currently is, when the window is visible but not focused, for example the user uses the windows explorer window and therefore my ui isn't focused anymore.

                  But i provide a systemtray icon "Show UI" which brings the window back to the top with the qml function "raise()", but it doesn't focus my ui.

                  I've tried the following command:

                  		raise();
                  		forceActiveFocus();
                  		requestActivate();
                  

                  and none of those work for focusing my ui when bringing it back to the top when being visible.

                  DiracsbracketD Offline
                  DiracsbracketD Offline
                  Diracsbracket
                  wrote on last edited by Diracsbracket
                  #10

                  @Slei
                  On my system (Win10 Pro, Qt 11.0, VS2017 Community) this works without any problem: the system tray icon does its job correctly?

                  Could you try with this minimal example below?

                  Press the buttons 1 and 2 to show the windows. Then, in window1, you can start editing textField2, then switch to something else (thus textField2 is still in edit mode), e.g. Windows Explorer, or whatever.

                  Then press the icon in the tray. It should bring Windows1 up and (forcibly) remove focus from textField2 (even though it is still in editing mode, by issuing its accepted() signal, just for illustration) and put focus on textField1.

                  import QtQuick 2.11
                  import QtQuick.Window 2.11
                  import QtQuick.Controls 2.4
                  import Qt.labs.platform 1.0
                  
                  Window {
                      id: appWindow
                      visible: true
                      width: 640
                      height: 480
                      title: qsTr("Hello World")
                  
                      SystemTrayIcon {
                          visible: true
                          iconSource: "qrc:/images/tray-icon.png"
                  
                          onActivated: {
                              window1.show() //not needed if already showing
                              window1.raise()
                              window1.requestActivate()
                              if (window1.textField12.focus === true)
                                  window1.textField12.accepted()
                              window1.textField1.forceActiveFocus()
                          }
                      }
                  
                      Row {
                          Button {
                              text: "show window 1"
                              onClicked: {
                                  if (!window1.visible)
                                      window1.show()
                  
                                  window1.requestActivate()
                              }
                          }
                          Button {
                              text: "show window 2"
                              onClicked: {
                                  if (!window2.visible)
                                      window2.show()
                  
                                  window2.requestActivate()
                              }
                          }
                      }
                  
                      Window {
                          id: window1
                          height: 300
                          width: 600
                  
                          property alias textField12: textField12
                          property alias textField1: textField1
                  
                          Rectangle {
                              anchors.fill: parent
                              color: "red"
                          }
                          Column {
                              TextField {
                                  id: textField1
                                  focus: true
                                  placeholderText: "text 1"
                              }
                  
                              TextField {
                                  id: textField12
                                  placeholderText: "text 2"
                              }
                          }
                      }
                  
                      Window {
                          id: window2
                          height: 300
                          width: 600
                  
                          Rectangle {
                              anchors.fill: parent
                              color: "blue"
                          }
                  
                          TextField {
                              focus: true
                              id: textField2
                              placeholderText: "text 2"
                          }
                      }
                  }
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Slei
                    wrote on last edited by Slei
                    #11

                    @Diracsbracket

                    your example works, the difference is that you have to click in to the window anyway to press the button, which focuses the window.
                    in my case the window is in the back somewhere and i try to activate it through the systemtray icon, therefore im not directly activating any window of the app through clicks

                    Clicking the appicon in the task bar, works correctly for example, it has automatically the focus on the top window. So there must be somehow an issue with the activating/showing the window through my systemtray icon.

                    Currently I do the following:

                    Click on Systemtray Settings UI -> emit qmlShow() of Settings UI

                    So i don't activate anything direclty with the main ui or application, I only call qmlShow for the settings ui:

                    SettingsUI.qml
                    onQmlShow:

                    	       visible = true;
                    		raise();
                    		requestActivate();
                    		show();
                    		active = true;
                    		focus = true;
                    

                    Thats it.

                    This doesn't work because after using the function the window is in the front but the foucs is still somewhere on the desktop or windows taskbar.

                    Maybe I just miss a step for this to work correctly on windows, like activating the whole main ui first and then calling request activate, I'll test those and see if it somehow works

                    DiracsbracketD 1 Reply Last reply
                    0
                    • S Slei

                      @Diracsbracket

                      your example works, the difference is that you have to click in to the window anyway to press the button, which focuses the window.
                      in my case the window is in the back somewhere and i try to activate it through the systemtray icon, therefore im not directly activating any window of the app through clicks

                      Clicking the appicon in the task bar, works correctly for example, it has automatically the focus on the top window. So there must be somehow an issue with the activating/showing the window through my systemtray icon.

                      Currently I do the following:

                      Click on Systemtray Settings UI -> emit qmlShow() of Settings UI

                      So i don't activate anything direclty with the main ui or application, I only call qmlShow for the settings ui:

                      SettingsUI.qml
                      onQmlShow:

                      	       visible = true;
                      		raise();
                      		requestActivate();
                      		show();
                      		active = true;
                      		focus = true;
                      

                      Thats it.

                      This doesn't work because after using the function the window is in the front but the foucs is still somewhere on the desktop or windows taskbar.

                      Maybe I just miss a step for this to work correctly on windows, like activating the whole main ui first and then calling request activate, I'll test those and see if it somehow works

                      DiracsbracketD Offline
                      DiracsbracketD Offline
                      Diracsbracket
                      wrote on last edited by
                      #12

                      @Slei
                      My second example works from the system tray as well. The buttons are only there to initially show the windows. Then, even if window 2 has the foreground, clicking on the system tray will put window1 in the foreground, and give focus to the textfield immediately so you can start typing immediately without clicking the text field.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Slei
                        wrote on last edited by Slei
                        #13

                        @Diracsbracket oh i havent seen the second example lol, anyway i think i have found the issue.

                        I've tried to call qmlShow first on the main window which simply sets the visiblity to true and also calls raise()

                        and after this i call the qmlShow() for the child window which is in the front which executes the code posted previously.

                        So it seems like I need to first activate/raise the main ui and afterwards the sub ui since this works perfectly.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Slei
                          wrote on last edited by
                          #14

                          just notice the main issue with this is, that the main ui needs to be visible aswell and forces the main ui to show, so I use show for the settings ui without showing the mainui.
                          but this is probably an general issue with instancing the settings ui in the main ui

                          1 Reply Last reply
                          0

                          • Login

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