QWidget居中显示(qt窗口坐标原点是在”左上角”的,有图)
转载请说明出处, 并附上原文链接http://blog.csdn.net/qq907482638/article/details/72189014.
问题描述
在Qt学习过程中,在让QDialog居中显示的时候, 出现了一点问题. 然而百度的都是大同小异. 都不行.不知道为什么, 难道是我的搜索姿势不对. 于是自己实现了居中显示的函数.
须知
- 以下函数只要继承QWidget都可以使用.
- 例如 QDialog, QPushButton( -v- 一个居中的”引爆按钮”)
- 关于坐标问题: qt窗口坐标原点是在”左上角”的.

如图, (x2, y2)是我窗口的分辨率的一半
无论目前我的窗口在什么位置,我只要把窗口原点设置为(x1, y1)就行了.
所以目前我要获得(x1, y1)的值, 那就很简单啦.
通过
//app就是当前要居中的窗口
appWindowWidth = app->geometry()->width();
appWindowHeight = app->geometry()->height();
x2 = 屏幕宽度 / 2
y2 = 屏幕高度 / 2
最后:
x1 = x2 - appWindowWidth / 2
y1 = y2 -appWindowHeight / 2
然后把窗口中心设置为(x1, y1)就行了.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
实现细节
void LoginDialog::setCentralDisplay()
{
QDesktopWidget *screenResolution = QApplication::desktop();
int appWindowWidth = this->geometry().width();
int appWindowHeight = this->geometry().height();
int center_y = screenResolution->height()/2 - appWindowHeight/2;
int center_x = screenResolution->width()/2 - appWindowWidth/2;
//此处的Width,Height不要被修改了(例如除以2了)
//不然看起来不是居中的
setGeometry(center_x, center_y,
appWindowWidth,appWindowHeight);
//以下用于调试
qDebug()<<"origin_width"<<screenResolution->width();
qDebug()<<"origin_height"<<screenResolution->height();
qDebug()<<"window_width"<<appWindowWidth;
qDebug()<<"window_height"<<appWindowHeight;
qDebug()<<"center"<<center_x;
qDebug()<<"center"<<center_y;
}
http://blog.csdn.net/qq907482638/article/details/72189014
QWidget居中显示(qt窗口坐标原点是在”左上角”的,有图)的更多相关文章
- QT从入门到入土(二)——对象模型(对象树)和窗口坐标体系
摘要 我们使用的标准 C++,其设计的对象模型虽然已经提供了非常高效的 RTTI 支持,但是在某些方面还是不够灵活.比如在 GUI 编程方面,既需要高效的运行效率也需要强大的灵活性,诸如删除某窗口时可 ...
- Qt窗口屏幕居中显示(有专门的QDesktopWidget,先计算后显示)
窗口的屏幕居中显示问题,在各开发工具中原理相同,首先使用特定的方法得到显示屏幕的宽度和高度,再根据窗口本身的宽度和高度计算出窗口的左上角坐标位置. Qt中可以采用两种方法达到窗口的屏幕居中显示: 方法 ...
- Qt窗口屏幕居中显示
转自--> http://blog.chinaunix.net/uid-20718335-id-364404.html 窗口的屏幕居中显示问题,在各开发工具中原理相同,首先使用特定的方法得到显示 ...
- 【Qt】窗口居中显示
w.move((a.desktop()->width() - w.width())/, (a.desktop()->height() - w.height())/); 上述方法可以置中,但 ...
- Qt 设置窗口居中显示和窗体大小
设置窗口居中显示 方法一:在窗口(QWidget类及派生类)的构造函数中添加如下代码: #include <QDesktopWidget> //....... QDesktopWidget ...
- python之tkinter使用-窗口居中显示
# 窗口居中显示 import tkinter as tk def set_win_center(root, curWidth='', curHight=''): ''' 设置窗口大小,并居中显示 : ...
- Example005控制弹出窗口居中显示
<!-- 实例005控制弹出窗口居中显示 --> <head> <meta charset="UTF-8"> </head> < ...
- Java_Swing中让窗口居中显示的方法(三种方法)
方法一: int windowWidth = frame.getWidth(); // 获得窗口宽 int windowHeight = frame.getHeight(); // 获得窗口高 ...
- qt用于图片显示的窗口
用于图片显示的窗口 国产化
随机推荐
- HDU 1280 前m大的数 基数排序
http://acm.hdu.edu.cn/showproblem.php?pid=1280 题目大意: 给你N(N<=3000)个数(这些数不超过5000),要求输出他们两两相加后和最大的M( ...
- 3、Pycharm使用
1.设置文件模板 file->settings->Editor->File and Code Templates->Python Script 2.运行 a.点击要运行的文件, ...
- Range锁(也即范围锁)
浅析SQL Server在可序列化隔离级别下,防止幻读的范围锁的锁定问题 本文出处:http://www.cnblogs.com/wy123/p/7501261.html (保留出处并非什么原创作品权 ...
- js循环一维数组按指定长度截取为二维数组
//随便创建一个数组 let data = "abcdefghijklmnopkrstuvw12322999".split(""); //总数组 let pro ...
- 安卓 WebView加载本地图片时居中显示
在一个项目中使用WebView显示gif图片(自定义的View无法放大gif),当图片过小时只在左侧显示,经过研究发现无论设置android:layout_gravity="center_h ...
- Bluetooth---初步了解Android 蓝牙4.0
http://developer.android.com/reference/android/bluetooth/package-summary.html android.bluttooth 提供管理 ...
- javascript数组全排列,数组元素所有组合
function permute(input) { var permArr = [], usedChars = []; function main(input){ var i, ch; for (i ...
- 【35.43%】【hdu 4347】The Closest M Points
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) Total Submissio ...
- AngularJS之forEach
angular.forEach 描述: 循环对obj对象的每个元素调用iterator, obj对象可以是一个Object或一个Array. Iterator函数调用方法: iterator( ...
- Linux下iptables屏蔽IP和端口号
http://blog.csdn.net/kobejayandy/article/details/24332597 iptables 屏蔽端口