init study Qt
This commit is contained in:
73
.gitignore
vendored
Normal file
73
.gitignore
vendored
Normal file
@@ -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
|
||||||
|
|
||||||
11
main.cpp
Normal file
11
main.cpp
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#include "widget.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
Widget w;
|
||||||
|
w.show();
|
||||||
|
return a.exec();
|
||||||
|
}
|
||||||
23
qperson.cpp
Normal file
23
qperson.cpp
Normal file
@@ -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);
|
||||||
|
}
|
||||||
33
qperson.h
Normal file
33
qperson.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef QPERSION_H
|
||||||
|
#define QPERSION_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
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
|
||||||
36
studyQt2.pro
Normal file
36
studyQt2.pro
Normal file
@@ -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
|
||||||
3
studyQt2_zh_CN.ts
Normal file
3
studyQt2_zh_CN.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!DOCTYPE TS>
|
||||||
|
<TS version="2.1" language="studyQt2_zh_CN"></TS>
|
||||||
100
widget.cpp
Normal file
100
widget.cpp
Normal file
@@ -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<QPerson*>(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<QSpinBox*>(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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
34
widget.h
Normal file
34
widget.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef WIDGET_H
|
||||||
|
#define WIDGET_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
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
|
||||||
104
widget.ui
Normal file
104
widget.ui
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>Widget</class>
|
||||||
|
<widget class="QWidget" name="Widget">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>600</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>Widget</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
|
<item row="0" column="3">
|
||||||
|
<widget class="QPushButton" name="btnBoyInc">
|
||||||
|
<property name="text">
|
||||||
|
<string>boy长大一岁</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="4">
|
||||||
|
<widget class="QPushButton" name="btnClear">
|
||||||
|
<property name="text">
|
||||||
|
<string>清空文本框</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="0" colspan="5">
|
||||||
|
<widget class="QTextEdit" name="textEdit"/>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="0">
|
||||||
|
<widget class="QLabel" name="label">
|
||||||
|
<property name="text">
|
||||||
|
<string>设置男生年龄</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>spinBoy</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="3">
|
||||||
|
<widget class="QPushButton" name="btnGirlInc">
|
||||||
|
<property name="text">
|
||||||
|
<string>girl长大一岁</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0">
|
||||||
|
<widget class="QLabel" name="label_2">
|
||||||
|
<property name="text">
|
||||||
|
<string>设置女生年龄</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>spinGirl</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="4">
|
||||||
|
<widget class="QPushButton" name="btnClassInfo">
|
||||||
|
<property name="text">
|
||||||
|
<string>类的元对象信息</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="1">
|
||||||
|
<widget class="QSpinBox" name="spinBoy"/>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="1">
|
||||||
|
<widget class="QSpinBox" name="spinGirl"/>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="2">
|
||||||
|
<spacer name="horizontalSpacer">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>40</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="2">
|
||||||
|
<spacer name="horizontalSpacer_2">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>40</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
Reference in New Issue
Block a user