zoukankan      html  css  js  c++  java
  • Python: PySide(PyQt)QMessageBox按钮显示中文

    习惯了Delphi、c#调用系统MessageBox本地化显示,待用PySide调用时,Qt原生提示对话框,默认以英文显示。

    如何本地化呢?

    参考些资料,加以摸索,实现所需效果。并可根据此思路,设计自己所需要的MessageBox封装。

        QTextCodec.setCodecForTr(QTextCodec.codecForName("UTF-8"))
        box = QMessageBox(QMessageBox.Question, self.tr("提示"), self.tr("您确定要退出吗?"), QMessageBox.NoButton, self)
        yr_btn = box.addButton(self.tr(""), QMessageBox.YesRole)
        box.addButton(self.tr(""), QMessageBox.NoRole)
        box.exec_()
        if box.clickedButton() == yr_btn:
            print 'Bye bye...'
            return
        else:
            print '继续...'

    效果如下图示:

    直接以.exec_()判断,[是]按钮返回0,尚不知如何与QMesageBox.YesRole对应。但若使用QMessageBox.AcceptRole与QMessageBox.RejectRole则可以。

    下面代码,摘自PySide自带例子:

        MESSAGE = "<p>Message boxes have a caption, a text, and up to three " 
                  "buttons, each with standard or custom texts.</p>" 
                  "<p>Click a button to close the message box. Pressing the Esc " 
                  "button will activate the detected escape button (if any).</p>"
    
        msgBox = QMessageBox(QMessageBox.Question,
                "QMessageBox.warning()", MESSAGE,
                QMessageBox.NoButton, self)
        msgBox.addButton("Save &Again", QMessageBox.AcceptRole)
        msgBox.addButton("&Continue", QMessageBox.RejectRole)
        if msgBox.exec_() == QMessageBox.AcceptRole:
            print "Save Again"
        else:
            print "Continue"

    显示如图:

    另一种分步方案:

        box = QtGui.QMessageBox()
        box.setIcon(QtGui.QMessageBox.Question)
        box.setWindowTitle('Kaydet!')
        box.setText('Kaydetmek İstediğinize Emin Misiniz?')
        box.setStandardButtons(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        buttonY = box.button(QtGui.QMessageBox.Yes)
        buttonY.setText('Evet')
        buttonN = box.button(QtGui.QMessageBox.No)
        buttonN.setText('Iptal')
        box.exec_()
    
        if box.clickedButton() == buttonY:
            print 'YES pressed'
        elif box.clickedButton() == buttonN:
           print 'NO pressed'

    谁可知之?大抵其官方文档可见些解释吧。一些是点击触发事件,一些仅是样式类似。

    参考资料

    QMessageBox - PySide v1.0.7 documentation

    QDialogButtonBox Class | Qt 4.8

  • 相关阅读:
    android NDK开发及调用标准linux动态库.so文件
    android ndk增加对stl的支持
    Android中JNI的使用方法
    OCP-1Z0-052-V8.02-55题
    OCP-1Z0-053-V12.02-162题
    OCP-1Z0-052-V8.02-52题
    OCP-1Z0-052-V8.02-50题
    OCP-1Z0-052-V8.02-49题
    Android 中Java 和C/C++的相互调用方法
    用JNI调用C或C++动态联接库入门
  • 原文地址:https://www.cnblogs.com/crwy/p/9599361.html
Copyright © 2011-2022 走看看