代码拉取完成,页面将自动刷新
QT
几种标准对话框本文介绍几种标准对话框,都是 Qt
封装好的,我们先创建一个界面,添加几个按钮,然后分别在几个按钮的回调函数里添加创建不同对话框的逻辑
颜色对话框用来选择颜色,创建后会显示各种颜色和透明度信息
void MainWindow::on_color_clicked()
{
// QColorDialog colorDialog(Qt::blue, this);
// colorDialog.setOptions(QColorDialog::ShowAlphaChannel);
// colorDialog.exec();
// // QColor 用来输出 QColorDialog 选择了什么颜色的: QColor(ARGB 1, 0.313726, 0.34902, 1)
// QColor color = colorDialog.currentColor();
// qDebug() << "color is " << color << Qt::endl;
QColor color = QColorDialog::getColor(Qt::blue, this, tr("选择颜色"), QColorDialog::ShowAlphaChannel);
qDebug() << "color is " << color << Qt::endl;
}
注释中也是创建颜色对话框的一种方式,两种方式都可以。
创建一个选择文件的对话框,选择后,可以根据返回值打印选择的文件的路径信息
void MainWindow::on_file_clicked()
{
QString path = QDir::currentPath();
QString title = tr("文件对话框");
QString filter = tr("文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*。*)");
QString aFileName = QFileDialog::getOpenFileName(this, title, path, filter);
qDebug() << aFileName << Qt::endl;
}
输入对话框分几种,包括文本输入对话框,整数输入对话框,浮点数输入对话框,条目输入对话框。
先看看文本输入对话框
void MainWindow::on_input_clicked()
{
bool ok = false;
auto text = QInputDialog::getText(this, tr("文字输入对话框"), tr("请输入用户姓名"), QLineEdit::Normal, tr("admin"), &ok);
if(ok == true){
qDebug() << text << Qt::endl;
}
}
void MainWindow::on_dir_clicked()
{
bool ok = false;
auto intdata = QInputDialog::getInt(this, tr("数字输入对话框"), tr("请输入数字"), 200, 200, 400, 10, &ok);
if(ok == true){
qDebug() << intdata << Qt::endl;
}
}
void MainWindow::on_pushButton_clicked()
{
bool ok = false;
auto floatdata = QInputDialog::getDouble(this, tr("浮点数输入对话框"), tr("请输入浮点数"), 0.1, -1, 1, 2, &ok);
if(ok == true){
qDebug() << floatdata << Qt::endl;
}
}
QStringList
类似于 C++
中的 deque
,重载了 <<
运算符。
void MainWindow::on_pushButton_2_clicked()
{
QStringList items;
items << tr("条目1") << tr("条目2");
bool ok = false;
auto itemData = QInputDialog::getItem(this, tr("条目输入对话框"), tr("输入或选择条目"), items, 0, true, &ok);
if(ok == true){
qDebug() << "item is " << itemData << Qt::endl;
}
}
void MainWindow::on_pushButton_3_clicked()
{
auto ret = QMessageBox::question(this, tr("提问对话框"), tr("你是单身吗"), QMessageBox::Yes, QMessageBox::No);
if(ret == QMessageBox::Yes || ret == QMessageBox::No){
qDebug() << "ret is " << ret << Qt::endl;
}
auto ret2 = QMessageBox::information(this, tr("通知对话框"), tr("你是单身"), QMessageBox::Yes);
if(ret2 == QMessageBox::Yes){
qDebug() << "ret2 is " << ret2 << Qt::endl;
}
auto ret3 = QMessageBox::warning(this, tr("警告对话框"), tr("你最好找个地方发泄一下"), QMessageBox::Yes);
if(ret3 == QMessageBox::Yes){
qDebug() << "ret3 is " << ret3 << Qt::endl;
}
auto ret4 = QMessageBox::critical(this, tr("关键提示对话框"), tr("我梦寐以求是真爱和自由"), QMessageBox::Yes);
if(ret4 == QMessageBox::Yes){
qDebug() << "ret4 is " << ret4 << Qt::endl;
}
}
当我们复制或进行文件传输时,会显示一个进度条的对话框,我们可以使用 QProcessDialog
来操作。
void MainWindow::on_pushButton_4_clicked()
{
// 进度提示框一闪而逝,不是逻辑问题,是 5000 量级太小了
QProgressDialog progressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
progressDialog.setWindowTitle(tr("文件复制进度对话框"));
progressDialog.setWindowModality(Qt::ApplicationModal);
progressDialog.show();
for(int i = 0; i < 5000; i++){
progressDialog.setValue(i);
QApplication::processEvents();
if(progressDialog.wasCanceled() == true){
break;
}
}
progressDialog.setValue(5000);
}
因为循环很快完成,所以进度条一闪而逝,那么我们可以通过定时器的方式定时更新进度,这样就可以更清楚的看到进度条对话框更新情况了
void MainWindow::on_pushButton_4_clicked()
{
/*
// 进度提示框一闪而逝,不是逻辑问题,是 5000 量级太小了
QProgressDialog progressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
progressDialog.setWindowTitle(tr("文件复制进度对话框"));
progressDialog.setWindowModality(Qt::ApplicationModal);
progressDialog.show();
for(int i = 0; i < 5000; i++){
progressDialog.setValue(i);
QApplication::processEvents();
if(progressDialog.wasCanceled() == true){
break;
}
}
progressDialog.setValue(5000);
*/
// 因为循环很快完成,所以进度条一闪而逝,那么我们可以通过定时器的方式定时更新进度,这样就可以更清楚的看到进度条对话框更新情况了
progressDialog_ = new QProgressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
progressDialog_->setWindowTitle(tr("文件复制进度对话框"));
progressDialog_->setWindowModality(Qt::ApplicationModal);
timer_ = new QTimer(this);
connect(timer_, &QTimer::timeout, this, &MainWindow::on_updateProgressDialog);
connect(progressDialog_, &QProgressDialog::canceled, this, &MainWindow::on_cancelProgressDialog);
timer_->start(2);
qDebug("复制结束");
}
上面代码连接了定时器的 timeout
信号和主窗口的 on_updateProgressDialog
函数, 该函数的主要作用就是定时更新进度条,逻辑如下
void MainWindow::on_updateProgressDialog()
{
count_++;
if(count_ > 5000){
timer_->stop();
delete timer_;
timer_ = nullptr;
delete progressDialog_;
progressDialog_ = nullptr;
count_ = 0;
return;
}
progressDialog_->setValue(count_);
}
另外我们需要在进度条满了的时候发消息通知主窗口关闭
void MainWindow::on_cancelProgressDialog(){
timer_->stop();
delete timer_;
timer_ = nullptr;
delete progressDialog_;
progressDialog_ = nullptr;
count_ = 0;
return;
}
这样进度条满了之后我们就可以回收定时器资源了。
安装软件的时候一般会看到这样的场景。
当我们想引导用户操作时可以采用向导对话框,比如安装软件时就会根据向导对话框一步一步操作,我们也实现一个向导对话框
void MainWindow::on_pushButton_6_clicked()
{
QWizard wizard(this);
wizard.setWindowTitle(tr("全城热恋"));
QWizardPage* page1 = new QWizardPage();
page1->setTitle(tr("婚恋介绍引导程序"));
auto label1 = new QLabel();
label1->setText(tr("该程序帮助您找到人生伴侣"));
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(label1);
page1->setLayout(layout);
wizard.addPage(page1);
QWizardPage* page2 = new QWizardPage();
page2->setTitle("选择心动类型");
QButtonGroup *group = new QButtonGroup(page2);
QRadioButton * btn1 = new QRadioButton();
btn1->setText("白富美");
group->addButton(btn1);
QRadioButton * btn2 = new QRadioButton();
btn2->setText("萝莉");
group->addButton(btn2);
QRadioButton * btn3 = new QRadioButton();
btn3->setText("御姐");
group->addButton(btn3);
QRadioButton * btn4 = new QRadioButton();
btn4->setText("小家碧玉");
group->addButton(btn4);
QRadioButton * btn5 = new QRadioButton();
btn5->setText("女汉子");
group->addButton(btn5);
QRadioButton * btn6 = new QRadioButton();
btn6->setText("成年人不做选择,全选!");
group->addButton(btn6);
QVBoxLayout *vboxLayout2 = new QVBoxLayout();
for(int i = 0; i < group->buttons().size(); i++){
vboxLayout2->addWidget(group->buttons()[i]);
}
page2->setLayout(vboxLayout2);
wizard.addPage(page2);
QWizardPage* page3 = new QWizardPage();
page3->setTitle(tr("你的缘分即将到来"));
auto label3 = new QLabel();
label3->setText(tr("感谢您的参与,接下来的一个月会遇到对的人"));
QVBoxLayout *layout3 = new QVBoxLayout();
layout3->addWidget(label3);
page3->setLayout(layout3);
wizard.addPage(page3);
wizard.show();
wizard.exec();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。