diff --git a/EventTest/.gitignore b/EventTest/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/EventTest/.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/EventTest/EventTest.pro b/EventTest/EventTest.pro new file mode 100644 index 0000000..0086fd6 --- /dev/null +++ b/EventTest/EventTest.pro @@ -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 diff --git a/EventTest/main.cpp b/EventTest/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/EventTest/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/EventTest/mainwindow.cpp b/EventTest/mainwindow.cpp new file mode 100644 index 0000000..41a26bd --- /dev/null +++ b/EventTest/mainwindow.cpp @@ -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; +} + diff --git a/EventTest/mainwindow.h b/EventTest/mainwindow.h new file mode 100644 index 0000000..4643e32 --- /dev/null +++ b/EventTest/mainwindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +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 diff --git a/EventTest/mainwindow.ui b/EventTest/mainwindow.ui new file mode 100644 index 0000000..b232854 --- /dev/null +++ b/EventTest/mainwindow.ui @@ -0,0 +1,22 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + diff --git a/qtdemo03/.gitignore b/qtdemo03/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/qtdemo03/.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/qtdemo03/form1.cpp b/qtdemo03/form1.cpp new file mode 100644 index 0000000..661207e --- /dev/null +++ b/qtdemo03/form1.cpp @@ -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; +} diff --git a/qtdemo03/form1.h b/qtdemo03/form1.h new file mode 100644 index 0000000..d43df0b --- /dev/null +++ b/qtdemo03/form1.h @@ -0,0 +1,22 @@ +#ifndef FORM1_H +#define FORM1_H + +#include + +namespace Ui { +class Form1; +} + +class Form1 : public QWidget +{ + Q_OBJECT + +public: + explicit Form1(QWidget *parent = nullptr); + ~Form1(); + +private: + Ui::Form1 *ui; +}; + +#endif // FORM1_H diff --git a/qtdemo03/form1.ui b/qtdemo03/form1.ui new file mode 100644 index 0000000..f3b2d3b --- /dev/null +++ b/qtdemo03/form1.ui @@ -0,0 +1,45 @@ + + + Form1 + + + + 0 + 0 + 480 + 640 + + + + Form + + + + + + + + + QLineEdit::NoEcho + + + + + + + QLineEdit::Password + + + + + + + QLineEdit::PasswordEchoOnEdit + + + + + + + + diff --git a/qtdemo03/form2.cpp b/qtdemo03/form2.cpp new file mode 100644 index 0000000..1e6e237 --- /dev/null +++ b/qtdemo03/form2.cpp @@ -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); +} diff --git a/qtdemo03/form2.h b/qtdemo03/form2.h new file mode 100644 index 0000000..3ca4b01 --- /dev/null +++ b/qtdemo03/form2.h @@ -0,0 +1,25 @@ +#ifndef FORM2_H +#define FORM2_H + +#include + +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 diff --git a/qtdemo03/form2.ui b/qtdemo03/form2.ui new file mode 100644 index 0000000..5b871bd --- /dev/null +++ b/qtdemo03/form2.ui @@ -0,0 +1,141 @@ + + + Form2 + + + + 0 + 0 + 480 + 300 + + + + Form + + + + + + + + + 放学了吗? + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + 吃了没? + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 显示结果 + + + + + + + TextLabel + + + + + + + + diff --git a/qtdemo03/form3.cpp b/qtdemo03/form3.cpp new file mode 100644 index 0000000..3839499 --- /dev/null +++ b/qtdemo03/form3.cpp @@ -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); +} diff --git a/qtdemo03/form3.h b/qtdemo03/form3.h new file mode 100644 index 0000000..d5bb2d6 --- /dev/null +++ b/qtdemo03/form3.h @@ -0,0 +1,25 @@ +#ifndef FORM3_H +#define FORM3_H + +#include + +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 diff --git a/qtdemo03/form3.ui b/qtdemo03/form3.ui new file mode 100644 index 0000000..e0ae1c4 --- /dev/null +++ b/qtdemo03/form3.ui @@ -0,0 +1,95 @@ + + + Form3 + + + + 0 + 0 + 480 + 300 + + + + Form + + + + + + + + + + + + 你喜欢的开发岗位? + + + + + + + IoT应用开发 + + + + + + + C/C++应用开发工程师 + + + + + + + 嵌入式驱动开发 + + + + + + + Qt桌面应用开发 + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 显示结果 + + + + + + + + + + 结果: + + + + + + + + diff --git a/qtdemo03/form4.cpp b/qtdemo03/form4.cpp new file mode 100644 index 0000000..372d679 --- /dev/null +++ b/qtdemo03/form4.cpp @@ -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)); +} diff --git a/qtdemo03/form4.h b/qtdemo03/form4.h new file mode 100644 index 0000000..f5fc08d --- /dev/null +++ b/qtdemo03/form4.h @@ -0,0 +1,26 @@ +#ifndef FORM4_H +#define FORM4_H + +#include +#include + +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 diff --git a/qtdemo03/form4.ui b/qtdemo03/form4.ui new file mode 100644 index 0000000..e5f1e86 --- /dev/null +++ b/qtdemo03/form4.ui @@ -0,0 +1,122 @@ + + + Form4 + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + + + + + + + + + + + + + + 北京 + + + + + 上海 + + + + + 陕西 + + + + + 甘肃 + + + + + + + + 家乡: + + + + + + + 性别: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + + 339 + 20 + + + + + + + + 提交 + + + + + + + + + + 结果: + + + + + + + + diff --git a/qtdemo03/form5.cpp b/qtdemo03/form5.cpp new file mode 100644 index 0000000..f9f0c22 --- /dev/null +++ b/qtdemo03/form5.cpp @@ -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; //释放项目所占的内存 +} diff --git a/qtdemo03/form5.h b/qtdemo03/form5.h new file mode 100644 index 0000000..de8ac8a --- /dev/null +++ b/qtdemo03/form5.h @@ -0,0 +1,32 @@ +#ifndef FORM5_H +#define FORM5_H + +#include +#include + +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 diff --git a/qtdemo03/form5.ui b/qtdemo03/form5.ui new file mode 100644 index 0000000..642b8f4 --- /dev/null +++ b/qtdemo03/form5.ui @@ -0,0 +1,145 @@ + + + Form5 + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + + + + + 添加 + + + + :/icons/images/add.png:/icons/images/add.png + + + + + + + + d:\images\1.png + + + + + d:\images\2.png + + + + + d:\images\3.png + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 获取 + + + + :/icons/images/open.png:/icons/images/open.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 删除 + + + + :/icons/images/stop.png:/icons/images/stop.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 清空 + + + + :/icons/images/OnePiece.png:/icons/images/OnePiece.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + diff --git a/qtdemo03/form6.cpp b/qtdemo03/form6.cpp new file mode 100644 index 0000000..63ad9eb --- /dev/null +++ b/qtdemo03/form6.cpp @@ -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 tableWidget->columnCount();col++) +// { +// ui->tableWidget->takeItem(row,col); +// } +} diff --git a/qtdemo03/form6.h b/qtdemo03/form6.h new file mode 100644 index 0000000..806f1f7 --- /dev/null +++ b/qtdemo03/form6.h @@ -0,0 +1,29 @@ +#ifndef FORM6_H +#define FORM6_H + +#include + +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 diff --git a/qtdemo03/form6.ui b/qtdemo03/form6.ui new file mode 100644 index 0000000..7dc7ec5 --- /dev/null +++ b/qtdemo03/form6.ui @@ -0,0 +1,270 @@ + + + Form6 + + + + 0 + 0 + 670 + 480 + + + + Form + + + + + + + + + 学号: + + + + + + + + + + + + + + 姓名: + + + + + + + + + + 性别: + + + + + + + + + + + + + + + + + + + + + 年龄: + + + + + + + + + + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 学号 + + + + + 姓名 + + + + + 性别 + + + + + 年龄 + + + + + 1001 + + + + + 张三 + + + + + + + + + + 20 + + + + + 1002 + + + + + 李四 + + + + + + + + + + 23 + + + + + 1003 + + + + + 王五 + + + + + + + + + + 20 + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + PushButton + + + + :/icons/images/add.png:/icons/images/add.png + + + + 50 + 50 + + + + + + + + ... + + + + :/icons/images/open.png:/icons/images/open.png + + + + 50 + 50 + + + + + + + + ... + + + + :/images/images/cleanport.png:/images/images/cleanport.png + + + + 50 + 50 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + + + + diff --git a/qtdemo03/form7.cpp b/qtdemo03/form7.cpp new file mode 100644 index 0000000..a6e4039 --- /dev/null +++ b/qtdemo03/form7.cpp @@ -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); +} diff --git a/qtdemo03/form7.h b/qtdemo03/form7.h new file mode 100644 index 0000000..d2967e2 --- /dev/null +++ b/qtdemo03/form7.h @@ -0,0 +1,39 @@ +#ifndef FORM7_H +#define FORM7_H + +#include + +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 diff --git a/qtdemo03/form7.ui b/qtdemo03/form7.ui new file mode 100644 index 0000000..b185d4c --- /dev/null +++ b/qtdemo03/form7.ui @@ -0,0 +1,148 @@ + + + Form7 + + + + 0 + 0 + 800 + 600 + + + + Form + + + + + + 7 + + + + + + + + + + + + + + + + + + Form1 + + + + + + + Form2 + + + + + + + Form3 + + + + + + + Form4 + + + + + + + Form5 + + + + + + + Form6 + + + + + + + SmallWidget + + + + + + + Form8 + + + + + + + + + + + SmallWidget + QWidget +
smallwidget.h
+ 1 +
+ + Form1 + QWidget +
form1.h
+ 1 +
+ + Form2 + QWidget +
form2.h
+ 1 +
+ + Form3 + QWidget +
form3.h
+ 1 +
+ + Form4 + QWidget +
form4.h
+ 1 +
+ + Form5 + QWidget +
form5.h
+ 1 +
+ + Form6 + QWidget +
form6.h
+ 1 +
+ + Form8 + QWidget +
form8.h
+ 1 +
+
+ + +
diff --git a/qtdemo03/form8.cpp b/qtdemo03/form8.cpp new file mode 100644 index 0000000..def9dce --- /dev/null +++ b/qtdemo03/form8.cpp @@ -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()<<"事件分发"<type(); + if(evt->type() == QEvent::KeyPress){ + QKeyEvent *keyEvent = static_cast(evt); + if(keyEvent->key() == Qt::Key_Return){ +// ui->Form8label->setText(tr("按下 %1 键").arg(keyEvent->text())); + qDebug()<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); +} + diff --git a/qtdemo03/form8.h b/qtdemo03/form8.h new file mode 100644 index 0000000..826e31e --- /dev/null +++ b/qtdemo03/form8.h @@ -0,0 +1,29 @@ +#ifndef FORM8_H +#define FORM8_H + +#include +#include +#include +#include + +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 diff --git a/qtdemo03/form8.ui b/qtdemo03/form8.ui new file mode 100644 index 0000000..0df8799 --- /dev/null +++ b/qtdemo03/form8.ui @@ -0,0 +1,28 @@ + + + Form8 + + + + 0 + 0 + 640 + 480 + + + + Form + + + + + + 请在这里打字(回车换行,空格清空,返回删除字符) + + + + + + + + diff --git a/qtdemo03/images/Edit.png b/qtdemo03/images/Edit.png new file mode 100644 index 0000000..07e386d Binary files /dev/null and b/qtdemo03/images/Edit.png differ diff --git a/qtdemo03/images/Frame.jpg b/qtdemo03/images/Frame.jpg new file mode 100644 index 0000000..0ebb428 Binary files /dev/null and b/qtdemo03/images/Frame.jpg differ diff --git a/qtdemo03/images/Left.png b/qtdemo03/images/Left.png new file mode 100644 index 0000000..e638f97 Binary files /dev/null and b/qtdemo03/images/Left.png differ diff --git a/qtdemo03/images/Luffy.png b/qtdemo03/images/Luffy.png new file mode 100644 index 0000000..f0410d8 Binary files /dev/null and b/qtdemo03/images/Luffy.png differ diff --git a/qtdemo03/images/LuffyQ.png b/qtdemo03/images/LuffyQ.png new file mode 100644 index 0000000..580a2e5 Binary files /dev/null and b/qtdemo03/images/LuffyQ.png differ diff --git a/qtdemo03/images/New.png b/qtdemo03/images/New.png new file mode 100644 index 0000000..5a6574f Binary files /dev/null and b/qtdemo03/images/New.png differ diff --git a/qtdemo03/images/OnePiece.png b/qtdemo03/images/OnePiece.png new file mode 100644 index 0000000..f64ca90 Binary files /dev/null and b/qtdemo03/images/OnePiece.png differ diff --git a/qtdemo03/images/Pause.png b/qtdemo03/images/Pause.png new file mode 100644 index 0000000..550be9f Binary files /dev/null and b/qtdemo03/images/Pause.png differ diff --git a/qtdemo03/images/Right.png b/qtdemo03/images/Right.png new file mode 100644 index 0000000..8885e22 Binary files /dev/null and b/qtdemo03/images/Right.png differ diff --git a/qtdemo03/images/Start.png b/qtdemo03/images/Start.png new file mode 100644 index 0000000..a2fd225 Binary files /dev/null and b/qtdemo03/images/Start.png differ diff --git a/qtdemo03/images/Sunny.jpg b/qtdemo03/images/Sunny.jpg new file mode 100644 index 0000000..1eea0c4 Binary files /dev/null and b/qtdemo03/images/Sunny.jpg differ diff --git a/qtdemo03/images/add.png b/qtdemo03/images/add.png new file mode 100644 index 0000000..8c41538 Binary files /dev/null and b/qtdemo03/images/add.png differ diff --git a/qtdemo03/images/butterfly.png b/qtdemo03/images/butterfly.png new file mode 100644 index 0000000..f3e050e Binary files /dev/null and b/qtdemo03/images/butterfly.png differ diff --git a/qtdemo03/images/butterfly1.png b/qtdemo03/images/butterfly1.png new file mode 100644 index 0000000..573f63c Binary files /dev/null and b/qtdemo03/images/butterfly1.png differ diff --git a/qtdemo03/images/cleanport.png b/qtdemo03/images/cleanport.png new file mode 100644 index 0000000..c1bf216 Binary files /dev/null and b/qtdemo03/images/cleanport.png differ diff --git a/qtdemo03/images/down.png b/qtdemo03/images/down.png new file mode 100644 index 0000000..4fc666e Binary files /dev/null and b/qtdemo03/images/down.png differ diff --git a/qtdemo03/images/logo.png b/qtdemo03/images/logo.png new file mode 100644 index 0000000..76b7fe2 Binary files /dev/null and b/qtdemo03/images/logo.png differ diff --git a/qtdemo03/images/mario.gif b/qtdemo03/images/mario.gif new file mode 100644 index 0000000..c38a831 Binary files /dev/null and b/qtdemo03/images/mario.gif differ diff --git a/qtdemo03/images/mesage.png b/qtdemo03/images/mesage.png new file mode 100644 index 0000000..89b7c80 Binary files /dev/null and b/qtdemo03/images/mesage.png differ diff --git a/qtdemo03/images/open.png b/qtdemo03/images/open.png new file mode 100644 index 0000000..1282b1e Binary files /dev/null and b/qtdemo03/images/open.png differ diff --git a/qtdemo03/images/reload.png b/qtdemo03/images/reload.png new file mode 100644 index 0000000..4a80a85 Binary files /dev/null and b/qtdemo03/images/reload.png differ diff --git a/qtdemo03/images/save.png b/qtdemo03/images/save.png new file mode 100644 index 0000000..4e33bd4 Binary files /dev/null and b/qtdemo03/images/save.png differ diff --git a/qtdemo03/images/stop.png b/qtdemo03/images/stop.png new file mode 100644 index 0000000..8178019 Binary files /dev/null and b/qtdemo03/images/stop.png differ diff --git a/qtdemo03/images/sunny.png b/qtdemo03/images/sunny.png new file mode 100644 index 0000000..4a7f5f5 Binary files /dev/null and b/qtdemo03/images/sunny.png differ diff --git a/qtdemo03/images/time.png b/qtdemo03/images/time.png new file mode 100644 index 0000000..b62bdca Binary files /dev/null and b/qtdemo03/images/time.png differ diff --git a/qtdemo03/images/up.png b/qtdemo03/images/up.png new file mode 100644 index 0000000..b909596 Binary files /dev/null and b/qtdemo03/images/up.png differ diff --git a/qtdemo03/images/write2file.png b/qtdemo03/images/write2file.png new file mode 100644 index 0000000..1d82305 Binary files /dev/null and b/qtdemo03/images/write2file.png differ diff --git a/qtdemo03/main.cpp b/qtdemo03/main.cpp new file mode 100644 index 0000000..1aed7e9 --- /dev/null +++ b/qtdemo03/main.cpp @@ -0,0 +1,29 @@ +#include "mainwindow.h" +#include "form1.h" +#include "form2.h" +#include "form3.h" +#include +#include +#include +#include +#include +#include + +#include + +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(); +} diff --git a/qtdemo03/mainwindow.cpp b/qtdemo03/mainwindow.cpp new file mode 100644 index 0000000..aa555cc --- /dev/null +++ b/qtdemo03/mainwindow.cpp @@ -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 = "

一号标题

百度"; + 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(); + +} + diff --git a/qtdemo03/mainwindow.h b/qtdemo03/mainwindow.h new file mode 100644 index 0000000..db43956 --- /dev/null +++ b/qtdemo03/mainwindow.h @@ -0,0 +1,32 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include + +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 diff --git a/qtdemo03/mainwindow.ui b/qtdemo03/mainwindow.ui new file mode 100644 index 0000000..661dace --- /dev/null +++ b/qtdemo03/mainwindow.ui @@ -0,0 +1,97 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + + + TextLabel + + + + + + + + + 图片 + + + + + + + 网页 + + + + + + + 动画 + + + + + + + + + + + + + + 0 + 0 + 800 + 26 + + + + + 文件 + + + + + + + + + + + + 打开 + + + + + 新建 + + + + + 退出 + + + + + + diff --git a/qtdemo03/qtdemo03.pro b/qtdemo03/qtdemo03.pro new file mode 100644 index 0000000..fef1604 --- /dev/null +++ b/qtdemo03/qtdemo03.pro @@ -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 diff --git a/qtdemo03/res.qrc b/qtdemo03/res.qrc new file mode 100644 index 0000000..dfc91ac --- /dev/null +++ b/qtdemo03/res.qrc @@ -0,0 +1,35 @@ + + + images/butterfly.png + images/butterfly1.png + images/cleanport.png + images/down.png + images/Frame.jpg + images/Luffy.png + images/LuffyQ.png + images/Sunny.jpg + images/sunny.png + + + images/mario.gif + + + images/add.png + images/Edit.png + images/Left.png + images/logo.png + images/New.png + images/open.png + images/Pause.png + images/reload.png + images/Right.png + images/save.png + images/Start.png + images/stop.png + images/time.png + images/up.png + images/write2file.png + images/mesage.png + images/OnePiece.png + + diff --git a/qtdemo03/smallwidget.cpp b/qtdemo03/smallwidget.cpp new file mode 100644 index 0000000..769a688 --- /dev/null +++ b/qtdemo03/smallwidget.cpp @@ -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))); + +} diff --git a/qtdemo03/smallwidget.h b/qtdemo03/smallwidget.h new file mode 100644 index 0000000..6736f12 --- /dev/null +++ b/qtdemo03/smallwidget.h @@ -0,0 +1,25 @@ +#ifndef SMALLWIDGET_H +#define SMALLWIDGET_H + +#include +#include +#include // 滑块 +#include // 水平排列布局管理器 +#include // 垂直排列布局管理器 + +class SmallWidget : public QWidget +{ + Q_OBJECT + +private: + QSpinBox *spinBox; + QSlider *slider; + +public: + explicit SmallWidget(QWidget *parent = nullptr); + +signals: + +}; + +#endif // SMALLWIDGET_H