QT窗口API函数
geometry()
用于获取窗口在屏幕上的几何位置和大小
QRect geo = widget->geometry(); int x = geo.x(); // 窗口左上角的 x 坐标 int y = geo.y(); // 窗口左上角的 y 坐标 int w = geo.width(); // 窗口的宽度 int h = geo.height(); // 窗口的高度
width()
函数返回窗口控件的宽度
height()
函数返回窗口控件的高度
rect()
用于获取窗口控件的矩形区域。
QWidget *widget = new QWidget; // ... QRect r = widget->rect(); int x = r.x(); // 矩形区域左上角的 x 坐标 int y = r.y(); // 矩形区域左上角的 y 坐标 int width = r.width(); // 矩形区域的宽度 int height = r.height(); // 矩形区域的高度
例子
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { // ui->setupUi(this); resize(300,160); setWindowTitle("QT窗口常用的api位置函数测试"); //布局 glayout=new QGridLayout(this); labelgeometry=new QLabel("geometry()"); labelgeometryValue=new QLabel; labelWidth =new QLabel("Width"); labelWidthValue=new QLabel; labelHeight=new QLabel("Height"); labelHeightValue=new QLabel; labelRect=new QLabel("rect"); labelRectValue = new QLabel; labelSize=new QLabel("size"); labelSizeValue = new QLabel; glayout->addWidget(labelgeometry,0,0); glayout->addWidget(labelgeometryValue,0,1); glayout->addWidget(labelWidth,1,0); glayout->addWidget(labelWidthValue,1,1); glayout->addWidget(labelHeight,2,0); glayout->addWidget(labelHeightValue,2,1); glayout->addWidget(labelRect,3,0); glayout->addWidget(labelRectValue,3,1); glayout->addWidget(labelSize,4,0); glayout->addWidget(labelSizeValue,4,1); UpdateLabelFunc(); } Dialog::~Dialog() { delete ui; } void Dialog::resizeEvent(QResizeEvent *) { UpdateLabelFunc(); } void Dialog::moveEvent(QMoveEvent *) { UpdateLabelFunc(); } void Dialog::UpdateLabelFunc() { QString strgeometry; //专门存放geometry()函数的显示结果 QString str1,str2,str3,str4; strgeometry=str1.setNum(geometry().x())+"," +str2.setNum(geometry().y()) + "," +str3.setNum(geometry().width()) + "," +str4.setNum(geometry().height()); labelgeometryValue->setText(strgeometry); QString strw,strh; labelWidthValue->setText(strw.setNum(width())); labelHeightValue->setText(strh.setNum(height())); QString strrect; QString strrect1,strrect2,strrect3,strrect4; strrect=strrect1.setNum(rect().x())+"," +strrect2.setNum(rect().y())+"," +strrect3.setNum(rect().width())+"," +strrect4.setNum(rect().height()); labelRectValue->setText(strrect); QString strsize; QString strsize1,strsize2; strsize=strsize1.setNum(width())+","+strsize2.setNum(height()); labelSizeValue->setText(strsize); }
标签:glayout,QT,geometry,13,API,setNum,addWidget,new,QLabel From: https://www.cnblogs.com/baisedeyu/p/17888339.html