Qt快速入门第三版章节3.2.3标准对话框练习

1.颜色对话框
代码中Qt::red在F1查看快捷键的截图显示:


使用QColorDialog类的静态函数来直接显示颜色对话框,其好处是不用创建对象,但是如果想要更灵活的设置,则可以先创建对象,然后进行各项设置


在设计模式中选择“颜色对话框”按钮鼠标右击按钮的clicked()单击信号槽。然后跳转到mywidget.cpp文件中进行相应的编辑,在后面实现其他的对话框中可重复此步骤中,可实现同样的效果。
在mywidget.cpp文件中添加
修改前:
void MyWidget::on_pushButton_clicked()
{
QColor color = QColorDialog::getColor( Qt:: red, this,tr( "颜色对话框"),
QColorDialog:: ShowAlphaChannel);
qDebug()<< "color: "<<color;
}

修改后:
void MyWidget::on_pushButton_clicked()
{
QColorDialog dialog( Qt:: red, this);
dialog.setOption( QColorDialog:: ShowAlphaChannel);
dialog. exec();
QColor color = dialog.currentColor();
qDebug()<< "color: "<<color;
}
运行截图:

2文件对话框
在mywidget.cpp文件中添加
#include <QFileDialog>
void MyWidget::on_pushButton_5_clicked()
{
QString fileName = QFileDialog::getOpenFileName( this,tr( "文件对话框"), "F:",tr( "图片文件(*png *jpg);;文本文件(*txt)"));
qDebug()<< "fileName:"<<fileName;
}




3字体对话框
在mywidget.cpp文件中添加
#include <QFontDialog>
void MyWidget::on_pushButton_2_clicked()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, this);
if(ok) ui-> pushButton_2->setFont(font);
else qDebug()<<tr( "没有选择字体");
}


4输入对话框
在mywidget.cpp文件中添加
#include <QInputDialog>
void MyWidget::on_pushButton_6_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之间的数值"), 0.00,- 1000, 1000, 2,&ok);
if(ok) qDebug()<< "value1:"<<value1;
double 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消息对话框
在mywidget.cpp文件中添加
#include <QMessageBox>
void MyWidget::on_pushButton_3_clicked()
{
int ret1 = QMessageBox::question( this,tr( "问题对话框"),
tr( "你了解Qt吗?"), QMessageBox:: Yes, QMessageBox:: No);
if(ret1 == QMessageBox:: Yes) qDebug()<<tr( "问题1");
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( "yafeilinux致力于Qt及Qt Creator的普及工作!") );
}










6进度对话框
在mywidget.cpp文件中添加
#include <QProgressDialog>
void MyWidget::on_pushButton_7_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错误信息对话框
mywidget.h头文件中添加
#include <QErrorMessage>
下面的代码在书本上描述的有点狗血,让我这种智商捉急的人找了半天,所以直接截图显示。(建议参照源码编写即可)





void MyWidget::on_pushButton_4_clicked()
{
errordlg->setWindowTitle(tr( "错误信息对话框") );
errordlg->showMessage(tr( "这里是出错信息") );
}

运行截图



8向导对话框:
mywidget.h头文件中添加
#include <QWizard>
在mywidget.h头文件中的 MyWidget类 中添加私有属性成员函数:
QWizardPage *createPage1();
QWizardPage *createPage2();
QWizardPage *createPage3();

在mywidget.cpp文件中添加
QWizardPage * MyWidget::createPage1()
{
QWizardPage *page = new QWizardPage;
page->setTitle( tr( "介绍") );
return page;
}

QWizardPage * MyWidget::createPage2()
{
QWizardPage *page = new QWizardPage;
page->setTitle( tr( "用户选择信息") );
return page;
}

QWizardPage * MyWidget::createPage3()
{
QWizardPage *page = new QWizardPage;
page->setTitle( tr( "结束") );
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();
}










以上就是对Qt标准对话框的练习,不足之处还请大家指导。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值