目录
- 1、引言
- 2、实验代码
- 3、位置函数
- 3.1 x()
- 3.2 y()
- 3.3 frame()
- 3.4 pos()
- 3.5 geometry()
- 3.6 width()
- 3.7 height()
- 3.8 rect()
- 3.9 size()
1、引言
QT有众多图形绘制函数,包括x()、y()、frame()、pos()、geometry()、width()、height()、rect()、size(),它们对屏幕坐标的捕获是有区别的。
2、实验代码
代码出自Qt5开发及实例(第3版)
geometry.h
#pragma once
#include <QtWidgets/QDialog>
#include <QLabel>
#include <QGridLayout>
class geometry : public QDialog
{
Q_OBJECT
public:
geometry(QWidget *parent = nullptr);
~geometry();
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 *);
};
geometry.cpp
#include "geometry.h"
geometry::geometry(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("CH601"));
xLabel = new QLabel(tr("x():"));
xValueLabel = new QLabel;
yLabel = new QLabel(tr("y():"));
yValueLabel = new QLabel;
FrmLabel = new QLabel(tr("Frame():"));
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(FrmLabel, 2, 0);
mainLayout->addWidget(FrmValueLabel, 2, 1);
mainLayout->addWidget(posLabel, 3, 0);
mainLayout->addWidget(posValueLabel, 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();
}
geometry::~geometry()
{}
void geometry::updateLabel()
{
QString xStr;
xValueLabel->setText(xStr.setNum(x()));
QString yStr;
yValueLabel->setText(xStr.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 positionStr;
QString tempStr11, tempStr12;
positionStr = tempStr11.setNum(pos().x()) + "," + tempStr12.setNum(pos().y());
posValueLabel->setText(positionStr);
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 wStr, hStr;
widthValueLabel->setText(wStr.setNum(width()));
heightValueLabel->setText(wStr.setNum(height()));
QString rectStr;
QString tempSr31, tempStr32, tempStr33, tempStr34;
rectStr = tempSr31.setNum(rect().x()) + "," + tempStr32.setNum(rect().y())+ ","
+ tempStr33.setNum(/*rect().width()*/width()) + "," + tempStr34.setNum(/*rect().height()*/height());
rectValueLabel->setText(rectStr);
QString sizeStr;
QString tempStr41, tempStr42;
sizeStr = tempStr41.setNum(size().width()) + "," + tempStr42.setNum(size().height());
sizeValueLabel->setText(sizeStr);
}
void geometry::moveEvent(QMoveEvent*)
{
updateLabel();
}
void geometry::resizeEvent(QResizeEvent*)
{
updateLabel();
}
实验结果:
结果1:
更改大小后得到结果2:
3、位置函数
3.1 x()
显示当前widget左上角坐标相对于主界面的x轴位置,x轴是向右计算。
3.2 y()
显示当前widget左上角坐标相对于主界面的y轴位置,y轴向下计算。
3.3 frame()
frame().x()和frame().y()和x()、y()相同,都表示左上角的坐标点,frame().width()表示当前整个程序框的宽度,frame().height()表示整个程序框的高度。
3.4 pos()
pos().x()和pos().y()都是相对于主界面来说的坐标点,与x()、y()相同。
3.5 geometry()
geometry().x()和geometry().y()返回当前主界面的坐标位置,同时geometry().width()和geometry().height()会返回主界面的大小,这里的Dialog属于是子窗口。
3.6 width()
width()返回当前窗口的宽度。
3.7 height()
height()返回当前窗口的高度。
3.8 rect()
rect().width()和rect().height()返回当前窗口的宽和高。
3.9 size()
size().width()和size().height()返回当前窗口的宽和高。