Qt学习笔记12:基本会话框4——总结

文件对话框静态函数

QString QFileDialog::getOpenFileName
{
QWidget *parent = 0; //标准文件对话框的父窗口
const QString &caption = QString(); //标准文件对话框的标题名
const QString &dir = QString(); //指定了默认的目录
const QString &filter = QString(); //对文件类型进行过滤,只有与过滤器匹配的文件类型才显示,可以同事指定多种多滤方式,用”;;“隔开
QString *selectedFilter = 0; //用户选择的过滤器通过此参数返回
Options options = 0; //选择显示文件名的格式,默认是同时显示目录与文件名
}
1
2
3
4
5
6
7
8
9
使用:

//以自己为父窗口,命名为“open file dialog",从”/“目录开始搜索,
//寻找.c,.cpp以及.h文件
QString s = QFileDialog::getOpenFileName(this,"open file dialog","/"
,"C++ files(*.cpp);;C file(*.c);;Head files(*.h)");
fileLineEdit->setText(s);
1
2
3
4
5
效果如果:

标准颜色对话框静态函数

QColor getColor()
{
const QColor &initial = Qt::white;//指出默认选中的颜色
QWidget *parent = 0;
}
1
2
3
4
5
通过QColor::isValid()函数判断用户选择的颜色是否有效

void Dialog::showColor()
{
QColor c = QColorDialog::getColor(Qt::blue);
if(c.isValid())
{
colorFrame->setPalette(QPalette(c));
}
}
1
2
3
4
5
6
7
8
效果如下:

字体对话框静态函数

QFont getFont
{
bool *ok;//用户选择ok,*ok为true,函数返回用户选择的字体,否则,返回默认字体
QWidget *parent = 0;
}
1
2
3
4
5
使用:

void dialog::showFont()
{
bool ok;
QFont f = QFontDialog::getFont(&ok);
if(ok)
{
fontLineEdit->setFont(f);
}
}
1
2
3
4
5
6
7
8
9
效果:


各种输入对话框
字符串输入对话框
静态函数getText()

QString getText
{
QWidget *parent, //父窗口
const QString &title, //标题名
const QString &label, //标签提示
QLineEdit::EchoMode mode = QLineEdit::Normal, //QLineEdit控件的输入模式
const QString &text = QString(), //对话框弹出时QLineEdit控件中默认出现的文字
bool *ok = 0, //指示哪个按钮被触发
Qt::WindowFlags flags = 0 //指明对话框的窗体标识
};
1
2
3
4
5
6
7
8
9
10
使用:

void InputDlg::ChangeName()
{
bool ok;
QString text = QInputDialog::getText(this,tr("标准字符串输入对话框"),tr("请输入姓名: "),QLineEdit::Normal,nameLabel2->text(),&ok);
if(ok && !text.isEmpty())
{
nameLabel2->setText(text);
}
}
1
2
3
4
5
6
7
8
9
效果:


条目对话框
静态函数getItem()

QString getItem
{
QWidget *parent,
const QString &title,
const QString &label,
const QString &items, //指定QComboBox控件显示的可选条目是一个QStringList对象
int current = 0, //QComboBox控件中默认显示的条目的序号
bool *ok = 0,
Qt::WindowFlags flags = 0
};
1
2
3
4
5
6
7
8
9
10
使用:

void InputDlg::ChangeSex()
{
QStringList SexItems;
SexItems << tr("男") << tr("女");
bool ok;
QString SexItem = QInputDialog::getItem(this,tr("标准条目选择对话框"),tr("请选择性别: "),SexItems,0,false,&ok);
if(ok && !SexItem.isEmpty())
sexLabel2->setText(SexItem);
}
1
2
3
4
5
6
7
8
9
效果:


int类型输入对话框
静态函数getInt()

int getInt
{
QWidget *parent,
const QString &title,
const QString &label,
int value = 0, //QSpinBox控件的默认显示值
int min = -2147483647, //指定QSpinBox控件的数值范围
int max = 2147483647,
int step = 1, //使用QSpinBox控件的步进值,也就是按上下一次增加多少
book *ok = 0,
Qt::WindowFlags flags = 0
};
1
2
3
4
5
6
7
8
9
10
11
12
使用:

void InputDlg::changeAge()
{
bool ok;
int age = QInputDialog::getInt(this,tr("标准int类型输入对话框"),
tr("请输入年龄: "),ageLabel2->text().toInt(&ok),0,100,1,&ok);
if(ok)
ageLabel2->setText(QString(tr("%1")).arg(age));
}
1
2
3
4
5
6
7
8


double类型输入对话框
静态函数getDouble()

double getDouble
{
QWidget *parent,
const QString &title.
const QString &label,
double valur = 0, //QSpinBox控件的默认显示值
double min = -2147483647, //QSpinBox控件的范围
double max = 2147483647,
int decimals = 1, //QSpinBox的步进值
bool *ok = 0,
Qt::WindowFlags flags = 0
};
1
2
3
4
5
6
7
8
9
10
11
12
使用:

