1个对话框的坐标参数显示
介绍
这个是为了得到对话框的长宽,各个点的坐标,相对坐标,相对父窗口的坐标,相对在桌面的坐标
代码
geometry.h
/**
* 书本:【Qt5开发及实例】
* 功能:显示对话框的坐标信息,参数信息
* 文件:geometry.cpp
* 时间:2015年1月20日20:19:35
* 作者:cutter_point
*/
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QDialog>
#include <QLabel>
#include <QGridLayout>
class Geometry : public QDialog
{
Q_OBJECT
public:
Geometry(QWidget *parent = 0);
~Geometry();
//更新数据的显示
void updateLabel();
private:
QLabel *xLabel; //表示对话框的最开始的x坐标横向----->
QLabel *xValueLabel;
QLabel *yLabel; //表示对话框的最开始的y坐标纵向下箭头
QLabel *yValueLabel;
QLabel *FrmLabel; //综合位置和对话框的宽和长
QLabel *FrmValueLabel;
QLabel *posLabel; //位置坐标,就是x,y
QLabel *posValueLabel;
QLabel *geoLabel; //x,y和对话框不包括标题的大小,长宽
QLabel *geoValueLabel;
QLabel *widthLabel; //里面的宽
QLabel *widthValueLabel;
QLabel *heightLabel; //里面的长
QLabel *heightValueLabel;
QLabel *rectLabel; //起始的0,0和里面的长和宽
QLabel *rectValueLabel;
QLabel *sizeLabel; //里面的长宽
QLabel *sizeValueLabel;
QGridLayout *mainLayout; //网格布局
protected:
void moveEvent(QMoveEvent *); //对话框移动的话,坐标要改变
void resizeEvent(QResizeEvent *); //对话框大小改变的话
};
#endif // GEOMETRY_H
geometry.cpp
/**
* 书本:【Qt5开发及实例】
* 功能:显示对话框的坐标信息,参数信息
* 文件:geometry.cpp
* 时间:2015年1月20日20:19:35
* 作者:cutter_point
*/
#include "geometry.h"
Geometry::Geometry(QWidget *parent) //构造函数
: QDialog(parent)
{
setWindowTitle(tr("cutter_point Geometry"));
xLabel = new QLabel(tr("x():")); //坐标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(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();
}
//更新坐标
void Geometry::updateLabel()
{
QString xStr; //取得x()函数的结果并显示
xValueLabel->setText(xStr.setNum(x()));
QString yStr; //取得y()函数的结果并显示
yValueLabel->setText(yStr.setNum(y()));
QString frameStr; //取得frameGeometry函数的结果并显示
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; //取得pos函数的结果并显示
QString tempStr11, tempStr12;
positionStr = tempStr11.setNum(pos().x()) + ", " + tempStr12.setNum(pos().y());
posValueLabel->setText(positionStr);
QString geoStr; //取得geometry()函数的结果并显示
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; //取得width, height函数的结果并显示
widthValueLabel->setText(wStr.setNum(width()));
heightValueLabel->setText(hStr.setNum(height()));
QString rectStr; //取得rect函数的结果并显示
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; //取得size函数的结果并显示
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();
}
Geometry::~Geometry()
{
}
main.cpp
#include "geometry.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Geometry w;
w.show();
return a.exec();
}
运行结果
好吧,我发现照片超过1M就没法上传了,666666,我记得好像是2M的,怪我太天真???