[英]How to identify QListWidget in Python/Qt
With a row of QListWidgets I wonder if there is a simple way to label each so the user would be able to say which List is which. 对于一排QListWidgets,我想知道是否有一种简单的方法来标记每个List,以便用户能够说出哪个List是哪个。 Here is a dialog's screenshot a code posted below results.
这是一个对话框的屏幕截图,其中的代码发布在结果下方。 I avoid using any widgets outside of QListWidgets().
我避免使用QListWidgets()之外的任何小部件。 Ideal solution would be solved utilizing QListWidgets itself.
理想的解决方案将利用QListWidgets本身解决。 It would be great if there is a way to place a text line-label similar to those available for QGroupBox with .setTitle('myString').
如果可以通过.setTitle('myString')放置类似于QGroupBox可用的文本行标签,那就太好了。 At very least an ability to place a label as a first list item would be sufficient too...
至少具有将标签放置为第一列表项的能力也足够了...
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.listLayout.addWidget(self.listA)
self.listLayout.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()
Unfortunately there's no (Qt-native) way to label a QListView
(on which QListWidget
is based). 不幸的是,没有(Qt本机)标记
QListView
( QListWidget
所基于的)的方法。 If your really don't want additional widgets, I would instead use a single-column QTableWidget
, and put the list title in the column header. 如果您确实不希望使用其他小部件,则可以使用单列
QTableWidget
,并将列表标题放在列标题中。 QTableWidget
and QListWidget
work pretty similarly, so this probably won't break too much of your existing code. QTableWidget
和QListWidget
工作原理非常相似,因此这可能不会破坏您现有代码的太多。
An example based on yours: 根据您的示例:
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
Here is my attempt to achieve it without abandoning QListWidget()... utilizing layout's .insertLayout() method to attach QLabel without losing GUI space usually taken by QGroupBox()... 这是我在不放弃QListWidget()的情况下实现它的尝试...利用布局的.insertLayout()方法附加QLabel而不丢失通常由QGroupBox()占用的GUI空间...
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.