This commit is contained in:
flykhan 2023-06-26 19:00:04 +08:00
parent 4295013896
commit 98bb2cf0f1
8 changed files with 222 additions and 0 deletions

17
day10/qtdemo0/main.cpp Executable file
View File

@ -0,0 +1,17 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
// 1. Qt 应用程序
QApplication a(argc, argv);
// 2. 应用程序中显示的主窗口
Widget w;
w.show();
// 3. 启动 Qt 程序, 监听窗口的相关事件
// 直到窗口关闭事件发生才退出
return a.exec(); // a.exec 的目的是保持程序活着
}

28
day10/qtdemo0/qtdemo0.pro Executable file
View File

@ -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

41
day10/qtdemo0/widget.cpp Executable file
View File

@ -0,0 +1,41 @@
#include "widget.h"
#include <QApplication>
#include <QPushButton>
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()
{
}

14
day10/qtdemo0/widget.h Executable file
View File

@ -0,0 +1,14 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H

11
day10/sumWidget/main.cpp Executable file
View 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();
}

28
day10/sumWidget/sumWidget.pro Executable file
View File

@ -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

56
day10/sumWidget/widget.cpp Executable file
View File

@ -0,0 +1,56 @@
#include "widget.h"
#include <QDebug>
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()
{
}

27
day10/sumWidget/widget.h Executable file
View File

@ -0,0 +1,27 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <iostream>
#include <QPushButton> // 按钮
#include <QLineEdit> // 文本输入框
#include <QLabel> // 文本标签
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