• Qt5开发从入门到精通——第六篇一节( 图像与图片——位置相关函数 )


    欢迎小伙伴的点评✨✨,相互学习、互关必回、全天在线🍳🍳🍳
    博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀


    前言

    本章首先介绍几种主要位置函数及其之间的区别,以及各种与位置相关函数的使用场合;然后,通过一个简单绘图工具实例,介绍利用 QPainter 和 QPainterPath 两种方法绘制各种基础图形。


    一、位置相关函数区别概述

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

    • x() 、 y()和 pos()函数的作用都是获得整个窗体左上角的坐标位置。
    • frameGeometry()函数与 geometry() 函数相对应。 frameGeometry()函数获得的是整个窗体的左上顶点和长、宽值,而 geometry()函数获得的是窗体内中央区域的左上顶点坐标及长、宽值。
    • 直接调用 width()和 height()函数获得的是中央区域的长、宽值。
    • rect() 、 size()函数获得的结果也都是对于窗体的中央区域而言的。 size()函数获得的是窗体中央区域的长、宽值。 rect()函数与 geometry()函数相同,返回一个 QRect 对象,这两个函数获得的长、宽值是相同的,都是窗体中央区域的长、宽值,只是左上顶点的坐标值不一样。 geometry()函数获得的左上顶点坐标是相对于父窗体而言的坐标,而 rect()函数获得的左上顶点坐标始终为(0,0) 。
      在实际应用中,需要根据情况使用正确的位置信息函数以获得准确的位置尺寸信息,尤其是在编写对位置精度要求较高的程序(如地图浏览程序)时,更应注意函数的选择,以避免产生不必要的误差。

    二、效果实例

    图一
    在这里插入图片描述

    三、原码解析

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include 
    #include 
    #include 
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        Dialog(QWidget *parent = 0);
        ~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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    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("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 ();
    }
    
    Dialog::~Dialog()
    {
    
    }
    
    
    void Dialog::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 tempStrl1,tempStrl2;
        positionStr =tempStrl1. setNum (pos () .x ()) +", "+
        tempStrl2. 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()*/width()) +", "+
        tempStr34.setNum(height()/*rect().height()*/);
        rectValueLabel->setText(rectStr);
        QString sizeStr;     //获得size()函数的结果并显示
        QString tempStr41,tempStr42;
        sizeStr=tempStr41.setNum (size() .width()) +","+tempStr42.setNum(size().height());
        sizeValueLabel->setText(sizeStr);
    
    
    
    
    }
    
    void Dialog::moveEvent(QMoveEvent *)  
    //重新定义 QWidget 的 moveEventO函数,响应对话框的移动事件,使得窗体在被移动时能够同步更新各函数的显示结果
    
    {
        updateLabel();
    }
    
    void Dialog::resizeEvent(QResizeEvent *)
    //重新定义 QWidget 的 resizeEventQ函数,响应对话框的大小调整事件,使得在窗体大小发生改变时,也能够同步更新各函数的显示结果
    {
        updateLabel();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    main.cpp

    #include "dialog.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    四、总结

    图像与图片的位置相关函数会在应用程序开发中经常用到的

  • 相关阅读:
    招投标系统软件源码,招投标全流程在线化管理
    SpringMVC06-SpringMVC的视图
    zemax埃尔弗目镜
    Lombok包依赖的注入
    转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
    天翎知识管理系统有哪些优势?
    HOOPS Visualize 2023 SP2 U1 Crack-HOOPS Visualize
    ubuntu中检查端口号的命令
    【虚幻引擎UE】UE4/UE5 基于2D屏幕坐标获取场景3D坐标 射线检测(蓝图/C++)
    闵帆老师《论文写作》课学习心得
  • 原文地址:https://blog.csdn.net/weixin_44759598/article/details/126773170