第六章 Qt5 图形与图片——位置相关函数

目录

一、简介

二、代码实现

1.窗体移动和调整大小

2.获取结果以frameGeometry()为例。

3.整体代码

.h文件

.cpp文件

4.运行结果


一、简介

        Qt提供了许多关于获取窗体位置及显示区域大小的函数,如x(),y(),pos(),size(),geometry(),frameGeometry(),rect()等,统称为“位置相关函数”。

第六章 Qt5 图形与图片——位置相关函数_第1张图片

1、x()、y()和pos()函数作用都是获得整个窗体左上角的坐标位置。

2、frameGeometry()与geometry()相对应,frameGeometry()获得整个窗体的顶点和长、宽值,而geometry()获得整个窗体内中心区域的左上顶点坐标及长、宽值

3、直接调用width()、height()获取中央区域的长、宽值

4、rect()size()获得的结果也都是对于窗体的中心区域而言的。size()获取中央区域的长、宽值。rect()获取的长、宽值和geometry()一样,但是它的左上顶点始终为(0,0)

二、代码实现

1.窗体移动和调整大小

        QMoveEvent 类包含移动事件的事件参数。QResizeEvent 类包含调整大小事件的事件参数。所以在private里添加void moveEvent(QMoveEvent * )void resizeEvent(QResizeEvent *),当窗体改变时做出响应

2.获取结果以frameGeometry()为例。

    QString frameStr;
    QString tempStr1,tempStr2,tempStr3,tempStr4;
    frameStr = tempStr1.setNum(frameGeometry().x())+","
            +tempStr2.setNum(frameGeometry().y())+","
            +tempStr3.setNum(frameGeometry().width())+","
            +tempStr4.setNum(frameGeometry().height());
    frameValue->setText(frameStr);

3.整体代码

.h文件

#ifndef GEOMETRY_H
#define GEOMETRY_H

#include 
#include 
#include 

class Geometry : public QDialog
{
    Q_OBJECT

public:
    Geometry(QWidget *parent = 0);
    ~Geometry();
private:
    void updateLabel();
    void moveEvent(QMoveEvent *);
    void resizeEvent(QResizeEvent *);
private:
    QLabel * xLabel;
    QLabel * xValue;
    QLabel * yLabel;
    QLabel * yValue;
    QLabel * posLabel;
    QLabel * posValue;
    QLabel * frameLabel;
    QLabel * frameValue;
    QLabel * geometryLabel;
    QLabel * geometryValue;
    QLabel * widthLabel;
    QLabel * widthValue;
    QLabel * heightLabel;
    QLabel * heightValue;
    QLabel * rectLabel;
    QLabel * rectValue;
    QLabel * sizeLabel;
    QLabel * sizeValue;
    QGridLayout * mainLayout;
};

#endif // GEOMETRY_H

.cpp文件

#include "geometry.h"
#include 

Geometry::Geometry(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Geometry"));
    QFont font("黑体",12);
    setFont(font);
    xLabel = new QLabel(tr("x:"));
    xValue = new QLabel;
    yLabel = new QLabel(tr("y:"));
    yValue = new QLabel;
    frameLabel = new QLabel(tr("frame:"));
    frameValue = new QLabel;
    posLabel = new QLabel(tr("pos:"));
    posValue = new QLabel;
    geometryLabel = new QLabel(tr("geometry:"));
    geometryValue = new QLabel;
    widthLabel = new QLabel(tr("widthLabel:"));
    widthValue = new QLabel;
    heightLabel = new QLabel(tr("heightLabel:"));
    heightValue = new QLabel;
    rectLabel = new QLabel(tr("rectLabel:"));
    rectValue = new QLabel;
    sizeLabel = new QLabel(tr("sizeLabel:"));
    sizeValue = new QLabel;
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(xLabel,0,0);
    mainLayout->addWidget(xValue,0,1);
    mainLayout->addWidget(yLabel,1,0);
    mainLayout->addWidget(yValue,1,1);
    mainLayout->addWidget(frameLabel,2,0);
    mainLayout->addWidget(frameValue,2,1);
    mainLayout->addWidget(posLabel,3,0);
    mainLayout->addWidget(posValue,3,1);
    mainLayout->addWidget(geometryLabel,4,0);
    mainLayout->addWidget(geometryValue,4,1);
    mainLayout->addWidget(widthLabel,5,0);
    mainLayout->addWidget(widthValue,5,1);
    mainLayout->addWidget(heightLabel,6,0);
    mainLayout->addWidget(heightValue,6,1);
    mainLayout->addWidget(rectLabel,7,0);
    mainLayout->addWidget(rectValue,7,1);
    mainLayout->addWidget(sizeLabel,8,0);
    mainLayout->addWidget(sizeValue,8,1);
    updateLabel();
}

Geometry::~Geometry()
{

}

void Geometry::updateLabel()
{
    QString xStr;                       //获得x()函数显示结果
    xValue->setText(xStr.setNum(x()));
    QString yStr;
    yValue->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());
    frameValue->setText(frameStr);
    //整个窗体的左上标点
    QString posStr;
    QString tempStr11,tempStr12;
    posStr = tempStr11.setNum(pos().x())+","
            +tempStr12.setNum(pos().y());
    posValue->setText(posStr);
    //窗体内中央区域
    QString geometryStr;
    QString tempStr21,tempStr22,tempStr23,tempStr24;
    geometryStr = tempStr21.setNum(geometry().x())+","
            +tempStr22.setNum(geometry().y())+","
            +tempStr23.setNum(geometry().width())+","
            +tempStr24.setNum(geometry().height());
    geometryValue->setText(geometryStr);
    QString wStr,hStr;
    widthValue->setText(wStr.setNum(width()));
    heightValue->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());
    rectValue->setText(rectStr);
    QString sizeStr;
    QString tempStr41,tempStr42;
    sizeStr = tempStr41.setNum(size().width())+","
            +tempStr42.setNum(size().height());
    sizeValue->setText(sizeStr);
}

void Geometry::moveEvent(QMoveEvent *)//QMoveEvent 类包含移动事件的事件参数。
{
    updateLabel();
}

void Geometry::resizeEvent(QResizeEvent *)//QResizeEvent 类包含调整大小事件的事件参数。
{
    updateLabel();
}

4.运行结果

第六章 Qt5 图形与图片——位置相关函数_第2张图片第六章 Qt5 图形与图片——位置相关函数_第3张图片

你可能感兴趣的:(Qt5开发及实例,qt,开发语言)