重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
*QMessageBox的用法:
创新互联主营杂多网站建设的网络公司,主营网站建设方案,重庆APP开发公司,杂多h5重庆小程序开发公司搭建,杂多网站营销推广欢迎杂多等地区企业咨询
members: 1.void setStandardButtons(QMessageBox::); //设置按钮. 2.void setText(QString); //设置内容. 3.void setInformativeText(QString); //设置重要内容.位于对话框底部的位置.按钮之上. 4.void setIconPixmap(QPixmap); //设置图片.可以自定义的. 5.void setDefaultButton(QMessageBox::); //设置默认被选中的按钮. 6.void setIcon(QMessageBox::Icon); //设置图标.默认5个选一个. 7.void setButtonText(QMessageBox::, QString); //给按钮重未名. 8.void setDetailedText(QString); //设置详细框内容. 9.void setCheckBox(QCheckBox*); //加入一个QCheckBox的对象.1234567891011
QFileDialog的用法:
QFileDialog::AcceptOpen. //打开文件,默认的.
QFileDialog::AcceptSave. //保存文件.
members: 1.void setAcceptMode(QFileDialog::); //设置对话框的模式. 2.int exec(); //返回选择的状态. 3.void setDefaultSuffix(QString); //设置默认后缀. 4.void setDirectory(QString); //设置路径. 5.QString getOpenFileName(); //获得打开的文件的名字. 6.QString getSaveFileName(); //获得保存的文件的名字. 7.QStringList selectedFiles(); //返回被选中的文件名. 8.void setNameFilter(QString); //设置过滤器.12345678910
先从Qt设计师中拖拽出所需要的布局和更改对象名:
以下是”c.cpp”内的代码:
#include "c.h"c::c(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); ui.openFileButton->setText(QString::fromLocal8Bit("打开文件")); ui.saveFileButton->setText(QString::fromLocal8Bit("保存文件")); ui.messageButton->setText(QString::fromLocal8Bit("完全版对话框")); ui.messageButton_2->setText(QString::fromLocal8Bit("显示六种快捷调用")); connect(ui.openFileButton, SIGNAL(clicked()), this, SLOT(openFileDialogSlot())); connect(ui.saveFileButton, SIGNAL(clicked()), this, SLOT(saveFileDialogSlot())); connect(ui.messageButton, SIGNAL(clicked()), this, SLOT(printValidatorDialogSlot())); connect(ui.messageButton_2, SIGNAL(clicked()), this, SLOT(showAllDialogSlot())); } c::~c() { }void c::openFileDialogSlot() { //方法1: //定义一个QFileDialog的对象. QFileDialog temp(this); //设置它的标题. temp.setWindowTitle("open file"); //设置它的模式为打开文件模式. temp.setAcceptMode(QFileDialog::AcceptOpen); //设置打开的路径. temp.setDirectory("c:/users/administrator/desktop"); //设置过滤器. temp.setNameFilter(QString::fromLocal8Bit("文本文档(*.txt);;所有文件(*.*)")); //判断是否成功打开一个文件. if (temp.exec() == QDialog::Accepted) { //返回打开文件的绝对路径. QString path = temp.selectedFiles().at(0); //连接文件,并且打开文件. QFile *file = new QFile(path); bool ok = file->open(QIODevice::ReadOnly); //如果成功打开文件的话...进行以下操作. if (ok) { QTextStream s(file); //....文件已经打开,接下来可以从打开的文件内读取字符流. } } //方法2: //获取打开的文件的绝对路径. QString path = QFileDialog::getOpenFileName(this, "open file", "c:/users/administrator/desktop", QString::fromLocal8Bit("文本文档(*.txt);;所有文件(*.*)")); //如果路径不会空,则视为成功打开文件.如果路径为空,则视为未选择打开文件,可能点击取消. if (!path.isEmpty()) { QFile *file = new QFile(path); bool ok = file->open(QIODevice::ReadOnly); if (ok) { QTextStream s(file); //....文件已经打开,接下来可以从打开的文件内读取字符流. } } }void c::saveFileDialogSlot() { //方法1: QFileDialog temp(this); temp.setWindowTitle("save file"); temp.setAcceptMode(QFileDialog::AcceptSave); temp.setDirectory("c:/users/administrator/desktop"); temp.setNameFilter(QString::fromLocal8Bit("文本文档(*.txt);;所有文件(*.*)")); //设置默认添加后缀".txt". temp.setDefaultSuffix(".txt"); if (temp.exec() == QDialog::Accepted) { QString path = temp.selectedFiles().at(0); QFile *file = new QFile(path); bool ok = file->open(QIODevice::WriteOnly); if (ok) { QTextStream s(file); //....文件已经打开,接下来向文件内写入字符流. } } //方法2: QString path = QFileDialog::getOpenFileName(this, "save file", "c:/users/administrator/desktop", QString::fromLocal8Bit("文本文档(*.txt);;所有文件(*.*)")); if (!path.isEmpty()) { QFile *file = new QFile(path); bool ok = file->open(QIODevice::ReadOnly); if (ok) { QTextStream s(file); //....文件已经打开,接下来向文件内写入字符流. } } }void c::printValidatorDialogSlot() { QCheckBox *s = new QCheckBox; s->setText(QString::fromLocal8Bit("你是否喜欢Qt")); QMessageBox temp; temp.setStandardButtons(QMessageBox::Ok | QMessageBox::No | QMessageBox::Cancel); temp.setWindowTitle(QString::fromLocal8Bit("输入情况")); temp.setText(QString::fromLocal8Bit("显示对话框")); temp.setCheckBox(s); temp.setDetailedText(QString::fromLocal8Bit("这是一个用来提醒用户是否输入正确的对话框!")); temp.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("确定")); temp.setButtonText(QMessageBox::No, QString::fromLocal8Bit("不确定")); temp.setButtonText(QMessageBox::Cancel, QString::fromLocal8Bit("取消")); temp.setIconPixmap(QPixmap("1.png").scaled(100, 100)); temp.setDefaultButton(QMessageBox::No); temp.setInformativeText(QString::fromLocal8Bit("输入正确!")); temp.exec(); }void c::showAllDialogSlot() { QMessageBox::aboutQt(this, "aboutQt"); QMessageBox::about(this, "about", "about"); QMessageBox::information(this, "information", "information"); QMessageBox::warning(this, "warning", "warning"); QMessageBox::question(this, "question", "question"); QMessageBox::critical(this, "critical", "critical"); }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
以下是”c.h”下的代码:
#ifndef C_H#define C_H#include#include "ui_c.h"#include #include #include #include #include #include class c : public QMainWindow{ Q_OBJECTpublic: c(QWidget *parent = 0); ~c();private slots: void openFileDialogSlot(); void saveFileDialogSlot(); void printValidatorDialogSlot(); void showAllDialogSlot();private: Ui::cClass ui; };#endif // C_H1234567891011121314151617181920212223242526272829303132
最后是”main.cpp”内的代码:
#include "c.h"#includeint main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }1234567891011
注:本博客所有代码全部经过实际测试,成功编译运行后才发出来.