如何强制QWidget显示在单独的窗口中?
我有
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget (QWidget *parent);
// ...
};
// here is ALL the code in MyWidget constructor
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
glWidget = new GLWidget(this, cluster);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(glWidget);
setLayout(mainLayout);
setWindowTitle("Visualization");
}
主窗口MainWindow w;
。
我想
- 从
w
创建 MyWidget 的新实例; - 在
QCloseEvent
之后或使用w
销毁实例(现在它们仅在QCloseEvent
之后销毁); - 该实例出现在新窗口中。
我正在创建 MyWidget
的新实例,如下所示:
void MainWindow::visualize()
{
MyWidget *widg = new MyWidget(this); // or widg = new MyWidget(0)
widg->show();
widg->raise();
widg->activateWindow();
}
当我尝试使用 w
作为 parent
创建 widg
时, widg
出现在 w
内部(位于左上角)。
解决这个问题最简单、最明确的方法是什么?
谢谢!
I have
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget (QWidget *parent);
// ...
};
// here is ALL the code in MyWidget constructor
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
glWidget = new GLWidget(this, cluster);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(glWidget);
setLayout(mainLayout);
setWindowTitle("Visualization");
}
and the main window MainWindow w;
.
I want
- to create new instances of MyWidget from
w
; - that instances to be destroyed after
QCloseEvent
or withw
(now they are destroyed only afterQCloseEvent
); - that instances to appear in new windows.
I am creating new instance of MyWidget
like this:
void MainWindow::visualize()
{
MyWidget *widg = new MyWidget(this); // or widg = new MyWidget(0)
widg->show();
widg->raise();
widg->activateWindow();
}
When I try to create widg
with w
as a parent
, widg
appears inside of the w
(in left top corner).
What is the easiest and most clear way to fix that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
Qt::Window
到QWidget
的构造函数应该执行您想要的操作。Adding
Qt::Window
to the constructor ofQWidget
should do what you want.正如 QWidget 的构造函数参考中所写,小部件成为窗口它的父级应该是 0。但是当父级为 0 时,它意味着父级是“YOU”:) - 也就是说,你必须照顾它们 - 将它们放在某个可到达的地方,并在适当的时候销毁它们(无论是在关闭事件时) 、析构函数或使用共享指针)。
As it is written in QWidget's constructor reference for a widget to become a window its parent should be 0. But when the parent is 0 it mens the parent is "YOU" :) - i.e you have to look after them - keep them to some reachable place and destroy them when the time is appropriate (either on close event, destructor or using shared pointers).