From 98bb2cf0f1491d699d35827dc54b35f4e9c7c52c Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 26 Jun 2023 19:00:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8Bqt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day10/qtdemo0/main.cpp | 17 +++++++++++ day10/qtdemo0/qtdemo0.pro | 28 ++++++++++++++++++ day10/qtdemo0/widget.cpp | 41 +++++++++++++++++++++++++ day10/qtdemo0/widget.h | 14 +++++++++ day10/sumWidget/main.cpp | 11 +++++++ day10/sumWidget/sumWidget.pro | 28 ++++++++++++++++++ day10/sumWidget/widget.cpp | 56 +++++++++++++++++++++++++++++++++++ day10/sumWidget/widget.h | 27 +++++++++++++++++ 8 files changed, 222 insertions(+) create mode 100755 day10/qtdemo0/main.cpp create mode 100755 day10/qtdemo0/qtdemo0.pro create mode 100755 day10/qtdemo0/widget.cpp create mode 100755 day10/qtdemo0/widget.h create mode 100755 day10/sumWidget/main.cpp create mode 100755 day10/sumWidget/sumWidget.pro create mode 100755 day10/sumWidget/widget.cpp create mode 100755 day10/sumWidget/widget.h diff --git a/day10/qtdemo0/main.cpp b/day10/qtdemo0/main.cpp new file mode 100755 index 0000000..f96d218 --- /dev/null +++ b/day10/qtdemo0/main.cpp @@ -0,0 +1,17 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + // 1. Qt 应用程序 + QApplication a(argc, argv); + + // 2. 应用程序中显示的主窗口 + Widget w; + w.show(); + + // 3. 启动 Qt 程序, 监听窗口的相关事件 + // 直到窗口关闭事件发生才退出 + return a.exec(); // a.exec 的目的是保持程序活着 +} diff --git a/day10/qtdemo0/qtdemo0.pro b/day10/qtdemo0/qtdemo0.pro new file mode 100755 index 0000000..241eb24 --- /dev/null +++ b/day10/qtdemo0/qtdemo0.pro @@ -0,0 +1,28 @@ +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 \ + widget.cpp + +HEADERS += \ + widget.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/day10/qtdemo0/widget.cpp b/day10/qtdemo0/widget.cpp new file mode 100755 index 0000000..119196a --- /dev/null +++ b/day10/qtdemo0/widget.cpp @@ -0,0 +1,41 @@ +#include "widget.h" +#include +#include + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ + this->setWindowTitle("我是第一个小窗口"); + + // this -> resize(200,300); + + this->setFixedSize(QSize(480, 320)); + + // 创建一个按钮 + // c++中创建类对象的两种方式: + // 1) 类名 对象 (构造函数的) + QPushButton *btn = new QPushButton("退出"); + btn->resize(100, 50); // 设置控件的大小 + btn->setParent(this); // 设置控件在哪一个窗口显示 + + btn->move(190, 135); + + // connect(btn,&QPushButton::clicked,this,&QApplication::quit); + connect(btn, &QPushButton::clicked, this, &QWidget::close); + + QPushButton *btn2 = new QPushButton("2"); + btn2->resize(100, 50); // 设置控件的大小 + btn2->setParent(this); // 设置控件在哪一个窗口显示 + + btn2->move(190 + 100, 135); + + QPushButton *btn3 = new QPushButton("3"); + btn3->resize(100, 50); // 设置控件的大小 + btn3->setParent(this); // 设置控件在哪一个窗口显示 + + btn3->move(190 + 200, 135); +} + +Widget::~Widget() +{ +} diff --git a/day10/qtdemo0/widget.h b/day10/qtdemo0/widget.h new file mode 100755 index 0000000..041ff0d --- /dev/null +++ b/day10/qtdemo0/widget.h @@ -0,0 +1,14 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); +}; +#endif // WIDGET_H diff --git a/day10/sumWidget/main.cpp b/day10/sumWidget/main.cpp new file mode 100755 index 0000000..c3efeb4 --- /dev/null +++ b/day10/sumWidget/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/day10/sumWidget/sumWidget.pro b/day10/sumWidget/sumWidget.pro new file mode 100755 index 0000000..241eb24 --- /dev/null +++ b/day10/sumWidget/sumWidget.pro @@ -0,0 +1,28 @@ +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 \ + widget.cpp + +HEADERS += \ + widget.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/day10/sumWidget/widget.cpp b/day10/sumWidget/widget.cpp new file mode 100755 index 0000000..d369a92 --- /dev/null +++ b/day10/sumWidget/widget.cpp @@ -0,0 +1,56 @@ +#include "widget.h" +#include + + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ + setWindowTitle("加法计数器"); + + setFixedSize(500,100); + + QPushButton *sumBtn=new QPushButton("计算",this); + sumBtn->setFixedSize(100,50); + sumBtn->move(300,25); + + le1 = new QLineEdit(this); + le2 = new QLineEdit(this); + le1->setFixedSize(100,50); + le2->setFixedSize(100,50); + le1->move(0,25); + le2->move(200,25); + + lable1 = new QLabel(this); + lable1->setText("+"); + lable1->setFixedSize(10,50); + lable1->move(145,25); + + lable2 = new QLabel(this); + lable2->setText("计算结果"); + lable2->setFixedSize(100,50); + lable2->move(400,25); + + connect(sumBtn,&QPushButton::clicked,this,&Widget::sum); + + +// connect(sumBtn,&QPushButton::clicked,this,lable2->setText(QString::number(c))); + +} + +void Widget::sum(){ + lable2->setText("点击"); + QString n1 = le1->text(); + QString n2 = le2->text(); + qDebug() << n1 << " " << n2; + + // 将文本转化为数值 + int ret = n1.toInt()+n2.toInt(); + QString str = QString::number(ret); + qDebug() << str; + lable2->setText(str); +} + +Widget::~Widget() +{ +} + diff --git a/day10/sumWidget/widget.h b/day10/sumWidget/widget.h new file mode 100755 index 0000000..e9743ae --- /dev/null +++ b/day10/sumWidget/widget.h @@ -0,0 +1,27 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include +#include +#include // 按钮 +#include // 文本输入框 +#include // 文本标签 + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +public slots: + // 自定义槽函数, 需要在源文件中去实现函数功能 + void sum(); + +private: + QLineEdit *le1,*le2; + QLabel *lable1,*lable2; + +}; +#endif // WIDGET_H