2016-05-13 6 views
1

Код создает окно QDialog с одним QPushButton. Нажатие кнопки вызывает окно QMessageBox с тремя кнопками. Есть ли способ изменить порядок кнопок?Как изменить порядок кнопок в QMessageBox

enter image description here

from PyQt4 import QtGui, QtCore 
app = QtGui.QApplication([]) 

class Dialog(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(Dialog, self).__init__(parent) 

     self.resize(300, 100) 
     self.setLayout(QtGui.QVBoxLayout()) 

     button = QtGui.QPushButton('Submit') 
     button.clicked.connect(self.onclick) 
     self.layout().addWidget(button) 

    def onclick(self): 
     self.close() 
     messagebox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "Title text", "body text", buttons = QtGui.QMessageBox.Ok | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel, parent=self) 
     messagebox.setDefaultButton(QtGui.QMessageBox.No) 
     exe = messagebox.exec_() 
     print exe 

dialog = Dialog() 
dialog.show() 
app.exec_() 

ответ

2

QMessageBox.addButton() перегрузка может быть использована для кнопки заказа. Заказ по умолчанию зависит от платформы. Используйте QMessageBox.clickedButton() после вызова функции exec() для запроса того, какая кнопка была использована.

enter image description here

from PyQt4 import QtGui, QtCore 
import sys 
app = QtGui.QApplication([]) 

class Dialog(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(Dialog, self).__init__(parent) 

     self.resize(300, 100) 
     self.setLayout(QtGui.QVBoxLayout()) 

     button = QtGui.QPushButton('Submit') 
     button.clicked.connect(self.onclick) 
     self.layout().addWidget(button) 

    def onclick(self): 
     self.close() 
     message = "<font size = 5 color = gray > Rich Html Title </font> <br/><br/>The clickable link <a href='http://www.google.com'>Google.</a> The lower and upper case text." 
     messagebox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "title", message, parent=self) 
     messagebox.addButton("ResetRole Left Most", QtGui.QMessageBox.ResetRole) 
     messagebox.addButton("ApplyRole Left", QtGui.QMessageBox.ApplyRole) 
     messagebox.addButton("RejectRole Right", QtGui.QMessageBox.RejectRole) 
     messagebox.addButton("NoRole Right Most", QtGui.QMessageBox.NoRole) 
     exe = messagebox.exec_() 
     print 'exe: %s clickedButton: %s'%(exe, messagebox.clickedButton()) 

dialog = Dialog() 
dialog.show()  
app.exec_() 
+0

Каждый знает, что происходит здесь с кнопки текста проклейки на OSX? Он не сосредоточен. – eephillip