From c3e374c375a7924678df71008b20fbd92cf8c79a Mon Sep 17 00:00:00 2001 From: cs180 Date: Sun, 24 Aug 2025 11:10:27 +0800 Subject: [PATCH] init study Qt --- .gitignore | 73 ++++++++++++++++++++++++++++++++ main.cpp | 11 +++++ qperson.cpp | 23 ++++++++++ qperson.h | 33 +++++++++++++++ studyQt2.pro | 36 ++++++++++++++++ studyQt2_zh_CN.ts | 3 ++ widget.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++ widget.h | 34 +++++++++++++++ widget.ui | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 417 insertions(+) create mode 100644 .gitignore create mode 100644 main.cpp create mode 100644 qperson.cpp create mode 100644 qperson.h create mode 100644 studyQt2.pro create mode 100644 studyQt2_zh_CN.ts create mode 100644 widget.cpp create mode 100644 widget.h create mode 100644 widget.ui diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b0a4ec2 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/qperson.cpp b/qperson.cpp new file mode 100644 index 0000000..7dc9d89 --- /dev/null +++ b/qperson.cpp @@ -0,0 +1,23 @@ +#include "qperson.h" + +QPerson::QPerson(QString fName, QObject *parent) : QObject(parent), m_name(fName) +{ + +} + +int QPerson::age() +{ + return m_age; +} + +void QPerson::setAge(int val) +{ + m_age = val; + emit ageChanged(m_age); +} + +void QPerson::incAge() +{ + m_age++; + emit ageChanged(m_age); +} diff --git a/qperson.h b/qperson.h new file mode 100644 index 0000000..feeb29a --- /dev/null +++ b/qperson.h @@ -0,0 +1,33 @@ +#ifndef QPERSION_H +#define QPERSION_H + +#include + +class QPerson : public QObject +{ + Q_OBJECT + Q_CLASSINFO("author", "Ding") + Q_CLASSINFO("company", "UPC") + Q_CLASSINFO("version", "1.0.0") + Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged) + Q_PROPERTY(QString name MEMBER m_name) + Q_PROPERTY(int score MEMBER m_score) + +private: + int m_age=10; + QString m_name; + int m_score=60; + +public: + explicit QPerson(QString fName, QObject *parent = nullptr); + int age(); + void setAge(int val); + void incAge(); + +signals: + void ageChanged(int val); + +public slots: +}; + +#endif // QPERSON_H diff --git a/studyQt2.pro b/studyQt2.pro new file mode 100644 index 0000000..49fb485 --- /dev/null +++ b/studyQt2.pro @@ -0,0 +1,36 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + qperson.cpp \ + widget.cpp + +HEADERS += \ + qperson.h \ + widget.h + +FORMS += \ + widget.ui + +TRANSLATIONS += \ + studyQt2_zh_CN.ts + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/studyQt2_zh_CN.ts b/studyQt2_zh_CN.ts new file mode 100644 index 0000000..5908e27 --- /dev/null +++ b/studyQt2_zh_CN.ts @@ -0,0 +1,3 @@ + + + diff --git a/widget.cpp b/widget.cpp new file mode 100644 index 0000000..231782e --- /dev/null +++ b/widget.cpp @@ -0,0 +1,100 @@ +#include "widget.h" +#include "ui_widget.h" +#include "qperson.h" +#include "QMetaProperty" + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); + + boy = new QPerson("南街"); + boy->setProperty("score", 99); + boy->setProperty("age", 10); + boy->setProperty("sex", "Boy"); //动态属性 + ui->spinBoy->setValue(10); + connect(boy, &QPerson::ageChanged, this, &Widget::on_ageChanged); + + girl = new QPerson("小丽"); + girl->setProperty("score", 66); + girl->setProperty("age", 6); + girl->setProperty("sex", "Girl"); //动态属性 + ui->spinGirl->setValue(6); + connect(girl, &QPerson::ageChanged, this, &Widget::on_ageChanged); + + ui->spinBoy->setProperty("isBoy", true); // ui动态属性 + ui->spinGirl->setProperty("isBoy", false); + + connect(ui->spinBoy, SIGNAL(valueChanged(int)), + this, SLOT(on_spin_valueChanged(int))); + connect(ui->spinGirl, SIGNAL(valueChanged(int)), + this, SLOT(on_spin_valueChanged(int))); +} + +Widget::~Widget() +{ + delete ui; +} + +void Widget::on_ageChanged(int val) +{ + Q_UNUSED(val) + QPerson *aPerson = qobject_cast(sender()); + QString hisName = aPerson->property("name").toString(); + QString hisSex = aPerson->property("sex").toString(); + int hisAge = aPerson->age(); + hisAge = aPerson->property("age").toInt(); + ui->textEdit->append(hisName + ", " + hisSex + QString::asprintf(", 年龄=%d", hisAge)); +} +void Widget::on_spin_valueChanged(int arg1) +{ + Q_UNUSED(arg1) + auto spinBox = qobject_cast(sender()); + if(spinBox->property("isBoy").toBool()) + { + boy->setAge(arg1); + } + else + { + girl->setAge(spinBox->value()); + } +} +void Widget::on_btnClear_clicked() +{ + ui->textEdit->clear(); +} +void Widget::on_btnBoyInc_clicked() +{ + int val = ui->spinBoy->value(); + ui->spinBoy->setValue(++val); +} +void Widget::on_btnGirlInc_clicked() +{ + int val = ui->spinGirl->value(); + ui->spinGirl->setValue(++val); +} +void Widget::on_btnClassInfo_clicked() +{ + const QMetaObject* meta=boy->metaObject(); + ui->textEdit->clear(); + ui->textEdit->append("==元对象信息==\n"); + ui->textEdit->append( + QString("类名: %1\n").arg(meta->className())); + ui->textEdit->append("properties:\n"); + for (int i= meta->propertyOffset(); i < meta->propertyCount(); ++i) + { + QMetaProperty prop = meta->property(i); + auto propName = prop.name(); + auto propVal = boy->property(propName).toString(); + ui->textEdit->append( + QString("属性名=%1,属性值=%2").arg(propName).arg(propVal)); + } + ui->textEdit->append(""); + ui->textEdit->append("classInfo"); + for (int i= meta->classInfoOffset(); i< meta->classInfoCount(); ++i) + { + QMetaClassInfo classInfo = meta->classInfo(i); + ui->textEdit->append(QString("Name=%1; Value=%2").arg(classInfo.name()).arg(classInfo.value())); + } +} diff --git a/widget.h b/widget.h new file mode 100644 index 0000000..54307dd --- /dev/null +++ b/widget.h @@ -0,0 +1,34 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +class QPerson; + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT + +public: + explicit Widget(QWidget *parent = nullptr); + ~Widget(); + +private: + Ui::Widget *ui; + QPerson * boy; + QPerson *girl; + +private slots: + void on_ageChanged(int val); + void on_spin_valueChanged(int arg1); + + void on_btnClear_clicked(); + void on_btnBoyInc_clicked(); + void on_btnGirlInc_clicked(); + void on_btnClassInfo_clicked(); +}; +#endif // WIDGET_H diff --git a/widget.ui b/widget.ui new file mode 100644 index 0000000..18dbb53 --- /dev/null +++ b/widget.ui @@ -0,0 +1,104 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + Widget + + + + + + boy长大一岁 + + + + + + + 清空文本框 + + + + + + + + + + 设置男生年龄 + + + spinBoy + + + + + + + girl长大一岁 + + + + + + + 设置女生年龄 + + + spinGirl + + + + + + + 类的元对象信息 + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + +