标签:
Qt5中提供了丰富的位置和区域大小相关函数。下面讲一讲他们的区别。
1、x(),y(),pos():获取整个窗体左上角的坐标位置。
2、frameGeometry():获取整个窗体左上角的坐标和长宽值。
3、geometry():获取窗体中央区域左上角的坐标和长宽值。
4、width()和height():获取窗体中央区域的长宽值。
5、size():获取窗体中央区域的长宽值。
6、rect():获得的左上角坐标始终为(0,0),长宽值为中央区域的长宽值。
下面举一个实例,来区分这几个函数的功能。
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include <QGridLayout> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); void updateLabel(); private: QLabel *xLabel; QLabel *xValueLabel; QLabel *yLabel; QLabel *yValueLabel; QLabel *frmLabel; QLabel *frmValueLabel; QLabel *posLabel; QLabel *posValueLabel; QLabel *geoLabel; QLabel *geoValueLabel; QLabel *widthLabel; QLabel *widthValueLabel; QLabel *heightLabel; QLabel *heightValueLabel; QLabel *rectLabel; QLabel *rectValueLabel; QLabel *sizeLabel; QLabel *sizeValueLabel; QGridLayout *mainLayout; protected: void moveEvent(QMoveEvent *); //鼠标移动事件 void resizeEvent(QResizeEvent *); //大小改变 }; #endif // DIALOG_H
#include "dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("Geometry")); xLabel = new QLabel(tr("X(): ")); xValueLabel = new QLabel; yLabel = new QLabel(tr("Y(): ")); yValueLabel = new QLabel; frmLabel = new QLabel(tr("FrameGeometry(): ")); frmValueLabel = new QLabel; posLabel = new QLabel(tr("Pos(): ")); posValueLabel = new QLabel; geoLabel = new QLabel(tr("Geometry(): ")); geoValueLabel = new QLabel; widthLabel = new QLabel(tr("Width(): ")); widthValueLabel = new QLabel; heightLabel = new QLabel(tr("Height(): ")); heightValueLabel = new QLabel; rectLabel = new QLabel(tr("Rect(): ")); rectValueLabel = new QLabel; sizeLabel = new QLabel(tr("Size(): ")); sizeValueLabel = new QLabel; mainLayout = new QGridLayout(this); mainLayout->addWidget(xLabel,0,0); mainLayout->addWidget(xValueLabel,0,1); mainLayout->addWidget(yLabel,1,0); mainLayout->addWidget(yValueLabel,1,1); mainLayout->addWidget(posLabel,2,0); mainLayout->addWidget(posValueLabel,2,1); mainLayout->addWidget(frmLabel,3,0); mainLayout->addWidget(frmValueLabel,3,1); mainLayout->addWidget(geoLabel,4,0); mainLayout->addWidget(geoValueLabel,4,1); mainLayout->addWidget(widthLabel,5,0); mainLayout->addWidget(widthValueLabel,5,1); mainLayout->addWidget(heightLabel,6,0); mainLayout->addWidget(heightValueLabel,6,1); mainLayout->addWidget(rectLabel,7,0); mainLayout->addWidget(rectValueLabel,7,1); mainLayout->addWidget(sizeLabel,8,0); mainLayout->addWidget(sizeValueLabel,8,1); updateLabel(); //更新控件 } Dialog::~Dialog() { } void Dialog::updateLabel() { QString xStr; xValueLabel->setText(xStr.setNum(x())); //获取整个窗体左上角的位置 QString yStr; yValueLabel->setText(yStr.setNum(y())); //获取整个窗体左上角的位置 QString frameStr; //获取整个窗体左上角的位置和长宽值 QString tempStr1,tempStr2,tempStr3,tempStr4; frameStr = tempStr1.setNum(frameGeometry().x()) + "," + tempStr2.setNum(frameGeometry().y()) + "," + tempStr3.setNum(frameGeometry().width()) + ","+ tempStr4.setNum(frameGeometry().height()); frmValueLabel->setText(frameStr); QString geoStr; //获取窗体中央区域的左上角位置和长宽值 QString tempStr21,tempStr22,tempStr23,tempStr24; geoStr = tempStr21.setNum(geometry().x()) + "," + tempStr22.setNum(geometry().y()) + "," + tempStr23.setNum(geometry().width()) + "," + tempStr24.setNum(geometry().height()); geoValueLabel->setText(geoStr); QString positionStr; //获取窗体左上角的位置 QString tempStr11,tempStr12; positionStr = tempStr11.setNum(pos().x()) + "," + tempStr12.setNum(pos().y()); posValueLabel->setText(positionStr); QString wStr,hStr; //获取窗体中央区域的长宽值 widthValueLabel->setText(wStr.setNum(width())); heightValueLabel->setText(hStr.setNum(height())); QString rectStr; //获取窗体中央区域的左上角值和长宽值 QString tempStr31,tempStr32,tempStr33,tempStr34; rectStr = tempStr31.setNum(rect().x()) + "," + tempStr32.setNum(rect().y()) + "," + tempStr33.setNum(rect().width()) + "," + tempStr34.setNum(rect().height()); rectValueLabel->setText(rectStr); QString sizeStr; //获取窗体中央区域的长宽值 QString tempStr41,tempStr42; sizeStr = tempStr41.setNum(size().width()) + "," + tempStr42.setNum(size().height()); sizeValueLabel->setText(sizeStr); } void Dialog::moveEvent(QMoveEvent *) { updateLabel(); } void Dialog::resizeEvent(QResizeEvent *) { updateLabel(); }
http://blog.csdn.net/rl529014/article/details/51645440
标签:
原文地址:http://www.cnblogs.com/findumars/p/5581225.html