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. [Windows] Retrieving a window handle of a control using objectName

[Windows] Retrieving a window handle of a control using objectName

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.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.
  • N Offline
    N Offline
    Nikola Ninkov
    wrote on last edited by
    #1

    Hello,

    I am working on an application featuring a video stream, played by a .dll plugin and shown in a QML-based GUI, with various controls on top (overlaying the stream). In order to play the stream, I have to retrieve a handle of the QML object and pass it to the DLL. From what I've read, the way to do that is via the objectName property, in conjunction with the findChild() method. I have the following:

    //MainWindow.qml:

    ApplicationWindow {
    /* ... */
      Rectangle {
      objectName: "streamRectangle"
      /* ... */
      }
    /* ... */
    }
    

    //main.cpp:

    int main(int argc, char *argv[])
    {
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;
    
      engine.load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
      if (engine.rootObjects().isEmpty())
        return -1;
    
      QObject *rootObject = engine.rootObjects().first();
      if (rootObject)
      {
        QObject *rect = rootObject->findChild<QObject*>("streamRectangle");
        if (rect)
        {
          std::cout << "Child item found" << std::endl;
          QWindow *window = qobject_cast<QWindow*>(rect);
          if (window)
          {
            std::cout << "Successfully retrieved stream window" << std::endl;
            WId wid = window->winId();
              if (wid)
              {
                std::cout << "Successfully retrieved WID!" << std::endl;
                // make use of the ID here
              }
          }
        }
      }
    
      return app.exec();
    }
    

    This compiles, however I get: qrc:/MainWindow.qml:27: ReferenceError: streamRectangle is not defined as an error message.
    A few searches later, I see that I'm supposed to add "streamRectangle" to the root context (?). I am, however, unable to do that successfully.

    Any thoughts on the issue? Help is greatly appreciated.

    1 Reply Last reply
    0
    • N Nikola Ninkov

      @IntruderExcluder This is MainWindow.qml - the only QML code being loaded:

      import QtQuick 2.12
      import QtQuick.Controls 2.12
      
      ApplicationWindow {
          visible: true
          width: 1280
          height: 720
      
          Rectangle {
              anchors.fill: parent
              objectName: streamRectangle
          }
      
      }
      

      And here's main.cpp:

      #include <QtQuick>
      
      int main(int argc, char *argv[])
      {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
      
        engine.load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
        if (engine.rootObjects().isEmpty())
          return -1;
      
        QObject *rootObject = engine.rootObjects().first();
        if (rootObject)
        {
          QObject *rect = rootObject->findChild<QObject*>("streamRectangle");
          if (rect)
          {
            QWindow *window = qobject_cast<QQuickItem*>(rect)->window();
            if (window)
            {
              WId wid = window->winId();
              if (wid)
              {
                // do something with wid
              }
            }
          }
        }
      
        return app.exec();
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #6

      @Nikola-Ninkov said in [Windows] Retrieving a window handle of a control using objectName:

      ApplicationWindow {
      visible: true
      width: 1280
      height: 720

      Rectangle {
          anchors.fill: parent
          objectName: streamRectangle
      }
      

      }

      that is not what you wrote in the opening post!
      streamRectangle has to be a string

      ApplicationWindow {
          visible: true
          width: 1280
          height: 720
      
          Rectangle {
              anchors.fill: parent
              objectName: "streamRectangle"
          }
      
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      N 1 Reply Last reply
      3
      • IntruderExcluderI Offline
        IntruderExcluderI Offline
        IntruderExcluder
        wrote on last edited by
        #2

        You are doing something strange here. Once you found your rectangle by object name you can cast it to a QQuickItem* and use window method of this item.

        N 1 Reply Last reply
        0
        • IntruderExcluderI IntruderExcluder

          You are doing something strange here. Once you found your rectangle by object name you can cast it to a QQuickItem* and use window method of this item.

          N Offline
          N Offline
          Nikola Ninkov
          wrote on last edited by
          #3

          @IntruderExcluder Thanks for your input. If I understand correctly, you are suggesting:

          QObject *rootObject = engine.rootObjects().first();
          if (rootObject)
            {
              QObject *rect = rootObject->findChild<QObject*>("streamRectangle");
              if (rect)
              {
                QWindow *window = qobject_cast<QQuickItem*>(rect)->window();
              /* ... */
              }
          }
          

          I am still getting qrc:/MainWindow.qml:27: ReferenceError: streamRectangle is not defined. How do I fix this?

          1 Reply Last reply
          0
          • IntruderExcluderI Offline
            IntruderExcluderI Offline
            IntruderExcluder
            wrote on last edited by IntruderExcluder
            #4

            We need more QML code to understand what happening.

            N 1 Reply Last reply
            0
            • IntruderExcluderI IntruderExcluder

              We need more QML code to understand what happening.

              N Offline
              N Offline
              Nikola Ninkov
              wrote on last edited by
              #5

              @IntruderExcluder This is MainWindow.qml - the only QML code being loaded:

              import QtQuick 2.12
              import QtQuick.Controls 2.12
              
              ApplicationWindow {
                  visible: true
                  width: 1280
                  height: 720
              
                  Rectangle {
                      anchors.fill: parent
                      objectName: streamRectangle
                  }
              
              }
              

              And here's main.cpp:

              #include <QtQuick>
              
              int main(int argc, char *argv[])
              {
                QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              
                QGuiApplication app(argc, argv);
                QQmlApplicationEngine engine;
              
                engine.load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
                if (engine.rootObjects().isEmpty())
                  return -1;
              
                QObject *rootObject = engine.rootObjects().first();
                if (rootObject)
                {
                  QObject *rect = rootObject->findChild<QObject*>("streamRectangle");
                  if (rect)
                  {
                    QWindow *window = qobject_cast<QQuickItem*>(rect)->window();
                    if (window)
                    {
                      WId wid = window->winId();
                      if (wid)
                      {
                        // do something with wid
                      }
                    }
                  }
                }
              
                return app.exec();
              }
              
              J.HilkJ 1 Reply Last reply
              0
              • N Nikola Ninkov

                @IntruderExcluder This is MainWindow.qml - the only QML code being loaded:

                import QtQuick 2.12
                import QtQuick.Controls 2.12
                
                ApplicationWindow {
                    visible: true
                    width: 1280
                    height: 720
                
                    Rectangle {
                        anchors.fill: parent
                        objectName: streamRectangle
                    }
                
                }
                

                And here's main.cpp:

                #include <QtQuick>
                
                int main(int argc, char *argv[])
                {
                  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                
                  QGuiApplication app(argc, argv);
                  QQmlApplicationEngine engine;
                
                  engine.load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
                  if (engine.rootObjects().isEmpty())
                    return -1;
                
                  QObject *rootObject = engine.rootObjects().first();
                  if (rootObject)
                  {
                    QObject *rect = rootObject->findChild<QObject*>("streamRectangle");
                    if (rect)
                    {
                      QWindow *window = qobject_cast<QQuickItem*>(rect)->window();
                      if (window)
                      {
                        WId wid = window->winId();
                        if (wid)
                        {
                          // do something with wid
                        }
                      }
                    }
                  }
                
                  return app.exec();
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @Nikola-Ninkov said in [Windows] Retrieving a window handle of a control using objectName:

                ApplicationWindow {
                visible: true
                width: 1280
                height: 720

                Rectangle {
                    anchors.fill: parent
                    objectName: streamRectangle
                }
                

                }

                that is not what you wrote in the opening post!
                streamRectangle has to be a string

                ApplicationWindow {
                    visible: true
                    width: 1280
                    height: 720
                
                    Rectangle {
                        anchors.fill: parent
                        objectName: "streamRectangle"
                    }
                
                }
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                N 1 Reply Last reply
                3
                • J.HilkJ J.Hilk

                  @Nikola-Ninkov said in [Windows] Retrieving a window handle of a control using objectName:

                  ApplicationWindow {
                  visible: true
                  width: 1280
                  height: 720

                  Rectangle {
                      anchors.fill: parent
                      objectName: streamRectangle
                  }
                  

                  }

                  that is not what you wrote in the opening post!
                  streamRectangle has to be a string

                  ApplicationWindow {
                      visible: true
                      width: 1280
                      height: 720
                  
                      Rectangle {
                          anchors.fill: parent
                          objectName: "streamRectangle"
                      }
                  
                  }
                  
                  N Offline
                  N Offline
                  Nikola Ninkov
                  wrote on last edited by
                  #7

                  @J-Hilk I think I just sank through my office floor. It works now.

                  As for the differences from the OP, I went back to the file and removed everything not directly relevant to the object I was targeting. Guess I also made the 0 IQ play with the quotation marks.

                  Thank you very much.

                  1 Reply Last reply
                  1

                  • Login

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