day3: 案例综合(常用控件、消息)

This commit is contained in:
flykhan 2023-08-09 18:22:00 +08:00
parent c92c63bb96
commit cd4484ebe5
66 changed files with 2120 additions and 0 deletions

73
EventTest/.gitignore vendored Normal file
View 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

31
EventTest/EventTest.pro Normal file
View File

@ -0,0 +1,31 @@
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 \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

11
EventTest/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

15
EventTest/mainwindow.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}

21
EventTest/mainwindow.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

22
EventTest/mainwindow.ui Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

73
qtdemo03/.gitignore vendored Normal file
View 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

14
qtdemo03/form1.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "form1.h"
#include "ui_form1.h"
Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);
}
Form1::~Form1()
{
delete ui;
}

22
qtdemo03/form1.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef FORM1_H
#define FORM1_H
#include <QWidget>
namespace Ui {
class Form1;
}
class Form1 : public QWidget
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = nullptr);
~Form1();
private:
Ui::Form1 *ui;
};
#endif // FORM1_H

45
qtdemo03/form1.ui Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form1</class>
<widget class="QWidget" name="Form1">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>480</width>
<height>640</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_2">
<property name="echoMode">
<enum>QLineEdit::NoEcho</enum>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_3">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_4">
<property name="echoMode">
<enum>QLineEdit::PasswordEchoOnEdit</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

21
qtdemo03/form2.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "form2.h"
#include "ui_form2.h"
Form2::Form2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form2)
{
ui->setupUi(this);
}
Form2::~Form2()
{
delete ui;
}
void Form2::on_btn_clicked()
{
QString r12 = ui->rb1->isChecked()?"放学了":"还没放";
QString r34 = ui->rb3->isChecked()?"吃饭了":"还没吃";
ui->result->setText(r12+", "+r34);
}

25
qtdemo03/form2.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef FORM2_H
#define FORM2_H
#include <QWidget>
namespace Ui {
class Form2;
}
class Form2 : public QWidget
{
Q_OBJECT
public:
explicit Form2(QWidget *parent = nullptr);
~Form2();
private slots:
void on_btn_clicked();
private:
Ui::Form2 *ui;
};
#endif // FORM2_H

141
qtdemo03/form2.ui Normal file
View File

@ -0,0 +1,141 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form2</class>
<widget class="QWidget" name="Form2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>480</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>放学了吗?</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<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>
<widget class="QRadioButton" name="rb1">
<property name="text">
<string>嗯</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="rb2">
<property name="text">
<string>没</string>
</property>
</widget>
</item>
<item>
<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>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>吃了没?</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<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>
<widget class="QRadioButton" name="rb3">
<property name="text">
<string>嗯</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="rb5">
<property name="text">
<string>没</string>
</property>
</widget>
</item>
<item>
<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>
</item>
<item>
<widget class="QPushButton" name="btn">
<property name="text">
<string>显示结果</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="result">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

30
qtdemo03/form3.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "form3.h"
#include "ui_form3.h"
Form3::Form3(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form3)
{
ui->setupUi(this);
}
Form3::~Form3()
{
delete ui;
}
void Form3::on_pushButton_clicked()
{
QString s;
if(ui->w1->isChecked())
s+=ui->w1->text()+", ";
if(ui->w2->isChecked())
s+=ui->w2->text()+", ";
if(ui->w3->isChecked())
s+=ui->w3->text()+", ";
if(ui->w4->isChecked())
s+=ui->w4->text()+", ";
// 删除 最后一个", "
s.remove(s.size()-2,1);
ui->result->setText(s);
}

25
qtdemo03/form3.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef FORM3_H
#define FORM3_H
#include <QWidget>
namespace Ui {
class Form3;
}
class Form3 : public QWidget
{
Q_OBJECT
public:
explicit Form3(QWidget *parent = nullptr);
~Form3();
private slots:
void on_pushButton_clicked();
private:
Ui::Form3 *ui;
};
#endif // FORM3_H

95
qtdemo03/form3.ui Normal file
View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form3</class>
<widget class="QWidget" name="Form3">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>480</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>你喜欢的开发岗位?</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="w1">
<property name="text">
<string>IoT应用开发</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="w2">
<property name="text">
<string>C/C++应用开发工程师</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="w3">
<property name="text">
<string>嵌入式驱动开发</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="w4">
<property name="text">
<string>Qt桌面应用开发</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<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>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>显示结果</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="result">
<property name="text">
<string>结果:</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

23
qtdemo03/form4.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "form4.h"
#include "ui_form4.h"
Form4::Form4(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form4)
{
ui->setupUi(this);
}
Form4::~Form4()
{
delete ui;
}
void Form4::on_okBtn_clicked()
{
QString sex = ui->sexBox->currentText();
QString city = ui->cityBox->currentText();
ui->result->setText(sex+", "+city);
QMessageBox::information(this,"信息",tr("性别: %1, 家乡: %2").arg(sex).arg(city));
}

26
qtdemo03/form4.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef FORM4_H
#define FORM4_H
#include <QWidget>
#include <QMessageBox>
namespace Ui {
class Form4;
}
class Form4 : public QWidget
{
Q_OBJECT
public:
explicit Form4(QWidget *parent = nullptr);
~Form4();
private slots:
void on_okBtn_clicked();
private:
Ui::Form4 *ui;
};
#endif // FORM4_H

122
qtdemo03/form4.ui Normal file
View File

@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form4</class>
<widget class="QWidget" name="Form4">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QComboBox" name="cityBox">
<item>
<property name="text">
<string/>
</property>
</item>
<item>
<property name="text">
<string>北京</string>
</property>
</item>
<item>
<property name="text">
<string>上海</string>
</property>
</item>
<item>
<property name="text">
<string>陕西</string>
</property>
</item>
<item>
<property name="text">
<string>甘肃</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>家乡: </string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>性别: </string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="sexBox">
<item>
<property name="text">
<string/>
</property>
</item>
<item>
<property name="text">
<string>男</string>
</property>
</item>
<item>
<property name="text">
<string>女</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>339</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="okBtn">
<property name="text">
<string>提交</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="result">
<property name="text">
<string>结果:</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

41
qtdemo03/form5.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "form5.h"
#include "ui_form5.h"
Form5::Form5(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form5)
{
ui->setupUi(this);
}
Form5::~Form5()
{
delete ui;
}
void Form5::on_addBtn_clicked()
{
ui->listWidget->addItem(ui->lineEdit->text());
}
void Form5::on_getBtn_clicked()
{
QString selectedContent = ui->listWidget->currentItem()->text();
QMessageBox::information(this,"选中",selectedContent);
}
void Form5::on_clearBtn_clicked()
{
ui->listWidget->clear();
}
void Form5::on_delBtn_clicked()
{
// currentItem() 获取当前选定的项目
QListWidgetItem *item = ui->listWidget->currentItem();
// takeItem() 通过索引删除行
ui->listWidget->takeItem(ui->listWidget->row(item));
delete item; //释放项目所占的内存
}

32
qtdemo03/form5.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef FORM5_H
#define FORM5_H
#include <QWidget>
#include <QMessageBox>
namespace Ui {
class Form5;
}
class Form5 : public QWidget
{
Q_OBJECT
public:
explicit Form5(QWidget *parent = nullptr);
~Form5();
private slots:
void on_addBtn_clicked();
void on_getBtn_clicked();
void on_clearBtn_clicked();
void on_delBtn_clicked();
private:
Ui::Form5 *ui;
};
#endif // FORM5_H

145
qtdemo03/form5.ui Normal file
View File

@ -0,0 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form5</class>
<widget class="QWidget" name="Form5">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="addBtn">
<property name="text">
<string>添加</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/icons/images/add.png</normaloff>:/icons/images/add.png</iconset>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QListWidget" name="listWidget">
<item>
<property name="text">
<string>d:\images\1.png</string>
</property>
</item>
<item>
<property name="text">
<string>d:\images\2.png</string>
</property>
</item>
<item>
<property name="text">
<string>d:\images\3.png</string>
</property>
</item>
</widget>
</item>
<item row="2" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<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>
<item>
<widget class="QPushButton" name="getBtn">
<property name="text">
<string>获取</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/icons/images/open.png</normaloff>:/icons/images/open.png</iconset>
</property>
</widget>
</item>
<item>
<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>
<widget class="QToolButton" name="delBtn">
<property name="text">
<string>删除</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/icons/images/stop.png</normaloff>:/icons/images/stop.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<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>
<widget class="QPushButton" name="clearBtn">
<property name="text">
<string>清空</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/icons/images/OnePiece.png</normaloff>:/icons/images/OnePiece.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<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>
</item>
</layout>
</widget>
<resources>
<include location="res.qrc"/>
</resources>
<connections/>
</ui>

59
qtdemo03/form6.cpp Normal file
View File

@ -0,0 +1,59 @@
#include "form6.h"
#include "ui_form6.h"
Form6::Form6(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form6)
{
ui->setupUi(this);
}
Form6::~Form6()
{
delete ui;
}
void Form6::on_addBtn_clicked()
{
// 获取当前行
int maxRow = ui->tableWidget->rowCount();
// 新增一行空行
ui->tableWidget->insertRow(maxRow);
QTableWidgetItem *sidItem = new QTableWidgetItem(ui->sidEdit->text());
QTableWidgetItem *nameItem = new QTableWidgetItem(ui->nameEdit->text());
QTableWidgetItem *sexItem = new QTableWidgetItem(ui->sexBox->currentText());
QTableWidgetItem *ageItem = new QTableWidgetItem(ui->ageEdit->text());
ui->tableWidget->setItem(maxRow,0,sidItem);
ui->tableWidget->setItem(maxRow,1,nameItem);
ui->tableWidget->setItem(maxRow,2,sexItem);
ui->tableWidget->setItem(maxRow,3,ageItem);
}
void Form6::on_getBtn_clicked()
{
int selectedRow = ui->tableWidget->currentRow();
// int maxCol = ui->tableWidget->columnCount();
QString sid = ui->tableWidget->item(selectedRow,0)->text();
ui->sidEdit->setText(sid);
QString name = ui->tableWidget->item(selectedRow,1)->text();
ui->nameEdit->setText(name);
QString sex = ui->tableWidget->item(selectedRow,2)->text();
ui->sexBox->setCurrentText(sex);
QString age = ui->tableWidget->item(selectedRow,3)->text();
ui->ageEdit->setText(age);
}
void Form6::on_delBtn_clicked()
{
int row = ui->tableWidget->currentRow();
ui->tableWidget->removeRow(row); // 删除行
// for(int col = 0; col <ui->tableWidget->columnCount();col++)
// {
// ui->tableWidget->takeItem(row,col);
// }
}

29
qtdemo03/form6.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef FORM6_H
#define FORM6_H
#include <QWidget>
namespace Ui {
class Form6;
}
class Form6 : public QWidget
{
Q_OBJECT
public:
explicit Form6(QWidget *parent = nullptr);
~Form6();
private slots:
void on_addBtn_clicked();
void on_getBtn_clicked();
void on_delBtn_clicked();
private:
Ui::Form6 *ui;
};
#endif // FORM6_H

270
qtdemo03/form6.ui Normal file
View File

@ -0,0 +1,270 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form6</class>
<widget class="QWidget" name="Form6">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>670</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>学号: </string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="sidEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>姓名: </string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="nameEdit"/>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>性别: </string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="sexBox">
<item>
<property name="text">
<string>男</string>
</property>
</item>
<item>
<property name="text">
<string>女</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>年龄: </string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="ageEdit"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QTableWidget" name="tableWidget">
<row>
<property name="text">
<string>1</string>
</property>
</row>
<row>
<property name="text">
<string>2</string>
</property>
</row>
<row>
<property name="text">
<string>3</string>
</property>
</row>
<column>
<property name="text">
<string>学号</string>
</property>
</column>
<column>
<property name="text">
<string>姓名</string>
</property>
</column>
<column>
<property name="text">
<string>性别</string>
</property>
</column>
<column>
<property name="text">
<string>年龄</string>
</property>
</column>
<item row="0" column="0">
<property name="text">
<string>1001</string>
</property>
</item>
<item row="0" column="1">
<property name="text">
<string>张三</string>
</property>
</item>
<item row="0" column="2">
<property name="text">
<string>男</string>
</property>
</item>
<item row="0" column="3">
<property name="text">
<string>20</string>
</property>
</item>
<item row="1" column="0">
<property name="text">
<string>1002</string>
</property>
</item>
<item row="1" column="1">
<property name="text">
<string>李四</string>
</property>
</item>
<item row="1" column="2">
<property name="text">
<string>女</string>
</property>
</item>
<item row="1" column="3">
<property name="text">
<string>23</string>
</property>
</item>
<item row="2" column="0">
<property name="text">
<string>1003</string>
</property>
</item>
<item row="2" column="1">
<property name="text">
<string>王五</string>
</property>
</item>
<item row="2" column="2">
<property name="text">
<string>男</string>
</property>
</item>
<item row="2" column="3">
<property name="text">
<string>20</string>
</property>
</item>
</widget>
</item>
<item row="1" column="1">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="addBtn">
<property name="text">
<string>PushButton</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/icons/images/add.png</normaloff>:/icons/images/add.png</iconset>
</property>
<property name="iconSize">
<size>
<width>50</width>
<height>50</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="getBtn">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/icons/images/open.png</normaloff>:/icons/images/open.png</iconset>
</property>
<property name="iconSize">
<size>
<width>50</width>
<height>50</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="delBtn">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="res.qrc">
<normaloff>:/images/images/cleanport.png</normaloff>:/images/images/cleanport.png</iconset>
</property>
<property name="iconSize">
<size>
<width>50</width>
<height>50</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="res.qrc"/>
</resources>
<connections/>
</ui>

