本文主要是介绍字体对话框QFontDialog、消息对话框 QMessageBox和输入对话框 QInputDialog,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
界面如下
1. 字体对话框 QFontDialog
1.1 基本函数
QFont getFont(bool *ok, const QFont &initial, QWidget *parent = nullptr, const QString &title = QString(), QFontDialog::FontDialogOptions options = FontDialogOptions())
返回值:QFont类型--字体
参数1:如果字体设置成功,ok返回true,否则返回false
参数2:设置初始字体
参数3:指定父对象
参数4:指定对话框标题
QFont getFont(bool *ok, QWidget *parent = nullptr)
返回值:QFont类型--字体
参数1:如果字体设置成功,ok返回true,否则返回false
参数2:指定父对象
1.2 示例
QDialog
dialog.cpp
//字体对话框
void Dialog::on_pushButton_clicked()
{bool ok = false;//选择字体等信息,选择完后后保存在fontQFont font = QFontDialog::getFont(&ok,this);if(ok){qDebug()<<"字体设置成功"<<endl;}//这里可以将我们上次保存的font里的字体信息,作为再次设置字体的默认值
// QFontDialog::getFont(&ok,font,this,"字体对话框");
}
点击
2. 消息对话框 QMessageBox
2.1 基本函数
关于类型
void about(QWidget *parent, const QString &title, const QString &text) //about关于
void aboutQt(QWidget *parent, const QString &title = QString())
有争议类型
QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) //critical有争议
信息提示类型
QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) //信息提示
返回值:QMessageBox::StandardButton返回按钮
参数1:指定父对象
参数2:指定标题
参数3:指定提示的文本
参数4:指定对话框中的按钮
参数5:设置默认按钮
有疑问类型
QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)//有疑问
警告类型
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) //警告
2.2 示例
参数4 指定对话框中的按钮 的参数
QMessageBox::Ok An"OK" button defined with the AcceptRole.
QMessageBox::Open An "Open" button defined with the AcceptRole.
QMessageBox::Save A "Save" button defined with the AcceptRole.
QMessageBox::Cancel A "Cancel" button defined with the RejectRole.
QMessageBox::Close A "Close" button defined with the RejectRole.
QMessageBox::Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
QMessageBox::Apply An "Apply" button defined with the ApplyRole.
QMessageBox::Reset A "Reset" button defined with the ResetRole.
QMessageBox::RestoreDefaults A "Restore Defaults" button defined with the ResetRole.
QMessageBox::Help A "Help" button defined with the HelpRole.
QMessageBox::SaveAll A "Save All" button defined with the AcceptRole.
QMessageBox::Yes A "Yes" button defined with the YesRole.
QMessageBox::YesToAll A "Yes to All" button defined with the YesRole.
QMessageBox::No A "No" button defined with the NoRole.
QMessageBox::NoToAll A "No to All" button defined with the NoRole.
QMessageBox::Abort An "Abort" button defined with the RejectRole.
QMessageBox::Retry A "Retry" button defined with the AcceptRole.
QMessageBox::Ignore An "Ignore" button defined with the AcceptRole.
QMessageBox::NoButton An invalid button.
QDialog
dialog.cpp
//消息对话框
void Dialog::on_pushButton_2_clicked()
{//弹出提示框,提示一些关于qt的东西
// QMessageBox::aboutQt(this,"关于Qt的一些基本信息");//信息提示类型
// QMessageBox::StandardButton btn=QMessageBox::information(this,"information","今天是星期五",QMessageBox::Ok|QMessageBox::Close);
// if(btn==QMessageBox::Ok)
// {
// qDebug()<<"用户已经知晓"<<endl;// }
// else if(btn==QMessageBox::Close)
// {
// qDebug()<<"用户关闭"<<endl;
// }//问题类型QMessageBox::StandardButton btn=QMessageBox::question(this,"question","你今天开心吗?",QMessageBox::Ok|QMessageBox::Close);if(btn==QMessageBox::Ok){qDebug()<<"我开心"<<endl;}else if(btn==QMessageBox::Close){qDebug()<<"不开心"<<endl;}}
3. 输入对话框 QInputDialog
3.1 基本函数
输入double类型
double getDouble(QWidget *parent, const QString &title, const QString &label, double value = 0, double min = -2147483647, double max = 2147483647, int decimals = 1, bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(), double step = 1)
返回值:double,获取double类型的数据
参数1:指定父对象
参数2:指定标题
参数3:设置标签(提示的文本)
参数4:设置输入对话框显示的当前值
参数5:设置最小值
参数6:设置最大值
参数7:设置小数位数
参数8:设置输入是否成功的标志
参数9:设置window的标志
参数10:设置步进值
输入intleixing
int getInt(QWidget *parent, const QString &title, const QString &label, int value = 0, int min = -2147483647, int max = 2147483647, int step = 1, bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
返回值:int 获取int 类型的数据
参数1:指定父对象
参数2:指定标题
参数3:设置标签(提示的文本)
参数4:设置输入对话框显示的当前值
参数5:设置最小值
参数6:设置最大值
参数7:设置步进值
参数8:设置输入是否成功的标志
参数9:设置window的标志
一些其他的类型的 函数原型
QString getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &items, int current = 0, bool editable = true, bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)QString getMultiLineText(QWidget *parent, const QString &title, const QString &label, const QString &text = QString(), bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString &text = QString(), bool *ok = nullptr, Qt::WindowFlags flags = Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
3.2 示例
QDialog
dialog.cpp
//输入对话框
void Dialog::on_pushButton_3_clicked()
{bool ok=false;//输入double类型的数据double value=QInputDialog::getDouble(this,"输入对话框","请输入double类型的数据:",0.00,0.00,100.00,2,&ok,Qt::WindowFlags(),0.01);if(ok){qDebug()<<"value="<<value<<endl;}//上面的getDoble函数有的参数有默认参数,可以不写直接省略,如下
// double value = QInputDialog::getDouble(this,"输入对话框","请输入double类型");// qDebug()<<"value="<<value<<endl;}
这篇关于字体对话框QFontDialog、消息对话框 QMessageBox和输入对话框 QInputDialog的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!