窗体坐标和区域大小各种位置信息

窗体坐标和区域大小各种位置信息

Qt提供了很多关于获取窗体位置以及显示区域大小的函数,包括窗体的坐在点位置、长、宽、尺寸信息等。
1、创建头文件geometry.h,声明继承于QDialog的类

#ifndef GEOMETRY_H
#define GEOMETRY_H

#include <QtGui>

class Geometry : public QDialog
{
    Q_OBJECT
public:
    Geometry();

    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLabel *label4;
    QLabel *label5;
    QLabel *label6;
    QLabel *label7;
    QLabel *label8;
    QLabel *label9;

    QLabel *xLabel;
    QLabel *yLabel;
    QLabel *frameGeoLabel;
    QLabel *posLabel;
    QLabel *geoLabel;
    QLabel *widthLabel;
    QLabel *heightLabel;
    QLabel *rectLabel;
    QLabel *sizeLabel;

    void updateLabel();

protected:
    void moveEvent(QMoveEvent *);
    void resizeEvent(QResizeEvent *);
};

#endif // GEOMETRY_H

2、创建geometry.cpp,实现函数功能

#include "geometry.h"

Geometry::Geometry()
{
    setWindowTitle(tr("Geometry"));//设置窗体标题

    label1 = new QLabel;
    label1->setText("x():");
    label2 = new QLabel;
    label2->setText("y():");
    label3 = new QLabel;
    label3->setText("frameGeometry():");
    label4 = new QLabel;
    label4->setText("pos()");
    label5 = new QLabel;
    label5->setText("geometry():");
    label6 = new QLabel;
    label6->setText("width():");
    label7 = new QLabel;
    label7->setText("height():");
    label8 = new QLabel;
    label8->setText("rect():");
    label9 = new QLabel;
    label9->setText("size():");

    xLabel = new QLabel;
    yLabel = new QLabel;
    frameGeoLabel = new QLabel;
    posLabel = new QLabel;
    geoLabel = new QLabel;
    widthLabel = new QLabel;
    heightLabel = new QLabel;
    rectLabel = new QLabel;
    sizeLabel = new QLabel;

    QGridLayout *layout = new QGridLayout(this);
    layout->setMargin(20);//布局边界空白
    layout->setSpacing(10);//布局内部组件之间空白,注意这两者之间的区别

    int l = 0;
    int c = 1;
    layout->addWidget(label1,0,l);
    layout->addWidget(xLabel,0,c);
    layout->addWidget(label2,1,l);
    layout->addWidget(yLabel,1,c);
    layout->addWidget(label3,2,l);
    layout->addWidget(frameGeoLabel,2,c);
    layout->addWidget(label4,3,l);
    layout->addWidget(posLabel,3,c);
    layout->addWidget(label5,4,l);
    layout->addWidget(geoLabel,4,c);
    layout->addWidget(label6,5,l);
    layout->addWidget(widthLabel,5,c);
    layout->addWidget(label7,6,l);
    layout->addWidget(heightLabel,6,c);
    layout->addWidget(label8,7,l);
    layout->addWidget(rectLabel,7,c);
    layout->addWidget(label9,8,l);
    layout->addWidget(sizeLabel,8,c);

    //setLayout(layout);
    updateLabel();
}

void Geometry::updateLabel()
{
    QString temp;
    QString temp1;
    QString temp2;
    QString temp3;

    QString str_x;
    xLabel->setText(str_x.setNum(x()));
    QString str_y;
    yLabel->setText(str_y.setNum(y()));
    QString frameGeo;

    frameGeo = temp.setNum(frameGeometry().x()) + ", " + temp1.setNum(frameGeometry().y()) + ", " +
                temp2.setNum(frameGeometry().width()) + ", " + temp3.setNum(frameGeometry().height());
    frameGeoLabel->setText(frameGeo);

    QString position;
    position = temp.setNum(pos().x()) + ", " + temp1.setNum(pos().y());
    posLabel->setText(position);

    QString geo;
    geo = temp.setNum(geometry().x()) + ", " + temp1.setNum(geometry().y()) + ", " +
            temp2.setNum(geometry().width()) + ", " + temp3.setNum(geometry().height());
    geoLabel->setText(geo);

    QString w;
    widthLabel->setText(w.setNum(width()));
    QString h;
    heightLabel->setText(h.setNum(height()));

    QString r;
    r = temp.setNum(rect().x()) + ", " + temp1.setNum(rect().y()) + ", " +
            temp2.setNum(rect().width()) + ", " + temp3.setNum(rect().height());
    rectLabel->setText(r);
    QString s;
    s = temp.setNum(size().width()) + ", " + temp1.setNum(size().height());
    sizeLabel->setText(s);
}

void Geometry::moveEvent(QMoveEvent *)  //重写鼠标移动事件
{
    updateLabel();
}

void Geometry::resizeEvent(QResizeEvent *)//重写窗体尺寸变化事件
{
    updateLabel();
}

3、最后创建main.cpp

#include "geometry.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    Geometry *g = new Geometry();
    g->show();
    return app.exec();
}

4、运行后

5、大家可以自己拖动一下,或者改变一下窗体尺寸大小,可以看到窗体里面显示的位置信息数据也发生了变化。自己可以查看一下这些函数,看帮助文档

6、代码资源下载

你可能感兴趣的:(qt,窗体位置,窗体尺寸)