QMessageBox 中的 OK 按鈕改為中文“確定”


有很多資料用於將 QMessageBox 的 OK 改為中文。但大多很麻煩。本文提供一個簡便方法,用於定制 QMessageBox 的按鈕,包括將其翻譯成中文顯示。
 
QMessageBox  對其內部的 Button 進行維護,用戶可以使用 addButton() 方法,以及 removeButton() 方法添加或者移除按鈕。每個 Button 都有個角色屬性(enum QMessageBox::ButtonRole),用於標識該 Button 的用途。
角色屬性列表如下:
Constant Value Description
QMessageBox::InvalidRole -1 The button is invalid.
QMessageBox::AcceptRole 0 Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole 1 Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole 2 Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox::ActionRole 3 Clicking the button causes changes to the elements within the dialog.
QMessageBox::HelpRole 4 The button can be clicked to request help.
QMessageBox::YesRole 5 The button is a "Yes"-like button.
QMessageBox::NoRole 6 The button is a "No"-like button.
QMessageBox::ApplyRole 8 The button applies current changes.
QMessageBox::ResetRole 7 The button resets the dialog's fields to default values.
因此,用戶可以使用 addButton() 和 removeButton() 方法,結合角色屬性,來自定義 QMessageBox 的Button。
另外,用戶可以通過 button() 或者 buttons() 方法獲得“按鍵”或者“按鍵列表”。
QMessageBox 還提供了一個復雜的構造函數,用於在創建消息對話框時就對其進行定制,改構造函數原型如下:
 
QMessageBox::QMessageBox(Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)
 
因此,我們得到了一個將 “OK” 按鍵變成“確定”按鍵的最簡單思路:使用 button() 方法獲得 QAbstractButton 按鍵,然后使用 QAbstractButton 的 setText() 方法將其重新命名為“確定”,示例代碼如下:
    WrrMsg = new QMessageBox(QMessageBox::NoIcon, title, msg, QMessageBox::Ok, 0);
    if(NULL!=WrrMsg->button(QMessageBox::Ok))
    {
        WrrMsg->button(QMessageBox::Ok)->setText("確 定");
    }
其中,title為QString類型的窗口標題,msg為QString類型的消息內容,QMessageBox()具體信息請參見Qt的相關幫助文檔。
 
http://blog.csdn.net/desert187/article/details/40302049


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM