Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python/Qt中识别QListWidget_Python_Qt_Pyqt - Fatal编程技术网

如何在Python/Qt中识别QListWidget

如何在Python/Qt中识别QListWidget,python,qt,pyqt,Python,Qt,Pyqt,对于一行QListWidgets,我想知道是否有一种简单的方法来标记每个控件,以便用户能够说出哪个列表是哪个。 这是一个对话框的截图,下面是一段代码。 我避免使用QListWidgets()之外的任何小部件。理想的解决方案是利用QListWidgets本身来解决。如果有一种方法可以放置一个类似于QGroupBox的文本行标签,并带有.setTitle('myString'),那就太好了。至少,将标签作为第一个列表项的能力也足够了 不幸的是,没有(Qt原生)方法来标记QListView(基于

对于一行QListWidgets,我想知道是否有一种简单的方法来标记每个控件,以便用户能够说出哪个列表是哪个。 这是一个对话框的截图,下面是一段代码。 我避免使用QListWidgets()之外的任何小部件。理想的解决方案是利用QListWidgets本身来解决。如果有一种方法可以放置一个类似于QGroupBox的文本行标签,并带有.setTitle('myString'),那就太好了。至少,将标签作为第一个列表项的能力也足够了




不幸的是,没有(Qt原生)方法来标记
QListView
(基于
QListWidget
)。如果您真的不需要额外的小部件,我将使用单个列
QTableWidget
,并将列表标题放在列标题中
QTableWidget
QListWidget
的工作原理非常相似,因此这可能不会破坏太多现有代码

基于您的示例:

class MyApp(object):
    def __init__(self):

        # snipped

        self.listA = self.prepareTableWidget('List A')
        self.listB = self.prepareTableWidget('List B')

        # snipped

    def prepareTableWidget(self, name):
        table = QtGui.QTableWidget()
        table.setColumnCount(1)
        table.setHorizontalHeaderLabels([name])
        table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        return table

    # snipped

以下是我在不放弃QListWidget()的情况下实现它的尝试。。。利用layout的.insertLayout()方法附加QLabel而不丢失通常由QGroupBox()占用的GUI空间



只要建议的QtGui.QTableWidget()支持“最流行的”QListWidget()功能,我就可以。。。谢谢
class MyApp(object):
    def __init__(self):

        # snipped

        self.listA = self.prepareTableWidget('List A')
        self.listB = self.prepareTableWidget('List B')

        # snipped

    def prepareTableWidget(self, name):
        table = QtGui.QTableWidget()
        table.setColumnCount(1)
        table.setHorizontalHeaderLabels([name])
        table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        return table

    # snipped
from PyQt4 import QtGui, QtCore

class MyApp(object):
    def __init__(self):
        super(MyApp, self).__init__()
        app = QtGui.QApplication(sys.argv)
        self.mainWidget = QtGui.QWidget()
        self.mainLayout = QtGui.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.groupbox = QtGui.QGroupBox()
        self.groupbox.setTitle('My Groupbox')
        self.layout = QtGui.QVBoxLayout()
        self.groupbox.setLayout(self.layout)

        self.listGroupbox = QtGui.QGroupBox()
        self.listLayout = QtGui.QHBoxLayout()
        self.listGroupbox.setLayout(self.listLayout)
        self.listA=QtGui.QListWidget()
        self.listB=QtGui.QListWidget()

        self.subLayoutA=QtGui.QVBoxLayout()        
        self.listLayout.insertLayout(0,self.subLayoutA)
        self.subLayoutA.addWidget(QtGui.QLabel('Label A') )
        self.subLayoutA.addWidget(self.listA)

        self.subLayoutB=QtGui.QVBoxLayout()
        self.listLayout.insertLayout(1,self.subLayoutB)
        self.subLayoutB.addWidget(QtGui.QLabel('Label B') )
        self.subLayoutB.addWidget(self.listB)


        self.layout.addWidget(self.listGroupbox) 

        self.okButton = QtGui.QPushButton('OK')
        self.okButton.clicked.connect(self.OK) 
        self.layout.addWidget(self.okButton)                      
        self.mainLayout.addWidget(self.groupbox)
        self.mainWidget.show()
        sys.exit(app.exec_())

    def OK(self):
        print 'Ok'    
if __name__ == '__main__':
    MyApp()