I\'m trying to do this with the check-box.
Sadly is made for C++ and any adaptation of the code for Python has the result this error: \'QWidget\' object is not callabl
Here is an example from ekhumoro to find what is checked when it s being clicked :
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
QtGui.QWidget.__init__(self)
self.table = QtGui.QTableWidget(rows, columns, self)
for column in range(columns):
for row in range(rows):
item = QtGui.QTableWidgetItem('Text%d' % row)
if row % 2:
item.setFlags(QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Unchecked)
self.table.setItem(row, column, item)
self.table.itemClicked.connect(self.handleItemClicked)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
self._list = []
def handleItemClicked(self, item):
if item.checkState() == QtCore.Qt.Checked:
print('"%s" Checked' % item.text())
self._list.append(item.row())
print(self._list)
else:
print('"%s" Clicked' % item.text())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(6, 3)
window.resize(350, 300)
window.show()
sys.exit(app.exec_())
But you can also iterate on your rows and use .findChild(type(QtGui.QCheckBox())).isChecked()
on the proper column
such as :
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
QtGui.QWidget.__init__(self)
self.table = QtGui.QTableWidget(rows, columns, self)
for row in range(rows):
qwidget = QtGui.QWidget()
checkbox = QtGui.QCheckBox()
checkbox.setCheckState(QtCore.Qt.Unchecked)
qhboxlayout = QtGui.QHBoxLayout(qwidget)
qhboxlayout.addWidget(checkbox)
qhboxlayout.setAlignment(Qt.AlignCenter)
qhboxlayout.setContentsMargins(0, 0, 0, 0)
self.table.setCellWidget(row, 0, qwidget)
self.table.setItem(row, 1, QtGui.QTableWidgetItem(str(row)))
layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton()
self.button.setObjectName("loadButton")
layout.addWidget(self.table)
layout.addWidget(self.button)
self.button.clicked.connect(self.ButtonClicked)
def ButtonClicked(self):
checked_list = []
for i in range(self.table.rowCount()):
if self.table.cellWidget(i, 0).findChild(type(QtGui.QCheckBox())).isChecked():
checked_list.append(self.table.item(i, 1).text())
print checked_list
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(3, 2)
window.resize(350, 300)
window.show()
sys.exit(app.exec_())
You could consider using the Qt Designer, so that you can:
After you get your desired window (i.e. the file with a .ui
extension), you can use the pyuic5
[1] utility, which will generate a Python file from the UI file. From the man page
pyuic5 - compile Qt5 user interfaces to Python code
Simple steps with example
You should use the Qt Designer and save the .ui file.
If your .ui file is named mainwindow.ui
then you can use the command:
pyuic5 mainwindow.ui -o mainwindow.py
Get your Python code to use the generated Python-based UI file.
from PyQt5.QtWidgets import QMainWindow
from mainwindow import Ui_MainWindow # <<--- important
# Set up the user interface from Designer.
win = QMainWindow()
gui = Ui_MainWindow()
gui.setupUi(win)
As you can see above, you need to import the class representing the UI from the Python module that was generated, in our case being the mainwindow.py
file from the command in the prev. step.
The class is automatically prefixed with Ui_
by the utility. You then instantiate a QMainWindow
and the generated class and use Qt's built-in method setupUi
for it to incorporate all the widgets, etc.
Important: Every time you update your window in Qt Designer, you'll need to repeat step #2. Considering the amount of time saved by using the Designer directly, this shouldn't be a problem.
Note: You can use the generated mainwindow.py
file to read the code and see how the desired layout was achieved. This should be useful if you really do not want to continue using this approach.
[1] The pyuic5
command is found within the pyqt5-dev-tools
package, so a sudo apt-get install pyqt5-dev-tools
should do it.