如何将表放入QMessageBox

时间:2016-07-05 09:19:25

标签: python pyqt

我实际上有两个问题:

  • 我能否拥有"显示详细信息" QMessageBox中的其他位置的按钮(例如QDialog中的例子)?
  • 如何将tableWidget放入QMessageBox?

2 个答案:

答案 0 :(得分:0)

这是将tableWidget放入QMessageBox的示例代码。 如果你不期待这个答案,抱歉。

import sys
from PyQt4 import QtCore, QtGui

class MyMessageBox(QtGui.QMessageBox):
    def __init__(self):
        QtGui.QMessageBox.__init__(self)
        self.setSizeGripEnabled (True)   

        self.setWindowTitle ('Hello MessageBox ???')

        #Create QPushButton in QMessageBox        
        self.addButton (QtGui.QPushButton('Accept'), QtGui.QMessageBox.YesRole)
        self.addButton(QtGui.QPushButton('Reject'), QtGui.QMessageBox.NoRole)
        self.addButton(QtGui.QPushButton('Cancel'), QtGui.QMessageBox.RejectRole)      

        #Add TableWidget to QMessageBox           
        self.addTableWidget (self) 

        #Return values while clicking QPushButton        
        currentClick    = self.exec_() 

        if currentClick==0 :
            print 'Accept'            
        if currentClick==1 :
            print 'Reject'            
        if currentClick==2 :
            print 'Cancel'

    #Create TableWidget 
    def addTableWidget (self, parentItem) :    
        self.tableWidget = QtGui.QTableWidget(parentItem)
        self.tableWidget.setGeometry (QtCore.QRect(0, 0, 540, 250))
        self.tableWidget.setObjectName ('tableWidget')

        self.tableWidget.setColumnCount(5)
        self.tableWidget.setRowCount(6) 

    #Allow resizing of QMessageBox
    def event(self, e):
        result = QtGui.QMessageBox.event(self, e)
        self.setMinimumWidth(0)
        self.setMaximumWidth(16777215)
        self.setMinimumHeight(0)
        self.setMaximumHeight(16777215)        
        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.resize(550, 300)

        return result    

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ex = MyMessageBox ()
    #ex.show()
    sys.exit(app.exec_())

#Thanks, 
#Subin Gopi

答案 1 :(得分:0)

在PyQt5中执行,并添加表小部件的布局,而不是调用QRect。

import sys
from PyQt5 import QtCore, QtWidgets

class MyMessageBox(QtWidgets.QMessageBox):
    def __init__(self):
        QtWidgets.QMessageBox.__init__(self)
        self.setSizeGripEnabled (True)

        self.setWindowTitle('Hello MessageBox ???')
        self.setIcon(self.Question)
        self.setText("Hello MessageBox")
        self.addButton (
            QtWidgets.QPushButton('Accept'), 
            QtWidgets.QMessageBox.YesRole
        )
        self.addButton(
            QtWidgets.QPushButton('Reject'), 
            QtWidgets.QMessageBox.NoRole
        )
        self.addButton(
            QtWidgets.QPushButton('Cancel'), 
            QtWidgets.QMessageBox.RejectRole
        )

        self.addTableWidget (self)

        currentClick = self.exec_()

        if currentClick==0 :
            print ('Accept')
        if currentClick==1 :
            print ('Reject')
        if currentClick==2 :
            print ('Cancel')

    def addTableWidget (self, parentItem) :
        self.l =  QtWidgets.QVBoxLayout()
        self.tableWidget = QtWidgets.QTableWidget(parentItem)
        self.tableWidget.setObjectName ('tableWidget')

        self.tableWidget.setColumnCount(5)
        self.tableWidget.setRowCount(6)
        self.tableWidget.move(30,80)
        self.tableWidget.resize(500, 170)
        self.l.addWidget(self.tableWidget)
        self.setLayout(self.l)

    def event(self, e):
        result = QtWidgets.QMessageBox.event(self, e)
        self.setMinimumWidth(0)
        self.setMaximumWidth(16777215)
        self.setMinimumHeight(0)
        self.setMaximumHeight(16777215)
        self.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, 
            QtWidgets.QSizePolicy.Expanding
        )
        self.resize(550, 300)

        return result

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = MyMessageBox ()
    sys.exit(app.exec_())

输出:

enter image description here