2016-05-13 62 views
1

該代碼創建一個帶有單個QPushButtonQDialog窗口。單擊該按鈕將彈出帶有三個按鈕的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()過載可用於按鈕排序。默認排序因平臺而異。調用exec()後,使用QMessageBox.clickedButton()查詢使用了哪個按鈕。

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

相關問題