网站后台无ftp,一个人做网站 优帮云,ai简历在线制作,乐山的网站建设公司一、前言 QMessageBox类继承于QDialog#xff0c;是一个模式对话框#xff0c;常用于通知用户或向用户提出问题并接收答案。 对话框QDialog QMessageBox消息框主要由四部分组成#xff0c;一个主要文本text#xff0c;用于提醒用户注意某种情况;一个信息文本informativeTex…一、前言 QMessageBox类继承于QDialog是一个模式对话框常用于通知用户或向用户提出问题并接收答案。 对话框QDialog QMessageBox消息框主要由四部分组成一个主要文本text用于提醒用户注意某种情况;一个信息文本informativeText用于进一步解释警报或向用户询问问题;一个可选的详细文本detailedText用于在用户请求时提供更多数据还有就是用于接受用户响应的图标和标准按钮。
QMessageBox msgBox;
msgBox.setWindowTitle(QMessageBox);
msgBox.setText(The document has been modified.);
msgBox.setInformativeText(Do you want to save your changes?);
msgBox.setDetailedText(The document may have been modified externally);
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret msgBox.exec(); QMessageBox提供一些静态的API(information(), question(), warning(), critical())可以直接打开对话框根据API自带不同的状态图标方便我们简单快速的使用但是提供参数比较少只有标题、主要文本和按钮没有实例化的灵活。
int ret QMessageBox::warning(this, tr(My Application),tr(The document has been modified.\nDo you want to save your changes?),QMessageBox::Save | QMessageBox::Discard| QMessageBox::Cancel,QMessageBox::Save); 二、QMessageBox类
1、text 该属性表示对话框显示的主要文本文本可以是纯文本或者富文本默认为空字符串。 部件会自动检测文本类型文本类型默认Qt::AutoText也可以通过setTextFormat设置。
QString text() const
void setText(const QString text)
2、detailedText 该属性表示对话框显示的详细文本文本为纯文本格式默认为空字符串。
QString detailedText() const
void setDetailedText(const QString text)
3、informativeText 该属性表示对话框显示的信息文本一般用来做主要文本的扩展在MAC系统中会以更小的字体显示在主要文本下面其他系统字体大小和主要文本相同文本为纯文本格式默认为空字符串。
QString informativeText() const
void setInformativeText(const QString text)
4、icon 该属性表示对话框显示的系统默认图标图标默认为空。也可以通过setIconPixmap设置自定义图标。
QMessageBox::Icon icon() const
void setIcon(QMessageBox::Icon) 5、iconPixmap 该属性表示当前显示的像素图标可以通过setIconPixmap自定义需要显示的像素图标。 一般像素图标不一定会适用于全部的系统所以如果在跨平台的时候需要为每个平台绘制适合的图标。
QPixmap iconPixmap() const
void setIconPixmap(const QPixmap pixmap)
6、standardButtons 该属性表示需要显示的标准按钮的集合。
QMessageBox::StandardButtons standardButtons() const
void setStandardButtons(QMessageBox::StandardButtons buttons) 比如你可以这么做
setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); QMessageBox::StandardButton描述QMessageBox::Ok带AcceptRole角色属性的“Ok”按钮QMessageBox::Open带AcceptRole角色属性的“Open”按钮QMessageBox::Save带AcceptRole角色属性的“Save”按钮QMessageBox::Cancel带RejectRole角色属性的“Cancel”按钮QMessageBox::Close带RejectRole角色属性的“Close”按钮QMessageBox::Discard带DestructiveRole角色属性的基于平台的“Discard”或者“Dont Save”按钮。QMessageBox::Apply带AcceptRole角色属性的“Apply”按钮QMessageBox::Reset带ResetRole角色属性的“Reset”按钮QMessageBox::RestoreDefaults带ResetRole角色属性的“Restore Defaults”按钮QMessageBox::Help带HelpRole角色属性的“Help”按钮QMessageBox::SaveAll带AcceptRole角色属性的“Save All”按钮QMessageBox::Yes带YesRole角色属性的“Yes”按钮QMessageBox::YesToAll带YesRole角色属性的“Yes to All”按钮QMessageBox::No带NoRole角色属性的“No”按钮QMessageBox::NoToAll带NoRole角色属性的“No to All”按钮QMessageBox::Abort带RehectRole角色属性的“Abort”按钮QMessageBox::Retry带AcceptRole角色属性的“Retry”按钮QMessageBox::Ignore带AcceptRole角色属性的“Ignore”按钮QMessageBox::NoButton无效按钮 7、textFormat 该属性表示显示的主要文本格式默认为Qt::AutoText。
Qt::TextFormat textFormat() const
void setTextFormat(Qt::TextFormat format)
8、textInteractionFlags 该属性表示对话框的标签文本应该如何和用户交互。
Qt::TextInteractionFlags textInteractionFlags() const
void setTextInteractionFlags(Qt::TextInteractionFlags flags) Qt::TextInteractionFlag描述Qt::NoTextInteraction不能与文本交互Qt::TextSelectableByMouse可以用鼠标选择文本并使用上下文菜单或标准键盘快捷键将文本复制到剪贴板。Qt::TextSelectableByKeyboard可以用键盘上的光标键选择文本。显示一个文本光标。Qt::LinksAccessibleByMouse链接可以用鼠标突出显示和激活。Qt::LinksAccessibleByKeyboard链接可以使用tab键聚焦并使用enter键激活。Qt::TextEditable文本是完全可编辑的。Qt::TextEditorInteractionTextSelectableByMouse | TextSelectableByKeyboard | TextEditableQt::TextBrowserInteractionTextSelectableByMouse | LinksAccessibleByMouse | LinksAccessibleByKeyboard 9、公共函数
1addButton 添加标准按钮并添加按钮的角色。
void addButton(QAbstractButton *button, QMessageBox::ButtonRole role)
QPushButton *addButton(const QString text, QMessageBox::ButtonRole role)
QPushButton *addButton(QMessageBox::StandardButton button) 如果觉得标准按钮不够灵活使用addButton()重载它接受一个文本和一个ButtonRole来添加自定义按钮。QMessageBox使用ButtonRole来确定屏幕上按钮的顺序(根据平台而变化)。可以在调用exec()之后测试clickedButton()的值。
QMessageBox msgBox;
QPushButton *connectButton msgBox.addButton(tr(Connect), QMessageBox::ActionRole);
QPushButton *abortButton msgBox.addButton(QMessageBox::Abort);
msgBox.exec();
if (msgBox.clickedButton() connectButton) {// connect
} else if (msgBox.clickedButton() abortButton) {// abort
}2button 返回标准按钮的指针。
QAbstractButton *button(QMessageBox::StandardButton which) const
3buttons 返回按钮的集合。
QListQAbstractButton * buttons() const
4buttonRole 返回按钮的的角色。
QMessageBox::ButtonRole buttonRole(QAbstractButton *button) const
5removeButton 移除按钮。
void removeButton(QAbstractButton *button)
6checkBox 返回复选框指针。
QCheckBox *checkBox() const
7setCheckBox 添加复选框添加后对话框获得复选框所有权。如果cb设置为空指针表示删除复选框。
void setCheckBox(QCheckBox *cb)
8clickedButton 返回用户按下的按钮指针可以通过此获取哪个自定义按钮被按下。
QAbstractButton *clickedButton() const
9defaultButton 返回默认按钮的指针。
QPushButton *defaultButton() const
10setDefaultButton 设置默认按钮当用户按下Enter的时候触发该按钮。如果未指定默认按钮QMessageBox将根据消息框中使用的按钮的按钮角色尝试查找一个。
void setDefaultButton(QPushButton *button)
void setDefaultButton(QMessageBox::StandardButton button)
11buttonRole 返回按钮角色。
QMessageBox::ButtonRole buttonRole(QAbstractButton *button) const QMessageBox::ButtonRole描述QMessageBox::InvalidRole无效按钮QMessageBox::AcceptRole表示接受Accept比如okQMessageBox::RejectRole表示拒绝Reject比如CancelQMessageBox::DestructiveRole表示丢弃并关闭对话框QMessageBox::ActionRole表示按下按钮对话框元素会被修改QMessageBox::HelpRole表示帮助QMessageBox::YesRoleYESQMessageBox::NoRoleNoQMessageBox::ApplyRole表示接收当前改变QMessageBox::ResetRole表示重置当前改变 12escapeButton 返回设置退出按钮指针。
QAbstractButton *escapeButton() const
13setEscapeButton 设置退出按钮指定之后按下ESC键会触发此按钮。 如果没有指定转义按钮QMessageBox将尝试使用以下规则找到一个转义按钮
如果只有一个按钮则为按Esc时激活的按钮。如果有“取消”按钮则是按Esc时激活的按钮。如果只有一个按钮具有Reject角色或No角色则它是按Esc时激活的按钮。 当无法使用这些规则确定退出按钮时按Esc无效。
void setEscapeButton(QAbstractButton *button)
void setEscapeButton(QMessageBox::StandardButton button)
14open 打开对话框并将其finished()或buttonClicked()信号连接到接收器和成员指定的插槽。如果槽函数的第一个参数有一个指针则连接到buttonClicked()否则连接到finished()当对话框关闭的时候这个信号槽连接会被断开。
void open(QObject *receiver, const char *member) 实例打开对话框的同时绑定信号finished与槽函数onMessageBoxFinish对话框关闭打印信息。
public slots:void onMessageBoxFinish();
void MainWindow::onMessageBoxFinish()
{qDebug()QMessageBox Finish;
}
QMessageBox msgBox;
msgBox.setWindowTitle(QMessageBox);
msgBox.setText(The document has been modified.);
msgBox.setInformativeText(Do you want to save your changes?);
msgBox.setDetailedText(The document may have been modified externally);
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
msgBox.open(this,SLOT(onMessageBoxFinish()));
int ret msgBox.exec();
15exec 重写的exec一样是模式对话框打开如果存在标准按钮返回值表示按下的标准按钮的StandardButton值如果是自定的按钮那么这个值不确定需要根据clickbutton()来确定按下了哪个按钮。 注意从QDialog继承来的result()也是返回StandardButton值而不是QDialog::DialogCode
virtual int exec() override
10、静态函数 在指定的父小部件前面打开具有给定标题和文本的关键消息模态对话框。
QMessageBox::StandardButton critical(QWidget *parent, const QString title, const QString text, QMessageBox::StandardButtons buttons Ok, QMessageBox::StandardButton defaultButton NoButton)
QMessageBox::StandardButton information(QWidget *parent, const QString title, const QString text, QMessageBox::StandardButtons buttons Ok, QMessageBox::StandardButton defaultButton NoButton)
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) 显示一个简单的关于框其中包含标题和文本。
void about(QWidget *parent, const QString title, const QString text) 显示一个关于Qt的简单消息框该消息包括应用程序正在使用的Qt版本号一般用于程序中的help菜单。
void aboutQt(QWidget *parent, const QString title QString())
11、信号 每当在QMessageBox中单击按钮时就会发出此信号。
void buttonClicked(QAbstractButton *button)