*QMessageBox的用法:
創(chuàng)新互聯(lián)主營(yíng)雜多網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā)公司,雜多h5重慶小程序開(kāi)發(fā)公司搭建,雜多網(wǎng)站營(yíng)銷(xiāo)推廣歡迎雜多等地區(qū)企業(yè)咨詢(xún)
members: 1.void setStandardButtons(QMessageBox::); //設(shè)置按鈕. 2.void setText(QString); //設(shè)置內(nèi)容. 3.void setInformativeText(QString); //設(shè)置重要內(nèi)容.位于對(duì)話(huà)框底部的位置.按鈕之上. 4.void setIconPixmap(QPixmap); //設(shè)置圖片.可以自定義的. 5.void setDefaultButton(QMessageBox::); //設(shè)置默認(rèn)被選中的按鈕. 6.void setIcon(QMessageBox::Icon); //設(shè)置圖標(biāo).默認(rèn)5個(gè)選一個(gè). 7.void setButtonText(QMessageBox::, QString); //給按鈕重未名. 8.void setDetailedText(QString); //設(shè)置詳細(xì)框內(nèi)容. 9.void setCheckBox(QCheckBox*); //加入一個(gè)QCheckBox的對(duì)象.1234567891011
QFileDialog的用法:
QFileDialog::AcceptOpen. //打開(kāi)文件,默認(rèn)的.
QFileDialog::AcceptSave. //保存文件.
members: 1.void setAcceptMode(QFileDialog::); //設(shè)置對(duì)話(huà)框的模式. 2.int exec(); //返回選擇的狀態(tài). 3.void setDefaultSuffix(QString); //設(shè)置默認(rèn)后綴. 4.void setDirectory(QString); //設(shè)置路徑. 5.QString getOpenFileName(); //獲得打開(kāi)的文件的名字. 6.QString getSaveFileName(); //獲得保存的文件的名字. 7.QStringList selectedFiles(); //返回被選中的文件名. 8.void setNameFilter(QString); //設(shè)置過(guò)濾器.12345678910
先從Qt設(shè)計(jì)師中拖拽出所需要的布局和更改對(duì)象名:
以下是”c.cpp”內(nèi)的代碼:
#include "c.h"c::c(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); ui.openFileButton->setText(QString::fromLocal8Bit("打開(kāi)文件")); ui.saveFileButton->setText(QString::fromLocal8Bit("保存文件")); ui.messageButton->setText(QString::fromLocal8Bit("完全版對(duì)話(huà)框")); ui.messageButton_2->setText(QString::fromLocal8Bit("顯示六種快捷調(diào)用")); 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: //定義一個(gè)QFileDialog的對(duì)象. QFileDialog temp(this); //設(shè)置它的標(biāo)題. temp.setWindowTitle("open file"); //設(shè)置它的模式為打開(kāi)文件模式. temp.setAcceptMode(QFileDialog::AcceptOpen); //設(shè)置打開(kāi)的路徑. temp.setDirectory("c:/users/administrator/desktop"); //設(shè)置過(guò)濾器. temp.setNameFilter(QString::fromLocal8Bit("文本文檔(*.txt);;所有文件(*.*)")); //判斷是否成功打開(kāi)一個(gè)文件. if (temp.exec() == QDialog::Accepted) { //返回打開(kāi)文件的絕對(duì)路徑. QString path = temp.selectedFiles().at(0); //連接文件,并且打開(kāi)文件. QFile *file = new QFile(path); bool ok = file->open(QIODevice::ReadOnly); //如果成功打開(kāi)文件的話(huà)...進(jìn)行以下操作. if (ok) { QTextStream s(file); //....文件已經(jīng)打開(kāi),接下來(lái)可以從打開(kāi)的文件內(nèi)讀取字符流. } } //方法2: //獲取打開(kāi)的文件的絕對(duì)路徑. QString path = QFileDialog::getOpenFileName(this, "open file", "c:/users/administrator/desktop", QString::fromLocal8Bit("文本文檔(*.txt);;所有文件(*.*)")); //如果路徑不會(huì)空,則視為成功打開(kāi)文件.如果路徑為空,則視為未選擇打開(kāi)文件,可能點(diǎn)擊取消. if (!path.isEmpty()) { QFile *file = new QFile(path); bool ok = file->open(QIODevice::ReadOnly); if (ok) { QTextStream s(file); //....文件已經(jīng)打開(kāi),接下來(lái)可以從打開(kāi)的文件內(nèi)讀取字符流. } } }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);;所有文件(*.*)")); //設(shè)置默認(rèn)添加后綴".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); //....文件已經(jīng)打開(kāi),接下來(lái)向文件內(nèi)寫(xiě)入字符流. } } //方法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); //....文件已經(jīng)打開(kāi),接下來(lái)向文件內(nèi)寫(xiě)入字符流. } } }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("顯示對(duì)話(huà)框")); temp.setCheckBox(s); temp.setDetailedText(QString::fromLocal8Bit("這是一個(gè)用來(lái)提醒用戶(hù)是否輸入正確的對(duì)話(huà)框!")); 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 <QtWidgets/QMainWindow>#include "ui_c.h"#include <QPushButton>#include <QFileDialog>#include <QMessageBox>#include <QTextStream>#include <QFile>#include <QCheckBox>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”內(nèi)的代碼:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }1234567891011
注:本博客所有代碼全部經(jīng)過(guò)實(shí)際測(cè)試,成功編譯運(yùn)行后才發(fā)出來(lái).
網(wǎng)站欄目:Qt學(xué)習(xí):QFileDialog和QMessageBox的用法和程序示例
新聞來(lái)源:http://muchs.cn/article18/iioddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、微信小程序、移動(dòng)網(wǎng)站建設(shè)、域名注冊(cè)、外貿(mào)網(wǎng)站建設(shè)、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)