Qt Creator module learning-dialog box

Qt Creator module learning-dialog box

remind

Before reading this chapter, if you don't understand signals and slots, it's best to first touch the signal and slot functions.
Click here : Signals and Slots

Modal and non-modal dialogs

Modal dialog: before closing the window, you cannot interact with other windows of the same application, such as opening files.
Non-modal dialog box: You can also interact with other windows of the same application before closing the window.
You can feel this code, put it in the constructor and the code is as follows:

 QMenuBar *mBar =menuBar();
    setMenuBar(mBar);
    QMenu *menu = mBar->addMenu("对话框");

    QAction *p1 = menu->addAction("模态对话框");
    connect(p1, &QAction::triggered,
            [=](){
    
    
        QDialog dlg;
        dlg.exec();
        qDebug()<<"模态对话框";
    });
    QAction *p2 = menu->addAction("非模态对话框");
    connect(p2, &QAction::triggered,
            [=](){
    
    
 /*QDialog dlg; 如果定义到这里会一闪而过,
 因为对话框生命周期到了,因此在类中对其定义*/
        dlg1.show();
        qDebug()<<"非模态对话框";
    });

Standard dialog

1. Color dialog

void MainWindow::on_pushButton_clicked()
{
    
    
    QColor color = QColorDialog::getColor(Qt::red,this,tr("颜色对话框"));
    qDebug()<<"color:"<<color;
}

2. Font dialog

void MainWindow::on_pushButton_2_clicked()
{
    
    
    bool ok;
    QFont font=QFontDialog::getFont(&ok,this);
    if(ok){
    
    
        ui->pushButton_2->setFont(font);
    }
    else{
    
    
        qDebug()<<tr("没有选择字体");
    }
}

3. Message dialog

void MainWindow::on_pushButton_3_clicked()
{
    
    
    //问题对话框
    int ret1 = QMessageBox::question(this,tr("问题对话框"),
                                     tr("你了解Qt吗?"),QMessageBox::Yes,QMessageBox::No);
    //提示对话框
    int ret2 = QMessageBox::information(this,tr("提示对话框"),
                                        tr("加油学习!"),QMessageBox::Ok);
    //警告对话框
    int ret3 = QMessageBox::warning(this,tr("警告对话框"),
                                    tr("不能摸这里!"),QMessageBox::Yes);
    //错误对话框
    int ret4 = QMessageBox::critical(this,tr("严重错误对话框"),
                                     tr("发现严重错误!!关闭所有文件!"),QMessageBox::YesAll);
    //关于对话框
     QMessageBox::about(this,tr("关于对话框"),tr("让我们化身为猿吧!"));
}

4. Input dialog

void MainWindow::on_pushButton_5_clicked()
{
    
    
    bool ok;
    //获取字符串
    QString str = QInputDialog::getText(this,tr("输入字符串对话框"),
                                        tr("请输入用户名:"),QLineEdit::Normal,tr("admin"),&ok);
    if(ok) qDebug()<<"String="<<str;
    //获取整数
    int value1 = QInputDialog::getInt(this,tr("输入整数对话框"),
                                       tr("请输入一个整数"),100,-1000,1000,10,&ok);
    if(ok) qDebug()<<"value1="<<value1;
    //获取浮点数
    int value2 = QInputDialog::getDouble(this,tr("输入小数对话框"),
                                       tr("请输入一个小数"),0.00,-1000,1000,2,&ok);
    if(ok) qDebug()<<"value2="<<value2;
    //获取条目
    QStringList items;
    items<<tr("条目1")<<tr("条目二");
    QString item = QInputDialog::getItem(this,tr("输入条目对话框"),
                                        tr("请选择或输入一个条目:"), items,0,true,&ok);
    if(ok) qDebug()<<"item="<<item;
}

5. Progress dialog

void MainWindow::on_pushButton_6_clicked()
{
    
    
    QProgressDialog dialog(tr("文件复制进度"),tr("取消"),
                           0,50000,this);
    dialog.setWindowTitle("进度对话框");
    dialog.setWindowModality(Qt::WindowModal);//将对话框定为模态
    dialog.show();
    for (int i=0;i<50000;i++) {
    
    
        dialog.setValue(i);//设置进度当前值
        QCoreApplication::processEvents();//避免界面冻结
        if(dialog.wasCanceled()) break;//按下取消键中断
    }
    dialog.setValue(50000);
    qDebug() << tr("复制结束!");
}

6. Error message dialog

void MainWindow::on_pushButton_7_clicked()
{
    
    
    errordlg = new QErrorMessage(this);
    errordlg->setWindowTitle(tr("错误信息对话框!"));
    errordlg->showMessage(tr("这里是出错信息!"));
}

7. Wizard dialog

//做页窗口
//每一个都要在类中定义
QWizardPage * MainWindow::createPage1()
{
    
    
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("介绍"));
    return page;
}
QWizardPage * MainWindow::createPage2()
{
    
    
    QWizardPage * page = new QWizardPage;
    page->setTitle("用户选择信息");
    return page;
}
QWizardPage * MainWindow::createPage3()
{
    
    
    QWizardPage * page = new QWizardPage;
    page->setTitle("结束");
    return page;
}
void MainWindow::on_pushButton_8_clicked()
{
    
    
    QWizard wizard(this);
    wizard.setWindowTitle("向导对话框");
    wizard.addPage(createPage1());
    wizard.addPage(createPage2());
    wizard.addPage(createPage3());
    wizard.exec();
}

8. File dialog box (there are many types of files, here is just an example, I will talk about the files in detail later)

void MainWindow::on_pushButton_4_clicked()
{
    
    
    QString filename = QFileDialog::getOpenFileName(this,tr("文件对话框"),
                                                   "D:",tr("图片文件(*png *jpg)"));
    qDebug()<<"filename="<<filename;
}

Guess you like

Origin blog.csdn.net/m0_50210478/article/details/108125542