Facing error while connecting a QML child object signal to another object's slot
-
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: 500ColumnLayout { 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.
-
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. -
Try with old connect syntax.
-
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]
-
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]
-
I don't think they are in any way related.