简体   繁体   中英

QQuickView child of QtWidget window

I am trying to to create a QQuickView sub-window that is embedded inside of an existing QWidget window. The below code I have creates a new separate QQuickView window instead of a sub-window.

main.cpp

subWindow::subWindow(QWidget* parent) {
    QQuickView* view = new QQuickView();
    view->setSource(QUrl("qrc:/main.qml"));
    view->show(); 
}

subWindow.cpp

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow) {

    subWindow* sw = new subWindow();
    QWidget *sub = QWidget::createWindowContainer(sw, this);
    sub->setMinimumSize(1061, 511);
    sub->setMaximumSize(1061, 511);
    sub->setFocusPolicy(Qt::TabFocus);
    ui->subVerticalLayout->addWidget(sub);
}

If you want to create a QWidget based on a QML , then better use a QQuickWidget , to do so add:

QT += quickwidgets

to your .pro

and in the end you only use the following:

QQuickWidget *sub = new QQuickWidget();
sub->setResizeMode(QQuickWidget::SizeRootObjectToView);
sub->setSource(QUrl("qrc:/main.qml"));
sub->setMinimumSize(1061, 511);
sub->setMaximumSize(1061, 511);
sub->setFocusPolicy(Qt::TabFocus);
ui->subVerticalLayout->addWidget(sub);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM