• [Qt]《开发指南》3.1源码分析


     界面:

     

    ButterflyGraph:

    可以看出,本工程在主程序main里调用窗口界面qmywidget,窗口界面继承了QWidget,并调用自定义类qperson,推测是qmywidget类中的一个属性

     

    qmywidget的实现,调用了显示界面ui_qmywidget,包含了元对象QMetaProperty

     UML图:

    QmyWidget中,on开头的都是槽函数,QmyWidget是主窗口,所以不发射信号,只接收信号

    main.cpp

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

    qperson.h

     1 #ifndef QPERSON_H
     2 #define QPERSON_H
     3 
     4 #include <QObject>
     5 
     6 class QPerson : public QObject
     7 {
     8     Q_OBJECT
     9 
    10     Q_CLASSINFO("author","Wang")
    11     Q_CLASSINFO("company","UPC")
    12     Q_CLASSINFO("version","1.0.0")
    13 
    14     Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged)
    15     Q_PROPERTY(QString name MEMBER m_name)
    16     Q_PROPERTY(int score MEMBER m_score)
    17 private:
    18     int  m_age=10;
    19     QString m_name;
    20     int     m_score=79;
    21 public:
    22     explicit QPerson(QString fName, QObject *parent = nullptr);
    23 
    24     int     age();
    25     void    setAge(int value);
    26 
    27     void    incAge();
    28 signals:
    29     void    ageChanged( int  value);
    30 
    31 public slots:
    32 };
    33 
    34 #endif // QPERSON_H

    qmywidget.cpp

      1 #include "qmywidget.h"
      2 #include "ui_qmywidget.h"
      3 #include    <QMetaProperty>
      4 
      5 QmyWidget::QmyWidget(QWidget *parent) :
      6     QWidget(parent),
      7     ui(new Ui::QmyWidget)
      8 {//构造函数
      9     ui->setupUi(this);
     10 
     11     boy=new QPerson("王小明");
     12     boy->setProperty("score",95);
     13     boy->setProperty("age",10);
     14     boy->setProperty("sex","Boy");//动态属性
     15 //    connect(boy,SIGNAL(ageChanged(int)),this,SLOT(on_ageChanged(int)));
     16     connect(boy,&QPerson::ageChanged,this,&QmyWidget::on_ageChanged);
     17 
     18     girl=new QPerson("张小丽");
     19     girl->setProperty("score",81);
     20     girl->setProperty("age",20);
     21     girl->setProperty("sex","Girl");//动态属性
     22     connect(girl,&QPerson::ageChanged,this,&QmyWidget::on_ageChanged);
     23 
     24     ui->spinBoy->setProperty("isBoy",true); //动态属性
     25     ui->spinGirl->setProperty("isBoy",false);
     26 
     27 //  不能使用此形式,因为QSpinBox有两种参数形式的valueChanged()信号
     28 //    connect(ui->spinGirl,&QSpinBox::valueChanged,
     29 //            this,&QmyWidget::on_spinBoy_valueChanged);
     30     connect(ui->spinGirl,SIGNAL(valueChanged(int)),
     31             this,SLOT(on_spin_valueChanged(int)));
     32     connect(ui->spinBoy,SIGNAL(valueChanged(int)),
     33             this,SLOT(on_spin_valueChanged(int)));
     34 }
     35 
     36 QmyWidget::~QmyWidget()
     37 {
     38     delete ui;
     39 }
     40 
     41 void QmyWidget::on_ageChanged( int value)
     42 {//响应QPerson的ageChanged()信号
     43     Q_UNUSED(value);
     44     QPerson *aPerson = qobject_cast<QPerson *>(sender()); //类型投射
     45     QString hisName=aPerson->property("name").toString(); //姓名
     46 //    QString hisName=aPerson->name(); //获取姓名,错误
     47     QString hisSex=aPerson->property("sex").toString(); //动态属性
     48     int hisAge=aPerson->age();//通过接口函数获取年龄
     49 //    int hisAge=aPerson->property("age").toInt();//通过属性获得年龄
     50 
     51     ui->textEdit->appendPlainText(hisName+","+hisSex
     52                                +QString::asprintf(",年龄=%d",hisAge));
     53 }
     54 
     55 void QmyWidget::on_btnClear_clicked()
     56 {//"清空文本框"按钮
     57     ui->textEdit->clear();
     58 }
     59 
     60 void QmyWidget::on_btnBoyInc_clicked()
     61 {//"boy长大一岁"按钮
     62     boy->incAge();
     63 }
     64 
     65 void QmyWidget::on_btnGirlInc_clicked()
     66 {//"girl长大一岁"按钮
     67     girl->incAge();
     68 }
     69 
     70 void QmyWidget::on_spin_valueChanged(int arg1)
     71 {//响应界面上spinBox的valueChanged(int)信号
     72     Q_UNUSED(arg1);
     73     QSpinBox *spinBox = qobject_cast<QSpinBox *>(sender());
     74     if (spinBox->property("isBoy").toBool())
     75         boy->setAge(spinBox->value());
     76     else
     77         girl->setAge(spinBox->value());
     78 }
     79 
     80 void QmyWidget::on_btnClassInfo_clicked()
     81 {//"类的元对象信息"按钮
     82     const QMetaObject *meta=boy->metaObject();
     83 //    const QMetaObject *meta=girl->metaObject();
     84 //    const QMetaObject *meta=ui->spinBoy->metaObject();
     85     ui->textEdit->clear();
     86 
     87     ui->textEdit->appendPlainText("==元对象信息==
    ");
     88     ui->textEdit->appendPlainText(QString("类名称:%1
    ").arg(meta->className()));
     89 
     90     ui->textEdit->appendPlainText("property");
     91     for (int i=meta->propertyOffset();i<meta->propertyCount();i++)
     92     {
     93         const char* propName=meta->property(i).name();
     94         ui->textEdit->appendPlainText(
     95            QString("属性名称=%1,属性值=%2").arg(propName).arg(boy->property(propName).toString()));
     96     }
     97 
     98     ui->textEdit->appendPlainText("");
     99     ui->textEdit->appendPlainText("classInfo");
    100     for (int i=meta->classInfoOffset();i<meta->classInfoCount();++i)
    101     {
    102        QMetaClassInfo classInfo=meta->classInfo(i);
    103         ui->textEdit->appendPlainText(
    104            QString("Name=%1; Value=%2").arg(classInfo.name()).arg(classInfo.value()));
    105     }
    106 
    107 }

    qperson.cpp

     1 #include "qperson.h"
     2 
     3 QPerson::QPerson(QString fName,QObject *parent) : QObject(parent)
     4 { //构造函数
     5     m_name=fName;
     6 }
     7 
     8 int QPerson::age()
     9 { //返回age
    10     return  m_age;
    11 }
    12 
    13 void QPerson::setAge(int value)
    14 {//设置age
    15     m_age=value;
    16     emit ageChanged(m_age); //发射信号
    17 }
    18 
    19 void QPerson::incAge()
    20 {
    21     m_age++;
    22     emit ageChanged(m_age);//发射信号
    23 }
  • 相关阅读:
    去除phpcms会员登录后头部登陆条的会员名称的括号
    图片左右间隔滚动Jquery特效
    JS判断字符串长度的5个方法
    HTML中&nbsp; &ensp; &emsp; &thinsp;等6种空白空格的区别
    Chrome firefox ie等浏览器空格&nbsp;宽度不一样怎么办
    jQuery延迟加载插件(Lazy Load)详解
    jquery复选框 选中事件 及其判断是否被选中
    手机网页Html代码实现(解决显示页面很小的问题)
    iOS下的按钮css去除原生样式
    用纯css改变下拉列表select框的默认样式(不兼容IE10以下)
  • 原文地址:https://www.cnblogs.com/cxc1357/p/11809584.html
走看看 - 开发者的网上家园