QT實(shí)現(xiàn)QMessageBox中文按鈕示例詳解
這是我記錄Qt學(xué)習(xí)過程心得文章的第二篇,主要是為了方便QMessageBox彈出框的使用,通過自定義的方式,將其常用的功能,統(tǒng)一封裝成一個函數(shù),還是寫在了Skysonya類里面。
實(shí)現(xiàn)代碼
//中文提示對話框
bool Skysonya::messageBox(QString msgType, QString dlgTitle, QString strInfo)
{
QMessageBox *msgBox;
int result = -1;
bool value = false;
if (msgType.toLower() == "critical")
{
//創(chuàng)建一個Critical彈出對話框,添加按鈕:"Ok"
msgBox = new QMessageBox(QMessageBox::Critical, dlgTitle, strInfo, QMessageBox::Ok);
msgBox->button(QMessageBox::Ok)->setText("確定"); //將"Ok"按鈕改為顯示"確定"
result = msgBox->exec(); //顯示Critical彈出對話框
if (result == QMessageBox::Ok)
{
value = true;
}
}
else if (msgType.toLower() == "warning")
{
//創(chuàng)建一個Warning彈出對話框,添加按鈕:"Ok"
msgBox = new QMessageBox(QMessageBox::Warning, dlgTitle, strInfo, QMessageBox::Ok);
msgBox->button(QMessageBox::Ok)->setText("確定"); //將"Ok"按鈕改為顯示"確定"
result = msgBox->exec(); //顯示W(wǎng)arning彈出對話框
if (result == QMessageBox::Ok)
{
value = true;
}
}
else if (msgType.toLower() == "warningyes")
{
//創(chuàng)建一個Warning彈出對話框,添加按鈕:"Yes"、"No"
msgBox = new QMessageBox(QMessageBox::Warning, dlgTitle, strInfo, QMessageBox::Yes | QMessageBox::No);
msgBox->button(QMessageBox::Yes)->setText("是"); //將"Yes"按鈕改為顯示"是"
msgBox->button(QMessageBox::No)->setText("否"); //將"No"按鈕改為顯示"否"
result = msgBox->exec(); //顯示W(wǎng)arning彈出對話框
if (result == QMessageBox::Yes)
{
value = true;
}
}
else if (msgType.toLower() == "information")
{
//創(chuàng)建一個Information彈出對話框,添加按鈕:"Ok"
msgBox = new QMessageBox(QMessageBox::Information, dlgTitle, strInfo, QMessageBox::Ok);
msgBox->button(QMessageBox::Ok)->setText("確定"); //將"Ok"按鈕改為顯示"確定"
result = msgBox->exec(); //顯示Information彈出對話框
if (result == QMessageBox::Ok)
{
value = true;
}
}
else if (msgType.toLower() == "informationyes")
{
//創(chuàng)建一個Information彈出對話框,添加按鈕:"Ok"
msgBox = new QMessageBox(QMessageBox::Information, dlgTitle, strInfo, QMessageBox::Yes | QMessageBox::No);
msgBox->button(QMessageBox::Yes)->setText("是"); //將"Yes"按鈕改為顯示"是"
msgBox->button(QMessageBox::No)->setText("否"); //將"No"按鈕改為顯示"否"
result = msgBox->exec(); //顯示Information彈出對話框
if (result == QMessageBox::Yes)
{
value = true;
}
}
return value;
}具體使用:

//文件
void MainWindow::slot_file_triggered()
{
//輸入對話框
QString strInfo = skysonya.inputDialog("對話框", "請輸入:");
skysonya.messageBox("warning", "輸入對話框", strInfo);
}自定義類:
#ifndef SKYSONYA_H
#define SKYSONYA_H
#include <QDebug>
#include <QFile>
#include <QInputDialog>
#include <QMessageBox>
#include <QObject>
#include <QPushButton>
#include <QString>
#include <QTextCodec>
enum EncodingFormat
{
ANSI,
UTF16LE,
UTF16BE,
UTF8,
UTF8BOM,
};
class Skysonya : public QObject
{
Q_OBJECT
Q_ENUM(EncodingFormat)
public:
explicit Skysonya(QObject *parent = nullptr);
~Skysonya();
QString doAppAbout(QString appName); //程序關(guān)于信息
bool messageBox(QString msgType, QString dlgTitle, QString strInfo); //中文提示對話框
QString inputDialog(QString dlgTitle, QString labelText, QString textValue = ""); //中文按鈕文本輸入對話框
QTextCodec *getFileCharacterEncoding(const QString &fileName); //獲取文件編碼格式函數(shù)
QString openFileByIOWhole(const QString &fileName); //用QFile打開文件,整體讀取
QString openFileByIOLines(const QString &fileName); //用QFile打開文件,逐行讀取
QString openFileByStreamWhole(const QString &fileName); //用QTextStream讀取文件,整體讀取
QString openFileByStreamLines(const QString &fileName); //用QTextStream讀取文件,逐行讀取
bool saveFileByIOWhole(const QString &fileName, QString text); //用QFile保存文件,整體保存
bool saveFileByStreamWhole(const QString &fileName, QString text); //用QTextStream保存文件,整體保存
private:
QString appVersion; //軟件版本號
QString buildTime; //程序構(gòu)建時間
QString qtVersion; // QT版本號
QString fun_buildTime(); //獲取程序構(gòu)建時間
};
#endif // SKYSONYA_H到此這篇關(guān)于QT實(shí)現(xiàn)QMessageBox中文按鈕示例詳解的文章就介紹到這了,更多相關(guān)QT QMessageBox中文按鈕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(105.由先序和中序遍歷建立二叉樹),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言實(shí)現(xiàn)常見進(jìn)制轉(zhuǎn)換的示例代碼
生活中最常見的進(jìn)制是十進(jìn)制,而有一類編程題會要求將十進(jìn)制轉(zhuǎn)換為其他進(jìn)制,本文將主要講述C語言中常見的幾類進(jìn)制轉(zhuǎn)換問題,希望對大家有所幫助2023-04-04
C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
c語言中十六進(jìn)制轉(zhuǎn)二進(jìn)制顯示的實(shí)現(xiàn)方法
本篇文章對c語言中十六進(jìn)制轉(zhuǎn)二進(jìn)制顯示的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

