QQuickWidget的内容在父级的父级更改时消失

huangapple go评论85阅读模式
英文:

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-&gt;addSubWindow(this);

  // Create child QML widget
  auto* qml_widget = new QQuickWidget(engine, this);
  setWidget(qml_widget);

  // Add QML content
  auto* qml_obj = qobject_cast&lt;QQuickItem*&gt;(qml_component-&gt;create(engine-&gt;rootContext()));

  auto* root_item = qml_widget-&gt;quickWindow()-&gt;contentItem();
  qml_obj-&gt;setParent(root_item);
  qml_obj-&gt;setParentItem(root_item);

  // Show subwindow
  showMaximized();
}

// Move subwindow to a new MDI area
void MySubWindow::set_area(QMdiArea* new_area) {
  mdiArea()-&gt;removeSubWindow(this);
  new_area-&gt;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 编码 (&gt;&lt;) 转换为相应的符号以便于阅读,但实际代码中应该保留原始 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-&gt;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&lt;QQuickItem*&gt;(qml_component-&gt;create(engine-&gt;rootContext()));

  auto* root_item = qml_view-&gt;contentItem();
  qml_obj-&gt;setParent(root_item);
  qml_obj-&gt;setParentItem(root_item);

  // Show subwindow
  showMaximized();
}

// Move subwindow to a new MDI area
void MySubWindow::set_area(QMdiArea* new_area) {
  mdiArea()-&gt;removeSubWindow(this);
  new_area-&gt;addSubWindow(this);

  showMaximized();
}

huangapple
  • 本文由 发表于 2023年7月27日 23:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定