[Qt] Qt对话框 [2013-09-17更新]

来源:互联网 发布:查找英文文献的数据库 编辑:程序博客网 时间:2024/06/06 08:28
- 模式与非模式对话框

dialog.show()     // 是否为显示模式对话框根据modal属性的设置而定
dialog.exec()     // 忽略modal属性,总显示为模式对话框
void setModal (bool modal)     // 设置true时,表示设置为模式对话框
bool isModal ()                // 判断是否为模式对话框



- QMessageBox

#include <QMessageBox>

QMessageBox msgBox;
msgBox.setText(QObject::tr("message消息内容"));
msgBox.exec();

QMessageBox msgBox;
msgBox.setWindowTitle("确认重启设备");
msgBox.setText("确定现在重启系统吗?");
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowIcon(QIcon(":res/logo.jpg"));     // 不设置的话默认与父窗体相同
msgBox.exec();

------------------------------------------------------------------------

QMessageBox::about ( QWidget * parent, const QString & title, const QString & text )

QMessageBox::aboutQt ( QWidget * parent, const QString & title = QString() )

QMessageBox::critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

QMessageBox::information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

QMessageBox::question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

QMessageBox::warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

------------------------------------------------------------------------

- StandardButton examples

QMessageBox::Yes / QMessageBox::No

简单示例:
quint32 ret = QMessageBox::question(this,
                                    "确认是否重启",
                                    "确定现在重启系统吗?",
                                    QMessageBox::No,
                                    QMessageBox::Yes);

if (ret == QMessageBox::Yes)
{
    reboot();
}
...



- 打开文件对话框(QFileDialog)

#include <QFileDialog>

QString filePath = QFileDialog::getOpenFileName(this,
"Select files",                                        // 对话框标题
QDir::currentPath(),                                   // 默认路径
"File type description(*.fileType1 *.fileType2 ...);;All files(*.*)");

if (!filePath.isNull())
{
    ...
}

! 注意:用空格隔开文件类型(在同一下拉列表项中显示);“;;”后面的内容将会作为一个新的文件类型下拉列表项。



- QInputDialog

#include <QInputDialog>

#define PI_EXIT_PWD      "123456"

QInputDialog *inputDlg = new QInputDialog(this);
inputDlg->setInputMode(QInputDialog::TextInput);
inputDlg->setTextEchoMode(QLineEdit::Password);
inputDlg->setLabelText("请输入验证密码:");
int ret = inputDlg->exec();

if (ret == QInputDialog::Accepted)
{
    if (inputDlg->textValue() != PI_EXIT_PWD)
    {
        showCritical("密码不正确!");
        e->ignore();
    }
}
else if (ret == QInputDialog::Rejected)
{
    e->ignore();
}
...