Qt5位置相关函数异同详解(附源码)

时间:2022-03-13 12:04:47

Qt5中提供了丰富的位置和区域大小相关函数。下面讲一讲他们的区别。

主要函数:

1、x(),y(),pos():获取整个窗体左上角的坐标位置。

2、frameGeometry():获取整个窗体左上角的坐标和长宽值。

3、geometry():获取窗体*区域左上角的坐标和长宽值。

4、width()和height():获取窗体*区域的长宽值。

5、size():获取窗体*区域的长宽值。

6、rect():获得的左上角坐标始终为(0,0),长宽值为*区域的长宽值。

下面举一个实例,来区分这几个函数的功能。

实例:

1、新建GUI应用,项目名称为“Geometry”,基类选择“QDialog”,类名自定,取消“创建界面”复选框,最后完成项目工程的建立。

2、打开“dialog.h”,添加如下代码:

#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include <QLabel>
#include <QGridLayout> class Dialog : public QDialog
{
Q_OBJECT public:
Dialog(QWidget *parent = );
~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

3、打开"dialog.cpp",添加如下代码:

#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,,);
mainLayout->addWidget(xValueLabel,,); mainLayout->addWidget(yLabel,,);
mainLayout->addWidget(yValueLabel,,); mainLayout->addWidget(posLabel,,);
mainLayout->addWidget(posValueLabel,,); mainLayout->addWidget(frmLabel,,);
mainLayout->addWidget(frmValueLabel,,); mainLayout->addWidget(geoLabel,,);
mainLayout->addWidget(geoValueLabel,,); mainLayout->addWidget(widthLabel,,);
mainLayout->addWidget(widthValueLabel,,); mainLayout->addWidget(heightLabel,,);
mainLayout->addWidget(heightValueLabel,,); mainLayout->addWidget(rectLabel,,);
mainLayout->addWidget(rectValueLabel,,); mainLayout->addWidget(sizeLabel,,);
mainLayout->addWidget(sizeValueLabel,,); 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