您好,登錄后才能下訂單哦!
*QMessageBox的用法:
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 <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”內的代碼:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }1234567891011
注:本博客所有代碼全部經過實際測試,成功編譯運行后才發出來.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。