Qt dialog

This commit is contained in:
flykhan 2023-08-08 17:42:39 +08:00
parent f6d37a40a2
commit 1464145959
6 changed files with 243 additions and 1 deletions

View File

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

109
qtdemo02/mainwindow2.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "mainwindow2.h"
#include <QDebug>
#include <QFileDialog>
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;
});
}

26
qtdemo02/mainwindow2.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef MAINWINDOW2_H
#define MAINWINDOW2_H
#include <QMainWindow>
#include <QDialog>
#include <QTextEdit>
#include <QPushButton>
#include <QStatusBar>
#include <QMessageBox>
#include <QTextEdit>
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

72
qtdemo02/mainwindow3.cpp Normal file
View File

@ -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);
}

29
qtdemo02/mainwindow3.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef MAINWIDGET3_H
#define MAINWIDGET3_H
#include <QMainWindow>
#include <QDialog>
#include <QTextEdit>
#include <QPushButton>
#include <QStatusBar>
#include <QMessageBox>
#include <QTextEdit>
#include <QTextStream>
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QDebug>
class MainWindow3 : public QMainWindow
{
Q_OBJECT
QPushButton *btn1,*btn2,*btn3,*btn4,*btn5;
QTextEdit *mEdit;
public:
explicit MainWindow3(QWidget *parent = nullptr);
signals:
};
#endif // MAINWIDGET3_H

View File

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