一、新建GUI應用,項目名稱"Geometry",基類選擇"QDialog",類名爲"Geometry",取消"建立界面"。函數
main.cpp不變:this
#include "geometry.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Geometry w; w.show(); return a.exec(); }
二、Geometry類繼承自QDialog類,在頭文件中聲明須要的控件[QLabel]以及所須要的函數code
#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; 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 // GEOMETRY_H
三、在構造函數中完成控件的建立以及初始化工做
四、updateLabel()函數完成得到各位置函數的信息並顯示功能
五、重定義QWidget的moveEvent()函數,響應對話框的移動事件,使得窗體在被移動時可以同步更新個函數的顯示結果。
六、重定義QWidget的resizeEvent()函數,響應對話框的大小調整事件,使得窗體大小改變時可以同步更新個函數的顯示結果。blog
#include "geometry.h" Geometry::Geometry(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(); } Geometry::~Geometry() { } 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()*/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 Geometry:: moveEvent(QMoveEvent *) { updateLabel(); } void Geometry::resizeEvent(QResizeEvent *) { updateLabel(); }