1.获取打开文件的文件路径
//获取打开文件的文件路径--父窗口,窗口名称,打开路径,文件筛选
QString s = QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.cpp)::C files(*.c)::Head files(*.h)");
2.打开颜色管理器,获取选中的颜色
//打开颜色管理器,获取选中的颜色,并改变QFrame的颜色为选中的颜色
QColor c = QColorDialog::getColor(Qt::blue);
if(c.isValid())
{
QFrame *frame=new QFrame;
frame->setFrameShape(QFrame::Box);
frame->setAutoFillBackground(true);
frame->setPalette(QPalette(c));
}
3.打开字体设置对话框,返回设置的字体
//打开字体设置对话框,返回设置的字体
bool ok;
QFont f = QFontDialog::getFont(&ok);
if (ok)
{
//lineEdit->setFont(f);
}
4.用户自定义对话框
//用户自定义对话框,并返回用户点击的按钮
QMessageBox customMsgBox;
customMsgBox.setWindowTitle(tr("用户自定义消息框"));
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("Qt.png"));
customMsgBox.exec();
if(customMsgBox.clickedButton()==yesBtn)
return 1;
if(customMsgBox.clickedButton()==noBtn)
return 0;
if(customMsgBox.clickedButton()==cancelBtn)
return 2;
return;
//1.QuestionMessageBox
int tempnum = 1;
switch(QMessageBox::question(this,tr("Question框"),
tr("修改完成,是否要结束程序?"),
QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))
{
case QMessageBox::Ok:
tempnum = 1;
break;
case QMessageBox::Cancel:
tempnum = 0;
break;
default:
break;
}
return tempnum;
//2.informationMessageBox
QMessageBox::information(this,tr("Information框"),tr("这是Information框"));
//3.warningMessageBox
int tempnum = 1;
switch(QMessageBox::warning(this,tr("Warning框"),
tr("您修改的内容还未保存,是否要保存?"),
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,
QMessageBox::Save))
{
case QMessageBox::Save:
tempnum = 1;
break;
case QMessageBox::Discard:
tempnum = 2;
break;
case QMessageBox::Cancel:
tempnum = 0;
break;
default:
break;
}
return tempnum;
//4.criticalMessageBox--错误消息框
QMessageBox::critical(this,tr("Critical消息框"),tr("Critical框测试!"));
//5.aboutMessageBox
QMessageBox::about(this,tr("About消息框"),tr("About框测试!"));
//1.字符串
bool ok;
QString text=QInputDialog::getText(this,tr("标准输入对话框"),tr("请输入XXXX:"), QLineEdit::Normal,"默认内容",&ok);
if (ok && !text.isEmpty())
return text;
//2.下拉框
QStringList SexItems;
SexItems << tr("男") << tr("女");
bool ok;
QString SexItem = QInputDialog::getItem(this, tr("标准条目选择对话框"),
tr("请选择性别:"), SexItems, 0, false, &ok);
if (ok && !SexItem.isEmpty())
return SexItem;
//3.int型
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));
//4.double型
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));