In this PyQt5 tutorial, I will be covering how to insert a checkable QComboBox widget.

What is PyQt?

PyQt is a library created by Riverbank based on the Qt framework to let you build desktop applications in Python. (An alternative is PySide2, by Qt the company itself).

Qt framework itself is written in C++, and the framework is also available in other programming languages, such as Java and C++. By using Python, we can build applications much more rapidly.

🔔 Subscribe: https://www.youtube.com/channel/UCvVZ19DRSLIC2-RUOeWx8ug

Source Code:


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QPushButton, QVBoxLayout
from PyQt5.QtCore import Qt

class CheckableComboBox(QComboBox):
	def __init__(self):
		super().__init__()
		self._changed = False

		self.view().pressed.connect(self.handleItemPressed)

	def setItemChecked(self, index, checked=False):
		item = self.model().item(index, self.modelColumn()) # QStandardItem object

		if checked:
			item.setCheckState(Qt.Checked)
		else:
			item.setCheckState(Qt.Unchecked)

	def handleItemPressed(self, index):
		item = self.model().itemFromIndex(index)

		if item.checkState() == Qt.Checked:
			item.setCheckState(Qt.Unchecked)
		else:
			item.setCheckState(Qt.Checked)
		self._changed = True


	def hidePopup(self):
		if not self._changed:
			super().hidePopup()
		self._changed = False

	def itemChecked(self, index):
		item = self.model().item(index, self.modelColumn())
		return item.checkState() == Qt.Checked

class MyApp(QWidget):
	def __init__(self):
		super().__init__()
		self.resize(500, 150)

		mainLayout = QVBoxLayout()

		self.combo = CheckableComboBox()
		mainLayout.addWidget(self.combo)

		for i in range(6):
			self.combo.addItem('Item {0}'.format(str(i)))
			self.combo.setItemChecked(i, False)
		
		btn = QPushButton('Print Values')
		btn.clicked.connect(self.getValue)
		mainLayout.addWidget(btn)

		self.setLayout(mainLayout)

	def getValue(self):
		for i in range(self.combo.count()):
			print('Index: {0} is checked {1}'.format(i, self.combo.itemChecked(i)))

if __name__ == '__main__':
	app = QApplication(sys.argv)
	app.setStyleSheet('''
		QWidget {
			font-size: 40px;
		}
	''')

	myApp = MyApp()
	myApp.show()

	app.exit(app.exec_())	

#python #pyqt5

How to Create Checkable ComboBox Widget | PyQt5 Tutorial
9.50 GEEK