QWidget setGeometry 在不使用 QLayout 的情况下显示 [英] QWidget setGeometry show without use of a QLayout
本文介绍了QWidget setGeometry 在不使用 QLayout 的情况下显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标是在另一个 QWidget 中绘制一个 QWidget 子类.只提供坐标.
The target is to paint a QWidget subclass in a other QWidget. By give only the coords.
#include <QApplication>
#include <QWidget>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget* w = new QWidget;
w->show();
QLabel* l = new QLabel;
l->setText("Hello World!");
l->setParent(w);
l->setGeometry(0,0,100,100);
return a.exec();
}
为什么我在窗户上什么也看不到.
Why i see nothing on the window.
推荐答案
您必须致电 QWidget::show
显示标签,因为您在之后添加了它,父小部件已经显示.
You must call QWidget::show
to show the label since you add it after the parent widget has already been shown.
QLabel* l = new QLabel;
l->setText("Hello World!");
l->setParent(w);
l->setGeometry(0,0,100,100);
l->show();
另一种解决方案是在添加了所有子小部件后显示父小部件.您不需要在堆中明确分配任何内容:
An alternative solution is to show the parent after all the child widgets are already added. You don't need to allocate anything explicitly the heap:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QLabel l("Hello World!", &w);
l.setGeometry(0,0,100,100);
w.show();
return a.exec();
}
这篇关于QWidget setGeometry 在不使用 QLayout 的情况下显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文