56
qtdemo03/form7.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "form7.h"
#include "ui_form7.h"
Form7::Form7(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form7)
{
ui->setupUi(this);
this->setWindowTitle("合并案例");
this->setWindowIcon(QIcon(":/icons/images/logo.png"));
}
Form7::~Form7()
{
delete ui;
}
void Form7::on_btn1_clicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
void Form7::on_btn2_clicked()
{
ui->stackedWidget->setCurrentIndex(1);
}
void Form7::on_btn3_clicked()
{
ui->stackedWidget->setCurrentIndex(2);
}
void Form7::on_btn5_clicked()
{
ui->stackedWidget->setCurrentIndex(3);
}
void Form7::on_btn6_clicked()
{
ui->stackedWidget->setCurrentIndex(4);
}
void Form7::on_btn7_clicked()
{
ui->stackedWidget->setCurrentIndex(5);
}
void Form7::on_btn4_clicked()
{
ui->stackedWidget->setCurrentIndex(6);
}
void Form7::on_btn8_clicked()
{
ui->stackedWidget->setCurrentIndex(7);
}

39
qtdemo03/form7.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef FORM7_H
#define FORM7_H
#include <QWidget>
namespace Ui {
class Form7;
}
class Form7 : public QWidget
{
Q_OBJECT
public:
explicit Form7(QWidget *parent = nullptr);
~Form7();
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
void on_btn3_clicked();
void on_btn4_clicked();
void on_btn5_clicked();
void on_btn6_clicked();
void on_btn7_clicked();
void on_btn8_clicked();
private:
Ui::Form7 *ui;
};
#endif // FORM7_H

148
qtdemo03/form7.ui Normal file
View File

@ -0,0 +1,148 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form7</class>
<widget class="QWidget" name="Form7">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>7</number>
</property>
<widget class="Form1" name="page_1"/>
<widget class="Form2" name="page_2"/>
<widget class="Form3" name="page_3"/>
<widget class="Form4" name="page_4"/>
<widget class="Form5" name="page_5"/>
<widget class="Form6" name="page_6"/>
<widget class="SmallWidget" name="page_SmallWidget"/>
<widget class="Form8" name="page_8"/>
</widget>
</item>
<item row="0" column="1">
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="btn1">
<property name="text">
<string>Form1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn2">
<property name="text">
<string>Form2</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn3">
<property name="text">
<string>Form3</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn5">
<property name="text">
<string>Form4</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn6">
<property name="text">
<string>Form5</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn7">
<property name="text">
<string>Form6</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn4">
<property name="text">
<string>SmallWidget</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn8">
<property name="text">
<string>Form8</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>SmallWidget</class>
<extends>QWidget</extends>
<header location="global">smallwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form1</class>
<extends>QWidget</extends>
<header>form1.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form2</class>
<extends>QWidget</extends>
<header>form2.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form3</class>
<extends>QWidget</extends>
<header>form3.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form4</class>
<extends>QWidget</extends>
<header>form4.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form5</class>
<extends>QWidget</extends>
<header>form5.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form6</class>
<extends>QWidget</extends>
<header>form6.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Form8</class>
<extends>QWidget</extends>
<header>form8.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