void InputDlg::ChangeScore()
{
bool ok;
double score = QInputDialog::getDouble(this,tr("标准double输入对话框"),tr("请输入成绩: "),
scoreLabel2->text().toDouble(&ok),0,100,1,&ok);
if(ok)
scoreLabel2->setText(QString(tr("%1")).arg(score));
}
1
2
3
4
5
6
7
8
效果:


消息会话框
Question消息框
静态函数QMessageBox::question

StandardButton QMessageBox::question
{
QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok, //填写希望在消息框中出现的按钮
StandardButton defaultButton=NoButton //默认按钮焦点默认处于哪个按钮
};
1
2
3
4
5
6
7
8
对于上述的Ok,可以用 | 连写,QMessageBox类提供了很多标准按钮,例如:
QMessageBox::Ok、QMessageBox::Close、QMessageBox::Discard等,这些东西虽然可以选择,但不可乱选,且通常都是成对出现的,例如:
Save与Discard
Abort、Retry、Ignore一起出现
使用:

void MsgBoxDlg::showQuestinMsg()
{
label->setText(tr("Question Message Box"));
switch(QMessageBox::question(this,tr("Question 消息框"),
tr("您现在已经修改完成,是否要结束程序?"),
QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Ok))
{
case QMessageBox::Ok:
label->setText("Question button/Ok");
break;
case QMessageBox::Cancel:
label->setText("Question button\Cancel");
break;
default:
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
效果:

Information消息框
静态函数Information()

StandardButton QMessageBox::information
{
QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok, //同上
StandardButton defaultButton = NoButton //同上
};
1
2
3
4
5
6
7
8
使用:

void MsgBoxDlg::showInformationMsg()
{
label->setText(tr("Information Message Box"));
QMessageBox::information(this,tr("Information消息框"),tr("这是Information对话框,欢迎您!"));
}
1
2
3
4
5
效果:

Warning消息框
静态函数warning()

StandardButton QMessage::warning
{
QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok,
StandardButton defaultButton=NoButton
};
1
2
3
4
5
6
7
8
使用:

void MsgBoxDlg::showWarningMsg()
{
label->setText(tr("Warning Message Box"));
switch(QMessageBox::warning(this,tr("Warning消息框"),tr("您修改的内容还未保存,是否要保存对文档的修改?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,QMessageBox::Save))
{
case QMessageBox::Save:
label->setText(tr("Warning button/Save"));
break;
case QMessageBox::Discard:
label->setText(tr("Warning button/Discard"));
break;
case QMessageBox::Cancel:
label->setText(tr("Warning button/Cancle"));
break;
default:
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
效果:

Critical消息框
静态函数critical()

StandardButton QMessageBox::critical()
{
QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton
};
1
2
3
4
5
6
7
8
使用:

void MsgBoxDlg::showCriticalMsg()
{
label->setText(tr("Critical Message Box"));
QMessageBox::critical(this,tr("Critical消息框"),tr("这是一个Critical消息框!"));
}
1
2
3
4
5
效果:

About消息框
静态函数about()

void MessageBox::about
{
QWidget *parent,
const QString &title,
const QString &text
};
1
2
3
4
5
6
使用:

void MsgBoxDlg::showAboutMsg()
{
label->setText(tr("About Message Box"));
QMessageBox::about(this,tr("About消息框"),tr("这是一个About消息框!"));
}
1
2
3
4
5
效果:

AboutQt消息框
静态函数aboutQt()

void QMessageBox::aboutQt
{
QWidget *parent,
const QString &title = QString()
};
1
2
3
4
5
使用:

void MsgBoxDlg::showAboutQtMsg()
{
label->setText(tr("About Qt Message Box"));
QMessageBox::aboutQt(this,tr("About Qt 消息框"));
}
1
2
3
4
5
效果:

自定义消息框

void dialog::showCustomDlg()
{
label->setText(tr("Custom Message Box"));
QMessageBox customMsgBox;
customMsgBox.setWindowTitle(tr("用户自定义消息框"));
//addButton()函数为消息框添加自定义的按钮,第一个参数为按钮名,第二个参数为按钮的类型
QPushButton *yesBtn = customMsgBox.addButton(tr("Yes"),QMessageBox::ActionRole);
QPushButton *noBtn = customMsgBox.addButton(tr("No"),QMessageBox::ActionRole);
QPushButton *cancelBtn = customMsgBox.addButton(QMessageBox::Cancel);
//设置消息框中显示的提示信息内容
customMsgBox.setText(tr("这是一个用户自定义消息框"));
//设置消息框的图标
customMsgBox.setIconPixmap(QPixmap("0.png"));
customMsgBox.exec();
if(customMsgBox.clickedButton() == yesBtn)
label->setText("Custom Message Box/Yes");
if(customMsgBox.clickedButton() == noBtn)
label->setText("Custom Message Box/No");
if(customMsgBox.clickedButton() == cancelBtn)
label->setText("Custom Message Box/Cancel");
}
--------------------- 

猜你喜欢

转载自www.cnblogs.com/hyhy904/p/11021125.html
今日推荐