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. Facing error while connecting a QML child object signal to another object's slot
Forum Updated to NodeBB v4.3 + New Features

Facing error while connecting a QML child object signal to another object's slot

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 5.2k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    Ghost
    wrote on last edited by
    #1

    The setup I am using is Qt 5.1 + Qt Quick 2 + Qt Quick Controls.

    I have a Main QML window. I handle the processing part of the Main window in a C++ class called AppController.
    Now, Main has a button "Authorize", clicking which should spawn another Authorizer QML window. The way I have handled this is to invoke AppController::launchOAuth2Browser() slot by connecting it to Main.Authorize.clicked() signal. The slot creates an instance of Authorizer QML using the new QQmlApplicationEngine. Thus, the C++ class of Main handles the creation of the other QML window (which I don't know is the best way to do it).

    Now, Authorizer window has a child object WebView. I need to connect the WebView.urlChanged() signal to an OAuth2::onBrowserUrlChanged() slot.

    Here is the code that handles the window creation part, and the connection part.

    @
    void AppController::launchOauth2Browser(QUrl& url)
    {
    QQmlApplicationEngine *engine = new QQmlApplicationEngine("OAuth2Browser.qml");
    QQmlContext *context = engine->rootContext();
    context->setContextProperty("oauth2", m_oauth2);

    QObject *topLevel = engine->rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    window->show();
    
    QWebView *browser = topLevel->findChild<QWebView *>("Browser");
    qDebug() << "connecting urlChanged to onBrowserUrlChanged";
    QObject::connect(browser, &QWebView::urlChanged, m_oauth2, &OAuth2::onBrowserUrlChanged);
    browser->setUrl(url);
    
    qDebug() << "connecting closeBrowser to close";
    QObject::connect(m_oauth2, &OAuth2::closeBrowser, window, &QQuickWindow::close);
    

    }
    @

    However, when this code runs, it gives error at the following line:
    @
    QObject::connect(browser, &QWebView::urlChanged, m_oauth2, &OAuth2::onBrowserUrlChanged);
    @

    The error says:
    @
    QObject::connect: invalid null parameter
    @

    Here is the Authorizer window code:
    @
    Window {
    id: window
    width: 500
    height: 500

    ColumnLayout {
        anchors.fill: parent
    
        WebView {
            objectName: "Browser"
            id: browser
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
    
        Label {
            id: loadingStatus
            Layout.fillWidth: true
            text: browser.loadProgress + "% loaded."
        }
    }
    

    }
    @

    I know that m_oauth2 is not null - it has already been referenced earlier. So, browser has to be null, the reason of which I don't know. I have already created the QML window. Shouldn't the child objects be created with it as well?

    Aside from that, where do you think I should have handled the new window creation? from the Main QML window itself, using dynamic window management, or the current C++ implementation which has the AppController have to deal with UI code?

    P.S.: Look at the first reply (posted by me) that explains the now changed error.

    Bhoot

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Ghost
      wrote on last edited by
      #2

      OK. After further analysis, I tried finding child by casting it to QObject * first, and then static_casting is to QWebView *.
      I modified the code thus.

      @
      void AppController::launchOauth2Browser(QUrl& url)
      {
      QQmlApplicationEngine *engine = new QQmlApplicationEngine("OAuth2Browser.qml");
      QQmlContext *context = engine->rootContext();
      context->setContextProperty("oauth2", m_oauth2);

      QObject *topLevel = engine->rootObjects().value(0);
      QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
      window->show();
      
      qDebug() << "connecting closeBrowser to close";
      QObject::connect(m_oauth2, &OAuth2::closeBrowser, window, &QQuickWindow::close);
      
      //QWebView *browser = topLevel->findChild<QWebView *>("Browser");
      QObject *browserObject = topLevel->findChild<QObject *>("Browser");
      QWebView *webView = static_cast<QWebView *>(browserObject);
      
      qDebug() << "connecting urlChanged to onBrowserUrlChanged";
      QObject::connect(webView, &QWebView::urlChanged, m_oauth2, &OAuth2::onBrowserUrlChanged);
      webView->setUrl(url);
      

      }
      @

      Now, I do get the webView child object - its not NULL anymore. However, the program now just stops working when it tries to connect the webView's signal and m_oauth2's slot. Here,
      @
      QObject::connect(webView, &QWebView::urlChanged, m_oauth2, &OAuth2::onBrowserUrlChanged);
      @

      I get hit by the classic Windows error "Program.exe has stopped working".
      I dont know how to debug that now.

      Bhoot

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        Try with old connect syntax.

        (Z(:^

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Ghost
          wrote on last edited by
          #4

          OK. Meanwhile I try that, another question that rose in my mind is whether the QML WebView can actually be casted into QWebView or not.

          If not, then what is the appropriate castable object?

          [quote author="sierdzio" date="1376030612"]Try with old connect syntax.[/quote]

          Bhoot

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Ghost
            wrote on last edited by
            #5

            Yes. I am getting NULL while casting QML WebView to QWebView.
            Same case when casting into QGraphicsWebView.

            So, apparently, I can't do that. What are my options then?

            [quote author="sierdzio" date="1376030612"]Try with old connect syntax.[/quote]

            Bhoot

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              I don't think they are in any way related.

              (Z(:^

              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