From 14641459598b2b62b62bd92689688289f8594b1a Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 8 Aug 2023 17:42:39 +0800 Subject: [PATCH] Qt dialog --- qtdemo02/main.cpp | 4 +- qtdemo02/mainwindow2.cpp | 109 +++++++++++++++++++++++++++++++++++++++ qtdemo02/mainwindow2.h | 26 ++++++++++ qtdemo02/mainwindow3.cpp | 72 ++++++++++++++++++++++++++ qtdemo02/mainwindow3.h | 29 +++++++++++ qtdemo02/qtdemo02.pro | 4 ++ 6 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 qtdemo02/mainwindow2.cpp create mode 100644 qtdemo02/mainwindow2.h create mode 100644 qtdemo02/mainwindow3.cpp create mode 100644 qtdemo02/mainwindow3.h diff --git a/qtdemo02/main.cpp b/qtdemo02/main.cpp index fd3e533..2941b97 100644 --- a/qtdemo02/main.cpp +++ b/qtdemo02/main.cpp @@ -1,11 +1,13 @@ #include "mainwindow.h" +#include "mainwindow2.h" +#include "mainwindow3.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; + MainWindow3 w; w.show(); return a.exec(); } diff --git a/qtdemo02/mainwindow2.cpp b/qtdemo02/mainwindow2.cpp new file mode 100644 index 0000000..8b57410 --- /dev/null +++ b/qtdemo02/mainwindow2.cpp @@ -0,0 +1,109 @@ +#include "mainwindow2.h" +#include +#include + +MainWindow2::MainWindow2(QWidget *parent) : QMainWindow(parent) +{ + setWindowTitle("对话框学习中..."); + setWindowIcon(QIcon(":/icon/open")); + resize(1200,960); + + mEdit = new QTextEdit(this); + mEdit->close(); + setCentralWidget(mEdit); + + setCentralWidget(new QTextEdit(this)); + QStatusBar *status_bar = new QStatusBar(this); + btn1 = new QPushButton("按钮1",status_bar); + btn2 = new QPushButton("按钮2",status_bar); + btn3 = new QPushButton("按钮3",status_bar); + btn4 = new QPushButton("定制MessageBox",status_bar); + btn5 = new QPushButton("button5",status_bar); + status_bar->addWidget(btn1); + status_bar->addWidget(btn2); + status_bar->addWidget(btn3); + status_bar->addWidget(btn4); + status_bar->addWidget(btn5); + setStatusBar(status_bar); + + connect(btn1,&QPushButton::clicked,[&](){ + QDialog dialog; + dialog.setWindowTitle(tr("自定义对话框 %1").arg(1)); + // dialog.exec(); // 应用级别的模态对话框 + dialog.show(); + }); + // connect(btn2,&QPushButton::clicked,[&](){ + // QDialog *dialog = new QDialog(this); + // dialog->setWindowTitle(tr("自定义对话框 %1").arg(2)); + // dialog->setAttribute(Qt::WA_DeleteOnClose); // 当关闭对话时,自动释放堆空间 + // dialog->open(); // 窗口级别的模态对话框 + + // }); + // connect(btn3,&QPushButton::clicked,[&](){ + // QDialog *dialog = new QDialog(this); + // dialog->setWindowTitle(tr("自定义对话框 %1").arg(3)); + // dialog->setAttribute(Qt::WA_DeleteOnClose); // 当关闭对话时,自动释放堆空间 + // dialog->show(); // 窗口级别的非模态对话框 + // }); + + + + connect(btn2,&QPushButton::clicked,[&](){ + QMessageBox::warning(this,"警告","你的操作可能会失败..."); + }); + connect(btn3,&QPushButton::clicked,[&](){ + // QMessageBox::StandardButton ret; + // ret = QMessageBox::question(this,"询问","选什么内容?"); + // if(ret == QMessageBox::Yes) + // qDebug()<<"选了行"; + // else + // qDebug()<<"没选行"; + // QMessageBox::critical(this,"警告","太警告了"); + QMessageBox::information(this,"info","no info",QMessageBox::NoButton,QMessageBox::Apply); + }); + + connect(btn4,&QPushButton::clicked,[&](){ + QMessageBox *msgBox = new QMessageBox(this); + msgBox->setAttribute(Qt::WA_DeleteOnClose); + + // msgBox->setIcon(QMessageBox::Critical); + // msgBox->setWindowTitle("自定义错误提示"); + msgBox->setText("Error"); + msgBox->setInformativeText("An error occurred while processing the request."); + msgBox->setStandardButtons(QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel); + msgBox->setDefaultButton(QMessageBox::Cancel); + int ret = msgBox->exec(); // 会返回一个 int + switch (ret) { + case QMessageBox::Ok: + qDebug()<<"Ok"; + break; + case QMessageBox::Yes: + qDebug()<<"Yes"; + break; + case QMessageBox::No: + qDebug()<<"No"; + break; + } + }); + + connect(btn5,&QPushButton::clicked,[&](){ + + // if(QMessageBox::Yes == QMessageBox::question( + // this, + // tr("Question"), + // tr("Are you OK?"), + // QMessageBox::Yes|QMessageBox::No, + // QMessageBox::Yes)) + // { + // QMessageBox::information(this,tr("Hmmm..."), + // tr("I'm glad to hear that!")); + // } + // else{ + // QMessageBox::information(this, + // tr("Hmmm..."), + // tr("I'm sorry!")); + // } + + QMessageBox msgBox; + }); +} diff --git a/qtdemo02/mainwindow2.h b/qtdemo02/mainwindow2.h new file mode 100644 index 0000000..1d007b4 --- /dev/null +++ b/qtdemo02/mainwindow2.h @@ -0,0 +1,26 @@ +#ifndef MAINWINDOW2_H +#define MAINWINDOW2_H + +#include +#include +#include +#include +#include +#include +#include + +class MainWindow2 : public QMainWindow +{ + Q_OBJECT + QPushButton *btn1,*btn2,*btn3,*btn4,*btn5; + QTextEdit *mEdit; +public: + explicit MainWindow2(QWidget *parent = nullptr); + +public: + +signals: + +}; + +#endif // MAINWINDOW2_H diff --git a/qtdemo02/mainwindow3.cpp b/qtdemo02/mainwindow3.cpp new file mode 100644 index 0000000..ea3c482 --- /dev/null +++ b/qtdemo02/mainwindow3.cpp @@ -0,0 +1,72 @@ +#include "mainwindow3.h" + +MainWindow3::MainWindow3(QWidget *parent) : QMainWindow(parent) +{ + setWindowTitle("对话框学习中..."); + setWindowIcon(QIcon(":/icon/start")); + resize(1200, 960); + + mEdit = new QTextEdit(this); + //mEdit->close(); + setCentralWidget(mEdit); + + + QStatusBar *bar = new QStatusBar(this); + btn1 = new QPushButton("打开文本文件", bar); + btn2 = new QPushButton("保存文本文件", bar); + btn3 = new QPushButton("颜色对话框", bar); + btn4 = new QPushButton("字体对话框", bar); + bar->addWidget(btn1); + bar->addWidget(btn2); + bar->addWidget(btn3); + bar->addWidget(btn4); + + connect(btn1, &QPushButton::clicked, [&](){ + // 获取待打开的文件名 (已存在的文件) + // QString fileName = QFileDialog::getOpenFileName(this, "选择图片文件", ".", "Png(*.png);;Jpeg(*.jpg)"); + QString fileName = QFileDialog::getOpenFileName(this, "选择文本文件", ".", "Text(*.txt)"); + qDebug() << "打开文件的位置:" << fileName ; + QFile file(fileName); + if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){ + // 以只读文本的模式打开文件失败 + QMessageBox::critical(this, "错误", fileName+" 打开失败!"); + return; + } + + QTextStream in(&file); + mEdit->setText(in.readAll()); + file.close(); + }); + + connect(btn2, &QPushButton::clicked, [&](){ + // 保存文件时,获取待保存的文件名 (可以是新文件) + // QString fileName = QFileDialog::getSaveFileName(this,"保存文件位置","."); + QString fileName = QFileDialog::getSaveFileName(this,"保存文件位置","d:/", "Text(*.txt)"); + qDebug() << "保存文件的位置: " << fileName ; + QFile file(fileName); + if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){ + QMessageBox::critical(this, "错误", fileName+" 保存失败!"); + return; + } + QTextStream out(&file); + // 将文本编辑控件 mEdit 中的文本写入到文件中。 + out << mEdit->toPlainText(); // 调用 mEdit->toPlainText() 将返回编辑控件中的纯文本内容 + file.close(); + }); + + connect(btn3, &QPushButton::clicked, [&](){ + QColor color = QColorDialog::getColor(Qt::black,this,"字体颜色选择"); + + mEdit->setTextBackgroundColor(color); +// mEdit->setTextColor(color); // 条件: 光标选中的文本来设置颜色 + }); + + connect(btn4, &QPushButton::clicked, [&](){ + bool yes = true; + QFont font = QFontDialog::getFont(&yes,this); + mEdit->setCurrentFont(font); + + }); + + setStatusBar(bar); +} diff --git a/qtdemo02/mainwindow3.h b/qtdemo02/mainwindow3.h new file mode 100644 index 0000000..9797a25 --- /dev/null +++ b/qtdemo02/mainwindow3.h @@ -0,0 +1,29 @@ +#ifndef MAINWIDGET3_H +#define MAINWIDGET3_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class MainWindow3 : public QMainWindow +{ + Q_OBJECT + QPushButton *btn1,*btn2,*btn3,*btn4,*btn5; + QTextEdit *mEdit; +public: + explicit MainWindow3(QWidget *parent = nullptr); + +signals: + +}; + +#endif // MAINWIDGET3_H diff --git a/qtdemo02/qtdemo02.pro b/qtdemo02/qtdemo02.pro index d958105..33a97ec 100644 --- a/qtdemo02/qtdemo02.pro +++ b/qtdemo02/qtdemo02.pro @@ -18,10 +18,14 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp \ + mainwindow2.cpp \ + mainwindow3.cpp \ widget1.cpp HEADERS += \ mainwindow.h \ + mainwindow2.h \ + mainwindow3.h \ widget1.h # Default rules for deployment.