在QtQml中以全屏模式打开新组件

时间:2014-04-12 09:51:37

标签: c++ qt qml fullscreen

我有一个使用qml和c ++的Qt应用程序。 Gui是几个组成部分。我想在全屏模式下打开一个新的组件(一个虚拟键盘),但打开这个新模块,不应该破坏打开的模块,只是以全屏大小显示在它们上面。

我尝试使用相同的QQuickView对象使用c ++扩展打开此键盘。它有效,但我正在努力将键盘焦点传递给新元素。

这就是我从c ++回调函数中打开虚拟键盘的方法:

QQmlComponent component(viewer->engine(), QUrl::fromLocalFile("qml/VirtualKeyboard.qml"));
keyboard = component.create();
QQmlProperty::write(keyboard, "parent", QVariant::fromValue<QObject*>(static_cast<QObject*>(viewer->rootObject())));
QQmlEngine::setObjectOwnership(keyboard, QQmlEngine::CppOwnership);

有没有更简单的方法可以将新组件作为子元素打开,但是全屏? 或者我该怎么做才能将焦点传递给新元素。我尝试使用以下代码,但它们都没有工作。

    viewer->rootObject()->childItems()[0]->setEnabled(false);   //original components
    viewer->rootObject()->childItems()[0]->setFocus(false);
    viewer->rootObject()->childItems()[1]->forceActiveFocus();  //virtual keyboard component

有什么建议吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

最后我解决了它。我不确定错误是什么,但可能我忘记了将VirtualKeyboard.qml作为FocusScope元素。

我还修改了GUI的结构,以便在全屏模式下更轻松地删除弹出窗口和其他消息框窗口。我写下了我的最终解决方案,以后可能对其他人有用。

首先我有一个在开始时运行的main.qml

Rectangle {
    id: mainWindow
    width: 800
    height: 600
    objectName: "mainWindow"

    Loader {
        objectName: "mainLoader"
        anchors.fill: parent
        source: ""

        onLoaded: item.objectName="loadedMain";
    }

    Loader {
        objectName: "popupLoader"
        anchors.fill: parent
        source: ""

        onLoaded: item.objectName="loadedPopup";
    }
}

在c逻辑中,使用了以下功能。

//How to open main windows
void MainUserSelection::showWindow(QString type) {

    QObject *main = viewer->rootObject()->findChild<QObject*>("mainLoader",Qt::FindChildrenRecursively);
    if (main==NULL)
        return;

    if (type=="SettingsWindow") {
        main->setProperty("source", "qml/Settings.qml");
        return;
    }
}

//how to open popups
void MainUserSelection::showPopup(QString type, QStringList params) {

    if (type=="PictureSelect") {

        QObject *main = viewer->rootObject()->findChild<QObject*>("popupLoader",Qt::FindChildrenRecursively);
        if (main!=NULL) {
            main->setProperty("source", "qml/Popups/PictureSelect.qml");

            QQuickItem *item = viewer->rootObject()->findChild<QQuickItem *>("loadedPopup");
            if (item!=NULL)
                item->forceActiveFocus();

            if (params.count()>0)
                item->setProperty("textEnglish",params[0]);

            if (params.count()>1)
                item->setProperty("textHungarian",params[1]);
        }

        return;
    }

    if (type=="Keyboard") {

        QObject *main = viewer->rootObject()->findChild<QObject*>("popupLoader",Qt::FindChildrenRecursively);
        if (main!=NULL) {
            main->setProperty("source", "qml/Popups/VirtualKeyboard.qml");

            QQuickItem *item = viewer->rootObject()->findChild<QQuickItem *>("loadedPopup");
            if (item!=NULL) {
                item->forceActiveFocus();
                if (params.count()>0)
                    item->setProperty("text",params[0]);

                if (params.count()>1)
                    item->setProperty("placeholder",params[1]);
            }
        }

        return;
    }
}

//popup windows returns by calling this function
void MainUserSelection::popupReturned(QString type, QStringList params) {

    QObject *main = viewer->rootObject()->findChild<QObject*>("popupLoader",Qt::FindChildrenRecursively);
    if (main!=NULL)
        main->setProperty("source", "");

    QQuickItem *item = viewer->rootObject()->findChild<QQuickItem *>("loadedMain");
    if (item!=NULL)
        item->forceActiveFocus();

    //also emit finish signal to main window qml
    emit popupFinished(type,params);
}

在MainUserSelection类

viewer->engine()->rootContext()->setContextProperty("userselection",this);

最后我从qml再见打开popup:

userselection.showPopup("PictureSelect","")
userselection.popupFinished.connect(someQmlReturnFunction);  //don't forget to disconnect