Reputation: 397
Im using Python3.3 and PyQt4. I want to add several checkboxes to an item in a qtlistwidget. I was able to add one with the following code:
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
But im not able to add more to this item, i tried it with something like:
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsUserCheckable)
or
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsUserCheckable)
but both are not working. Does anyone know if its possible to add more checkboxes or if it isnt how i can workaround that. Thanks alot!
Upvotes: 1
Views: 3563
Reputation:
QListWidgetItem
is not used in that way. Each item will display a single checkbox, no matter how many times you set the QtCore.Qt.ItemIsUserCheckable
flag. If you need to display multiple checkboxes in the same row QTableWidget
is probably what you are looking for:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#---------
# IMPORT
#---------
from PyQt4 import QtGui, QtCore
#---------
# MAIN
#---------
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
numberRows = 1
numberColumns = 2
self.tableWidget = QtGui.QTableWidget(self)
self.tableWidget.setRowCount(numberRows)
self.tableWidget.setColumnCount(numberColumns)
for rowNumber in range(numberRows):
for columnNumber in range(numberColumns):
item = QtGui.QTableWidgetItem("item {0} {1}".format(rowNumber, columnNumber))
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.tableWidget.setItem(rowNumber, columnNumber, item)
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.tableWidget)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.resize(333, 111)
main.show()
sys.exit(app.exec_())
Upvotes: 2