Qt 让弹出的窗口居中显示
Qt 让弹出的窗口居中显示
设置窗口居中显示这是在UI编程中经常要遇到的问题。
方法一:在窗口(QWidget类及派生类)的构造函数中添加如下代码:
#include <QDesktopWidget>
//.......
QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以
move((desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
//.......
重新编译后,该窗口启动时在屏幕居中的位置。
方法二:在调用show()函数后调用move()函数,j将窗口移动到屏幕中央。
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.move ((QApplication::desktop()->width() - w.width())/2,(QApplication::desktop()->height() - w.height())/2);
return a.exec();
}
Qt 的详细介绍:请点这里
Qt 的下载地址:请点这里
推荐阅读:
Ubuntu 环境下Gtk与QT编译环境安装与配置
Linux系统下QT环境搭建
Ubuntu下QT控制台程序无法运行的解决方案以及XTerm的配置方法
Ubuntu 10.04下QT4.7.4移植详解
Linux下为刚安装好的Qt Creator 创建桌面启动图标
Ubuntu 11.04安装QT Creator 2.5.0
Qt creator for PlayBook安装过程
评论暂时关闭