qt中的一些对话框(个人备忘录)
一、标准对话框
1.对于颜色对话框
void MyWidget::on_pushButton_clicked() { QColorDialog dialog(Qt::red,this); dialog.setOption(QColorDialog::ShowAlphaChannel);//显示alpha选项 dialog.exec(); QColor color=dialog.currentColor();//获取颜色 qDebug()<<"color:"<<color; }
2.对于文件对话框
void MyWidget::on_pushButton_2_clicked() { QString fileName=QFileDialog::getOpenFileName(this,tr("文件对话框"),"D:",tr("图片文件(*png *jpg")); qDebug()<<"fileName:"<<fileName; }
3.字体对话框
void MyWidget::on_pushButton_3_clicked() { bool ok; QFont font=QFontDialog::getFont(&ok,this); if(ok) { ui->pushButton_2->setFont(font); } else { qDebug()<<tr("没有你选择的字体"); } }
4.对于输入对话框
void MyWidget::on_pushButton_4_clicked() { bool ok; QString string=QInputDialog::getText(this,tr("输入字符对话框"),tr("请输入用户名:"),QLineEdit::Normal,tr("admin"),&ok); if(ok) { qDebug()<<"string:"<<string; } int value1=QInputDialog::getInt(this,tr("输入整数对话框"),tr("输入-1000到1000之间的数值"),100,-1000,1000,10,&ok); if(ok) { qDebug()<<"value1:"<<value1; } int value2=QInputDialog::getDouble(this,tr("输入浮点数对话框"),tr("输入-1000到1000之间的数值"),0.00,-1000,1000,2,&ok); if(ok) { qDebug()<<"value2:"<<value2; } QStringList items; items<<tr("条目1")<<tr("条目2"); QString item=QInputDialog::getItem(this,tr("输入条目对话框"),tr("请选择或输入一个条目"),items,0,true,&ok); if(ok) { qDebug()<<"item:"<<item; } }
5.消息对话框
void MyWidget::on_pushButton_5_clicked() { int ret1=QMessageBox::question(this,tr("问题对话框"),tr("你了解qt吗"),QMessageBox::Yes,QMessageBox::No); if(ret1==QMessageBox::Yes)qDebug()<<tr("问题!"); int ret2=QMessageBox::information(this,tr("提示对话框"),tr("这是Qt书籍!"),QMessageBox::Ok); if(ret2==QMessageBox::Ok) qDebug()<<tr("提示!"); int ret3=QMessageBox::warning(this,tr("警告对话框"),tr("不能提前结束"),QMessageBox::Abort); if(ret3==QMessageBox::Abort)qDebug()<<tr("警告"); int ret4=QMessageBox::critical(this,tr("严重错误对话框"),tr("发现一个严重的错误!,现在我要关闭所有的文件!"),QMessageBox::YesAll); if(ret4==QMessageBox::YesAll) qDebug()<<tr("错误"); QMessageBox::about(this,tr("关于对话框"),tr("yafeililnux致力于Qt以及QT的普及工作")); }
6.进度条对话框
void MyWidget::on_pushButton_6_clicked() { QProgressDialog dialog(tr("文件复制框"),tr("取消"),0,50000,this); dialog.setWindowTitle(tr("进度对话框")); 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("复制结束"); }
7.错误对话框
8.向导对话框
QWizardPage * MyWidget::createPage1() { QWizardPage * page=new QWizardPage; page->setTitle("介绍"); return page; } QWizardPage * MyWidget::createPage2() { QWizardPage * page=new QWizardPage; page->setTitle("用户选择信息"); return page; } QWizardPage * MyWidget::createPage3() { QWizardPage * page=new QWizardPage; page->setTitle("结束"); return page; } void MyWidget::on_pushButton_8_clicked() { QWizard wizard(this); wizard.setWindowTitle(tr("向导对话框")); wizard.addPage(createPage1()); wizard.addPage(createPage2()); wizard.addPage(createPage3()); wizard.exec(); }