diff --git a/qtdemo01/.gitignore b/qtdemo01/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/qtdemo01/.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/qtdemo01/main.cpp b/qtdemo01/main.cpp new file mode 100644 index 0000000..94c19db --- /dev/null +++ b/qtdemo01/main.cpp @@ -0,0 +1,35 @@ +#include "widget.h" +#include "mywidget2.h" +#include "widget3.h" + +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + // Widget w; + + // 设置窗口的标题、初始大小、固定的最大宽和高 + // w.setWindowTitle("标题"); + // w.resize(1200,900); + // w.setFixedSize(600,400); + + // 获取主屏幕 + // QScreen *desktop = QApplication::primaryScreen(); + // QRect screenGeometry = desktop->geometry(); + // int screenWidth = screenGeometry.width(); + // int screenHeight = screenGeometry.height(); + // // 获取屏幕信息 + // qDebug() << "屏幕宽度:" << screenWidth; + // qDebug() << "屏幕高度:" << screenHeight; + + +// MyWidget2 w2; // 修改为新增的 MyWidget2 类 + + Widget3 w3; + + w3.show(); + return a.exec(); +} diff --git a/qtdemo01/mywidget2.cpp b/qtdemo01/mywidget2.cpp new file mode 100644 index 0000000..b84e7cd --- /dev/null +++ b/qtdemo01/mywidget2.cpp @@ -0,0 +1,26 @@ +#include "mywidget2.h" + +MyWidget2::MyWidget2(QWidget *parent) : QWidget(parent) +{ + // 设置窗口的大小 + resize(800,640); + setWindowTitle("第二个窗口"); + + mBtn = new QPushButton("提取输入的内容",this); + // mEdit = new QLineEdit(this); + mEdit = new QLineEdit; + mEdit->setParent(this); + + mEdit->setFixedSize(200,50); + mEdit->move(10,10); + mBtn->setFixedSize(200,100); + mBtn->move(10,70); + + connect(mBtn,&QPushButton::clicked,this,&MyWidget2::getText); +} + +void MyWidget2::getText() +{ + // 绑定 + qDebug()<text(); +} diff --git a/qtdemo01/mywidget2.h b/qtdemo01/mywidget2.h new file mode 100644 index 0000000..81643c6 --- /dev/null +++ b/qtdemo01/mywidget2.h @@ -0,0 +1,27 @@ +#ifndef MYWIDGET2_H +#define MYWIDGET2_H + +#include +#include +#include // 单行输入框 +#include + +class MyWidget2 : public QWidget +{ + Q_OBJECT +public: + explicit MyWidget2(QWidget *parent = nullptr); + +public: + void getText(); // 自定义成员函数 + +private: + QPushButton *mBtn; + QLineEdit *mEdit; + +signals: + + +}; + +#endif // MYWIDGET2_H diff --git a/qtdemo01/qtdemo01.pro b/qtdemo01/qtdemo01.pro new file mode 100644 index 0000000..f897711 --- /dev/null +++ b/qtdemo01/qtdemo01.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 \ + mywidget2.cpp \ + student.cpp \ + teacher.cpp \ + widget.cpp \ + widget3.cpp + +HEADERS += \ + mywidget2.h \ + student.h \ + teacher.h \ + widget.h \ + widget3.h + +# 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/qtdemo01/student.cpp b/qtdemo01/student.cpp new file mode 100644 index 0000000..afff043 --- /dev/null +++ b/qtdemo01/student.cpp @@ -0,0 +1,6 @@ +#include "student.h" + +Student::Student(QObject *parent) : QObject(parent) +{ + +} diff --git a/qtdemo01/student.h b/qtdemo01/student.h new file mode 100644 index 0000000..8ec59d6 --- /dev/null +++ b/qtdemo01/student.h @@ -0,0 +1,18 @@ +#ifndef STUDENT_H +#define STUDENT_H + +#include + +class Student : public QObject +{ + Q_OBJECT +public: + explicit Student(QObject *parent = nullptr); + +signals: + void sleep(); // 信号(函数)不需要实现 + void sleep(const QString &msg,int n); + +}; + +#endif // STUDENT_H diff --git a/qtdemo01/teacher.cpp b/qtdemo01/teacher.cpp new file mode 100644 index 0000000..71f9ed2 --- /dev/null +++ b/qtdemo01/teacher.cpp @@ -0,0 +1,15 @@ +#include "teacher.h" + +Teacher::Teacher(QObject *parent) : QObject(parent) +{ + +} + +void Teacher::wake_up(){ + qDebug() << "请睡觉的学生站起来,出去跑两圈"; +} + +void Teacher::wake_up(const QString &msg) +{ + qDebug() << "学生因为 "< +#include + +class Teacher : public QObject +{ + Q_OBJECT +public: + explicit Teacher(QObject *parent = nullptr); + +public slots: + void wake_up(); + void wake_up(const QString &msg); // 信号带参数,处理的槽函数也要有参数,且类型保持一致 + +}; + +#endif // TEACHER_H diff --git a/qtdemo01/widget.cpp b/qtdemo01/widget.cpp new file mode 100644 index 0000000..2a2afa5 --- /dev/null +++ b/qtdemo01/widget.cpp @@ -0,0 +1,95 @@ +#include "widget.h" + +// : Qwidget(parent) 初始化列表,调用父类构造函数 +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ + // 初始化设置窗口的标题、大小和固定 + // setWindowTitle("按钮板"); + // // resize(400,300); + // setFixedSize(400, 190); + + // 添加按钮 + // 不要将对象存在栈区,应该在堆区创建 + // QPushButton *btn = new QPushButton("点我啊",this); + // QPushButton *btn = new QPushButton; + // btn->setParent(this); + // btn->setText("点一下"); + // btn->setFixedSize(200, 100); + // btn->move(100, 50); // 向右移动 100, 向下移动 50 + + // 创建 9 个按钮,三行三列,每个按钮的大小为 120,50 + // 每个按钮之间的间隔为 10 + // int w = 120, h = 50, space = 10; + // QVector vbs; // 使用指针类型存储按钮 + // for (int i = 0; i < 3; i++) + // { // 修改行范围 + // for (int j = 0; j < 3; j++) + // { // 修改列范围 + // int btnNumber = i + j * 3 + 1; + // // QPushButton *btn = new QPushButton(QString::number(btnNumber), this); + // QPushButton *btn = new QPushButton; + // btn->setText(QString::number(btnNumber)); + // btn->setParent(this); + // btn->setFixedSize(w, h); + // btn->move((space + w) * i + space, (space + h) * j + space); + // vbs.append(btn); // 将按钮添加到向量中 + // } + // } + + // 方法二 + // int w=120, h =50,space=10; + // QVector vbs; + // for(int i=0;i <9; i++){ + // // QString::number(int) 将整数转化为QString字符串 + // QPushButton *btn=new QPushButton(QString::number(i+1), this); + // btn->setFixedSize(w,h); + + // // 第一行 + // int row = i/3; + // int col= i % 3; + // btn->move((space+w)*col+space, + // (space+h)*row+space); + // vbs.append(btn); // 将按钮添加到向量中 + // } + + + resize(800,640); + setMaximumSize(900,700); // 窗口最大尺寸 + move(0,0); + + QPushButton *btn = new QPushButton("关闭",this); + btn->setFixedSize(120,50); + btn->move(10,10); + + // 当前类对象 QPushButton 的点击事件感兴趣 + // 使用 connect() 进行绑定到当前窗口的 close() + // 发送者和接受者都是 QObject 类对象的指针 + // Qt5 的信号绑定槽函数的方式 + connect(btn,&QPushButton::clicked,this,&Widget::close); + + + QPushButton *maxBtn = new QPushButton("最大化",this); + maxBtn->setFixedSize(120,50); + maxBtn->move(10,70); + + // 绑定的槽函数是自定义的 + connect(maxBtn,&QPushButton::clicked,this,&Widget::toggleShow); +} + +void Widget::toggleShow() // 自定义槽函数 +{ + // qDebug() 引入 头文件 + qDebug()<<"show or hide"<isMaximized() < +#include +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +public: + void toggleShow(); +}; +#endif // WIDGET_H diff --git a/qtdemo01/widget3.cpp b/qtdemo01/widget3.cpp new file mode 100644 index 0000000..d72d87d --- /dev/null +++ b/qtdemo01/widget3.cpp @@ -0,0 +1,66 @@ +#include "widget3.h" + +Widget3::Widget3(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("自定义信号和槽"); + resize(800,640); + + stu = new Student(); + stu->setParent(this); + + techr = new Teacher(this); + + // 绑定无参信号 + void(Student:: *sleep1)() = &Student::sleep; + void(Teacher:: *wakeup1)() = &Teacher::wake_up; + connect(stu,sleep1,techr,wakeup1); + + // 绑定有参的信号和槽函数 + void(Student:: *sleep2)(const QString &,int n) = &Student::sleep; + void(Teacher:: *wakeup2)(const QString &) = &Teacher::wake_up; + connect(stu,sleep2,techr,wakeup2); + // 手动发送一个信号 + // emit stu->sleep(); + + // 点击按钮发送学生睡觉的信号 + QPushButton *btn = new QPushButton("我睡会",this); + btn->resize(120,50); + btn->move(20,20); + + QPushButton *btn2 = new QPushButton("再睡会",this); + btn2->resize(120,50); + btn2->move(20,90); + edit = new QLineEdit(this); + edit->resize(300,50); + edit->move(20,160); + + // 将一个信号绑定到另一个信号上,即发送另一个信号 + // connect(btn,&QPushButton::clicked,stu,&Student::sleep); + // connect(btn,&QPushButton::clicked,this,&Widget3::to_sleep); + connect(btn, SIGNAL(clicked()) ,this,SLOT(to_sleep())); // QT4 的信号槽写法 + // connect(btn2,&QPushButton::clicked,this,&Widget3::to_sleep2); + + // Lambda表达式(匿名函数) + // connect(btn,&QPushButton::clicked,[&](){ + // emit stu->sleep(); + + // void(Student:: *sleep1)() = &Student::sleep; + // void(Teacher:: *wakeup1)() = &Teacher::wake_up; + // disconnect(stu,sleep1,techr,wakeup1); + // }); + connect(btn2,&QPushButton::clicked,[&](){ + const QString msg = edit->text(); + emit stu->sleep(msg,2); + }); +} + +void Widget3::to_sleep() +{ + emit stu->sleep(); // 发送一个睡觉的信号 +} + +void Widget3::to_sleep2() +{ + const QString &msg = edit->text(); + emit stu->sleep(msg,2); +} diff --git a/qtdemo01/widget3.h b/qtdemo01/widget3.h new file mode 100644 index 0000000..8e374e3 --- /dev/null +++ b/qtdemo01/widget3.h @@ -0,0 +1,29 @@ +#ifndef WIDGET3_H +#define WIDGET3_H + +#include +#include +#include +#include "student.h" +#include "teacher.h" + +class Widget3 : public QWidget +{ + Q_OBJECT +public: + explicit Widget3(QWidget *parent = nullptr); + +public slots: + void to_sleep(); + void to_sleep2(); + +private: + Student *stu; + Teacher *techr; + QLineEdit *edit; // 睡觉的原因 + +signals: + +}; + +#endif // WIDGET3_H