53
qtdemo03/form8.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "form8.h"
#include "ui_form8.h"
Form8::Form8(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form8)
{
ui->setupUi(this);
ui->Form8label->setWordWrap(true);
// ui->Form8label->setMinimumWidth(400);
// ui->Form8label->setMaximumWidth(400);
}
Form8::~Form8()
{
delete ui;
}
bool Form8::event(QEvent *evt)
{
qDebug()<<"事件分发"<<evt->type();
if(evt->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(evt);
if(keyEvent->key() == Qt::Key_Return){
// ui->Form8label->setText(tr("按下 %1 键").arg(keyEvent->text()));
qDebug()<<tr("按下 %1 键").arg(keyEvent->text());
}
// return false; // 不分发此事件
}
return QWidget::event(evt);
}
void Form8::keyPressEvent(QKeyEvent *event)
{
QString s = ui->Form8label->text();
// 如果按下的是换行
if(event->key() == Qt::Key_Enter){
s+="\r\n";
}else if(event->key() == Qt::Key_Space){
s.clear();
}else if(event->key() == Qt::Key_Backspace){
// s.remove(s.size()-1,1);
s.chop(1);
}
else{
s += event->text();
}
ui->Form8label->setText(s);
}

29
qtdemo03/form8.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef FORM8_H
#define FORM8_H
#include <QWidget>
#include <QKeyEvent>
#include <QEvent>
#include <QDebug>
namespace Ui {
class Form8;
}
class Form8 : public QWidget
{
Q_OBJECT
public:
explicit Form8(QWidget *parent = nullptr);
~Form8();
protected:
virtual void keyPressEvent(QKeyEvent *event) override;
virtual bool event(QEvent *event) override;
private:
Ui::Form8 *ui;
};
#endif // FORM8_H

28
qtdemo03/form8.ui Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form8</class>
<widget class="QWidget" name="Form8">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="Form8label">
<property name="text">
<string>请在这里打字(回车换行,空格清空,返回删除字符)</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

BIN
qtdemo03/images/Edit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
qtdemo03/images/Frame.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
qtdemo03/images/Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
qtdemo03/images/Luffy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
qtdemo03/images/LuffyQ.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
qtdemo03/images/New.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

BIN
qtdemo03/images/Pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
qtdemo03/images/Right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
qtdemo03/images/Start.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

BIN
qtdemo03/images/Sunny.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
qtdemo03/images/add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
qtdemo03/images/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
qtdemo03/images/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
qtdemo03/images/mario.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
qtdemo03/images/mesage.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
qtdemo03/images/open.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
qtdemo03/images/reload.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
qtdemo03/images/save.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
qtdemo03/images/stop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
qtdemo03/images/sunny.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

BIN
qtdemo03/images/time.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
qtdemo03/images/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

29
qtdemo03/main.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "mainwindow.h"
#include "form1.h"
#include "form2.h"
#include "form3.h"
#include <form4.h>
#include <form5.h>
#include <form6.h>
#include <form7.h>
#include <smallwidget.h>
#include <form8.h>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// Form1 w;
// Form2 w;
// Form3 w;
// Form4 w;
// Form5 w;
// Form6 w;
// SmallWidget w;
// Form7 w;
Form7 w;
w.show();
return a.exec();
}

53
qtdemo03/mainwindow.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this); // 显示UI界面
// // 为ui界面中的btn1按钮设置绑定
// connect(ui->btn1,&QPushButton::clicked,[&]{
// QMessageBox::about(this,"关于","ui中的按钮被点击了");
// });
// 退出事件绑定
connect(ui->exitAction,&QAction::triggered,[&]{
this->close();
});
}
MainWindow::~MainWindow()
{
delete ui; // 手动释放
}
void MainWindow::on_btn1_clicked()
{
// QLabel 显示图片
QPixmap pix;
pix.load(":/images/images/Sunny.jpg");
ui->label->setPixmap(pix);
}
void MainWindow::on_btn2_clicked()
{
// 显示网页内容
QString html = "<h1>一号标题</h1><a href='https://www.baidu.com'>百度</a>";
ui->label->setTextFormat(Qt::RichText); // 文本的格式为富文本
ui->label->setText(html);
ui->label->setOpenExternalLinks(true); // 支持内容中的 a 标签直接打开
}
void MainWindow::on_btn3_clicked()
{
// 显示动画
QMovie *movie = new QMovie(":/movies/images/mario.gif");
ui->label->setMovie(movie);
movie->start();
}

