[C++] 纯文本查看 复制代码
//头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QWizard>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_color_clicked();
void on_pushButton_file_clicked();
void on_pushButton_font_clicked();
void on_pushButton_input_clicked();
void on_pushButton_msg_clicked();
void on_pushButton_progress_clicked();
void on_pushButton_error_clicked();
void on_pushButton_guide_clicked();
private:
Ui::Widget *ui;
QWizardPage *createPage1(QString strTitle);
QWizardPage *createPage2(QString strTitle);
QWizardPage *createPage3(QString strTitle);
private:
QWizard *m_pQWizard;
};
#endif // WIDGET_H
//源文件
#include "widget.h"
#include "ui_widget.h"
#include <QColorDialog>
#include <QDebug>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressDialog>
#include <QCoreApplication>
#include <QErrorMessage>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, m_pQWizard(new QWizard(this))
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
//设置颜色对话框
void Widget::on_pushButton_color_clicked()
{
QColor color = QColorDialog::getColor(Qt::red, this, QStringLiteral("颜色对话框"), QColorDialog::ShowAlphaChannel);
qDebug() << "color:" << color;
}
//设置文件对话框
void Widget::on_pushButton_file_clicked()
{
QString strFile = QFileDialog::getOpenFileName(this, QStringLiteral("文件对话框"), NULL, QStringLiteral("文本文件(*txt);;系统文件(*dll *sys)"));
qDebug() << "fileName:" << strFile;
}
//设置字体对话框
void Widget::on_pushButton_font_clicked()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, this);
if (ok)
ui->pushButton_font->setFont(font);
else
qDebug() << QStringLiteral("没有选中字体");
}
//输入对话框
void Widget::on_pushButton_input_clicked()
{
bool ok;
//输入字符串
QString strInput = QInputDialog::getText(this, QStringLiteral("输入对话框"), QStringLiteral("请输入字符串:"),
QLineEdit::Normal, QStringLiteral("Admin"), &ok);
if (ok)
qDebug() << "strInput:" << strInput;
//输入整数
int nInput = QInputDialog::getInt(this, QStringLiteral("输入对话框"), QStringLiteral("请输入整数:"),
250, 0, 1000, 100, &ok);
if (ok)
qDebug() << "nInput:" << nInput;
//输入小数
double lfInput = QInputDialog::getDouble(this, QStringLiteral("输入对话框"), QStringLiteral("请输入小数:"),
250, 0, 1000, 100, &ok);
if (ok)
qDebug() << "lfInput:" << lfInput;
QStringList items;
//输入条目数
QString strItems = QInputDialog::getItem(this, QStringLiteral("输入对话框"), QStringLiteral("请输入条目数:"),
items, 0, true, &ok);
if (ok)
qDebug() << "strItems:" << strItems;
}
//消息对话框
void Widget::on_pushButton_msg_clicked()
{
int ret;
ret = QMessageBox::question(this, QStringLiteral("问题对话框"), QStringLiteral("你了解 Qt吗?"));
if (ret == QMessageBox::Yes)
qDebug() << QStringLiteral("问题");
ret = QMessageBox::information(this, QStringLiteral("提示对话框"), QStringLiteral("这是Qt的书籍"));
if (ret == QMessageBox::Ok)
qDebug() << QStringLiteral("提示");
ret = QMessageBox::warning(this, QStringLiteral("警告对话框"), QStringLiteral("发出警告"));
if (ret == QMessageBox::Ok)
qDebug() << QStringLiteral("警告");
ret = QMessageBox::critical(this, QStringLiteral("严重错误对话框"), QStringLiteral("严重错误"));
if (ret == QMessageBox::Ok)
qDebug() << QStringLiteral("严重错误");
QMessageBox::about(this, QStringLiteral("关于对话框"), QStringLiteral("关于Qt"));
}
//进度对话框
void Widget::on_pushButton_progress_clicked()
{
QProgressDialog progess(QStringLiteral("文件复制进度"), QStringLiteral("取消"), 0, 50000, this);
progess.setWindowTitle(QStringLiteral("进度对话框"));
progess.setWindowModality(Qt::WindowModal); //设为阻塞对话框
progess.show();
//模拟文件复制进度
for (int i = 0; i < 50000; i++)
{
progess.setValue(i);
// Processes all pending events for the calling thread according to the specified flags until there are no more events to process.
// You can call this function occasionally when your program is busy performing a long operation (e.g. copying a file).
// In the event that you are running a local loop which calls this function continuously,
// without an event loop, the DeferredDelete events will not be processed.
// This can affect the behaviour of widgets, e.g. QToolTip, that rely on DeferredDelete events to function properly.
// An alternative would be to call sendPostedEvents() from within that local loop.
// Calling this function processes events only for the calling thread.
// Note: This function is thread-safe.
// 根据指定的标志 处理调用该函数线程的所有等待事件 直到呢没有事件要处理为止
// 当你的程序执行一段很耗时的操作时, 偶尔呢可以调用这个函数去解决
// 如果你不停地在局部循环中调用该函数的话, 如果没有事件,那么不会处理DeferredDelete事件
// 它会影响widgets, e.g QToolTip的行为
// 它依靠DeferredDelete事件才能正确地执行
// 该函数仅针对调用线程有效
// 另一种方法是从本地循环中调用sendPostedEvents
// 该函数是线程安全的
QCoreApplication::processEvents(); //我们说在ui线程中执行耗时操作的话会卡死ui线程
//为了防止ui线程卡死 让该函数去处理当前线程的所有等待事件
//等待事件?当前不就是在for中吗 当然是执行for中的相关等待事件啦
if (progess.wasCanceled())
break;
}
progess.setValue(50000); //49999 需要再设置一次
qDebug() << QStringLiteral("文件复制完成!");
}
//错误信息对话框
void Widget::on_pushButton_error_clicked()
{
QErrorMessage *pError = new QErrorMessage(this);
if (pError == NULL)
return;
pError->setWindowTitle(QStringLiteral("错误信息对话框"));
pError->showMessage(QStringLiteral("显示一个错误信息"));
}
QWizardPage *Widget::createPage1(QString strTitle)
{
QWizardPage *pQWizardPage = new QWizardPage(this->m_pQWizard);
pQWizardPage->setTitle(strTitle);
return pQWizardPage;
}
QWizardPage *Widget::createPage2(QString strTitle)
{
QWizardPage *pQWizardPage = new QWizardPage(this->m_pQWizard);
pQWizardPage->setTitle(strTitle);
return pQWizardPage;
}
QWizardPage *Widget::createPage3(QString strTitle)
{
QWizardPage *pQWizardPage = new QWizardPage(this->m_pQWizard);
pQWizardPage->setTitle(strTitle);
return pQWizardPage;
}
//向导对话框
void Widget::on_pushButton_guide_clicked()
{
this->m_pQWizard->addPage(this->createPage1(QStringLiteral("介绍")));
this->m_pQWizard->addPage(this->createPage2(QStringLiteral("用户选择信息")));
this->m_pQWizard->addPage(this->createPage3(QStringLiteral("结束")));
this->m_pQWizard->exec(); //接收处理事件
}