有没有办法初始化QDialog的宽度和高度,而不使用ui文件更改x和y坐标?我只有一个简单的QDialog并且只想设置宽度和高度,并且x和y自动设置为父级的中心,但是当我尝试setGeometry时,继承的几何的x和y是0. x怎么样当使用ui文件创建对话框时,是否设置了?
class MyDialog : public QDialog
{
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent)
{
setGeometry(geometry().x(), geometry().y(), 200, 400);
}
}
Run Code Online (Sandbox Code Playgroud)
我有更好的解决方案:
class MyDialog : public QDialog
{
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent)
{
int nWidth = 300;
int nHeight = 400;
if (parent != NULL)
setGeometry(parent->x() + parent->width()/2 - nWidth/2,
parent->y() + parent->height()/2 - nHeight/2,
nWidth, nHeight);
else
resize(nWidth, nHeight);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我想扩展您的解决方案,使其也可以在具有第二个监视器的系统上工作(即使这是一个旧线程......):
...
if (parent != NULL)
QPoint parentPos = parent->mapToGlobal(parent->pos());
setGeometry(parentPos.x() + parent->width()/2 - nWidth/2,
parentPos.y() + parent->height()/2 - nHeight/2,
nWidth, nHeight);
else
...
Run Code Online (Sandbox Code Playgroud)
马塞尔
| 归档时间: |
|
| 查看次数: |
21101 次 |
| 最近记录: |