英文:
QQuickWidget's content disappears when parent's parent changes
问题
我正在尝试构建一个应用程序,其中包含多个包含QQuickWidget的QMdiSubWindows,并且可以在不同的QMdiAreas之间移动。我能够创建子窗口并在其中显示QML内容,但当子窗口移动到新的MDI区域时,此内容会消失。以下是示例代码。
// 构造函数
MySubWindow::MySubWindow(QQmlComponent* qml_component, QQmlEngine* engine,
QWidget* parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) {
// 添加到MDI区域
initial_area->addSubWindow(this);
// 创建子QML小部件
auto* qml_widget = new QQuickWidget(engine, this);
setWidget(qml_widget);
// 添加QML内容
auto* qml_obj = qobject_cast<QQuickItem*>(qml_component->create(engine->rootContext()));
auto* root_item = qml_widget->quickWindow()->contentItem();
qml_obj->setParent(root_item);
qml_obj->setParentItem(root_item);
// 显示子窗口
showMaximized();
}
// 将子窗口移动到新的MDI区域
void MySubWindow::set_area(QMdiArea* new_area) {
mdiArea()->removeSubWindow(this);
new_area->addSubWindow(this);
showMaximized();
}
我正在使用一个简单的QML组件,其中包含一个彩色矩形以进行测试。在创建MySubWindow
时,我可以看到矩形,但在调用set_area()
后,我得到一个空白的白色窗口。我已尝试在移动到contentItem
后重新设置QML对象的父项,就像构造函数中所示,但这会导致段错误。有何建议?
英文:
I'm trying to build an application consisting of multiple QMdiSubWindows that each contain a QQuickWidget and can be moved between different QMdiAreas. I'm able to create subwindows and display QML content inside them, but this content disappears when the subwindow is moved to a new MDI area. Example code follows.
// Constructor
MySubWindow::MySubWindow(QQmlComponent* qml_component, QQmlEngine* engine,
QWidget* parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) {
// Add to MDI area
initial_area->addSubWindow(this);
// Create child QML widget
auto* qml_widget = new QQuickWidget(engine, this);
setWidget(qml_widget);
// Add QML content
auto* qml_obj = qobject_cast<QQuickItem*>(qml_component->create(engine->rootContext()));
auto* root_item = qml_widget->quickWindow()->contentItem();
qml_obj->setParent(root_item);
qml_obj->setParentItem(root_item);
// Show subwindow
showMaximized();
}
// Move subwindow to a new MDI area
void MySubWindow::set_area(QMdiArea* new_area) {
mdiArea()->removeSubWindow(this);
new_area->addSubWindow(this);
showMaximized();
}
I'm using a simple QML component that contains a colored rectangle to test. I can see the rectangle when MySubWindow
is created, but I get a blank white window after set_area()
is called. I've tried to reparent my QML object after the move to contentItem
like shown in the constructor, but this causes a segfault. Any thoughts?
答案1
得分: 0
我不确定为什么,但通过将代码从 QQuickWidget
切换到 QQuickView
,我成功解决了这个问题。修改后的代码如下:
// 构造函数
MySubWindow::MySubWindow(QQmlComponent* qml_component, QQmlEngine* engine,
QWidget* parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) {
// 添加到 MDI 区域
initial_area->addSubWindow(this);
// 创建子 QML 窗口
auto* qml_view = new QQuickView();
auto* qml_widget = QWidget::createWindowContainer(qml_view, this);
setWidget(qml_widget);
// 添加 QML 内容
auto* qml_obj = qobject_cast<QQuickItem*>(qml_component->create(engine->rootContext()));
auto* root_item = qml_view->contentItem();
qml_obj->setParent(root_item);
qml_obj->setParentItem(root_item);
// 显示子窗口
showMaximized();
}
// 将子窗口移动到新的 MDI 区域
void MySubWindow::set_area(QMdiArea* new_area) {
mdiArea()->removeSubWindow(this);
new_area->addSubWindow(this);
showMaximized();
}
注意:我已经将 HTML 编码 (>
和 <
) 转换为相应的符号以便于阅读,但实际代码中应该保留原始 HTML 编码。
英文:
I'm not sure why, but I was able to fix the issue by switching from QQuickWidget
to QQuickView
. The modified code follows:
// Constructor
MySubWindow::MySubWindow(QQmlComponent* qml_component, QQmlEngine* engine,
QWidget* parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) {
// Add to MDI area
initial_area->addSubWindow(this);
// Create child QML widget
auto* qml_view = new QQuickView();
auto* qml_widget = QWidget::createWindowContainer(qml_view, this);
setWidget(qml_widget);
// Add QML content
auto* qml_obj = qobject_cast<QQuickItem*>(qml_component->create(engine->rootContext()));
auto* root_item = qml_view->contentItem();
qml_obj->setParent(root_item);
qml_obj->setParentItem(root_item);
// Show subwindow
showMaximized();
}
// Move subwindow to a new MDI area
void MySubWindow::set_area(QMdiArea* new_area) {
mdiArea()->removeSubWindow(this);
new_area->addSubWindow(this);
showMaximized();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论