32
qtdemo03/mainwindow.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
#include <QPixmap>
#include <QMovie>
#include <QIcon>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn1_clicked();
void on_btn3_clicked();
void on_btn2_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

97
qtdemo03/mainwindow.ui Normal file
View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="btn1">
<property name="text">
<string>图片</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn2">
<property name="text">
<string>网页</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn3">
<property name="text">
<string>动画</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>文件</string>
</property>
<addaction name="openAction"/>
<addaction name="newAction_3"/>
<addaction name="separator"/>
<addaction name="exitAction"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="openAction">
<property name="text">
<string>打开</string>
</property>
</action>
<action name="newAction_3">
<property name="text">
<string>新建</string>
</property>
</action>
<action name="exitAction">
<property name="text">
<string>退出</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

60
qtdemo03/qtdemo03.pro Normal file
View File

@ -0,0 +1,60 @@
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 += \
form1.cpp \
form2.cpp \
form3.cpp \
form4.cpp \
form5.cpp \
form6.cpp \
form7.cpp \
form8.cpp \
main.cpp \
smallwidget.cpp \
mainwindow.cpp
HEADERS += \
form1.h \
form2.h \
form3.h \
form4.h \
form5.h \
form6.h \
form7.h \
form8.h \
smallwidget.h \
mainwindow.h
FORMS += \
form1.ui \
form2.ui \
form3.ui \
form4.ui \
form5.ui \
form6.ui \
form7.ui \
form8.ui \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc

35
qtdemo03/res.qrc Normal file
View File

@ -0,0 +1,35 @@
<RCC>
<qresource prefix="/images">
<file>images/butterfly.png</file>
<file>images/butterfly1.png</file>
<file>images/cleanport.png</file>
<file>images/down.png</file>
<file>images/Frame.jpg</file>
<file>images/Luffy.png</file>
<file>images/LuffyQ.png</file>
<file>images/Sunny.jpg</file>
<file>images/sunny.png</file>
</qresource>
<qresource prefix="/movies">
<file>images/mario.gif</file>
</qresource>
<qresource prefix="/icons">
<file>images/add.png</file>
<file>images/Edit.png</file>
<file>images/Left.png</file>
<file>images/logo.png</file>
<file>images/New.png</file>
<file>images/open.png</file>
<file>images/Pause.png</file>
<file>images/reload.png</file>
<file>images/Right.png</file>
<file>images/save.png</file>
<file>images/Start.png</file>
<file>images/stop.png</file>
<file>images/time.png</file>
<file>images/up.png</file>
<file>images/write2file.png</file>
<file>images/mesage.png</file>
<file>images/OnePiece.png</file>
</qresource>
</RCC>

25
qtdemo03/smallwidget.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "smallwidget.h"
SmallWidget::SmallWidget(QWidget *parent) : QWidget(parent)
{
spinBox = new QSpinBox(this);
spinBox->setGeometry(20,20,100,50);
slider = new QSlider(Qt::Vertical,this); // Horizontal 垂直方向
slider->setGeometry(140,20,40,100);
slider->setMaximum(100);
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(spinBox);
layout->addWidget(slider);
setLayout(layout); // 设置当前 QWidget 的布局
connect(slider,&QSlider::valueChanged,[&](int val){
spinBox->setValue(val);
});
connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
}

25
qtdemo03/smallwidget.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef SMALLWIDGET_H
#define SMALLWIDGET_H
#include <QWidget>
#include <QSpinBox>
#include <QSlider> // 滑块
#include <QHBoxLayout> // 水平排列布局管理器
#include <QVBoxLayout> // 垂直排列布局管理器
class SmallWidget : public QWidget
{
Q_OBJECT
private:
QSpinBox *spinBox;
QSlider *slider;
public:
explicit SmallWidget(QWidget *parent = nullptr);
signals:
};
#endif // SMALLWIDGET_H