Reputation: 581
I have a QTableWidget
and a Qcombobox
. I want to get text values from every single cell in first column 1
and when ever a user inserts a new value, that would assign and set automatically to Qcombobox
. What I mean by every single cell is to get available values, when a cell is empty then do nothing.
Visualization:
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.setLayout(QtWidgets.QVBoxLayout())
combo = QtWidgets.QComboBox(self)
self.layout().addWidget(combo)
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.table = QtWidgets.QTableWidget(10, 2, self)
self.comboBox = QtWidgets.QComboBox()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.table)
names = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']
for index, name in enumerate(names):
self.table.setItem(index, 0, QtWidgets.QTableWidgetItem(name))
class Layout(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Layout, self).__init__()
self.comb = Widget()
self.table = Window()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.table)
layout.addWidget(self.comb)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Layout()
window.setGeometry(600, 200, 300, 300)
window.show()
sys.exit(app.exec_())
I am not sure about connection and slot solution will give a good choose?
Upvotes: 1
Views: 100
Reputation: 243887
In this it is better to pass as a model so that it is automatically updated without the unnecessary use of signals. But since you want no empty elements to be displayed, use QSortFilterProxyModel with an appropriate regex:
from PyQt5 import QtCore, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.table = QtWidgets.QTableWidget(10, 2)
names = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']
for index, name in enumerate(names):
self.table.setItem(index, 0, QtWidgets.QTableWidgetItem(name))
proxy = QtCore.QSortFilterProxyModel(self)
proxy.setSourceModel(self.table.model())
proxy.setFilterRegExp(r"^(?!\s*$).+")
self.comboBox = QtWidgets.QComboBox()
self.comboBox.setModel(proxy)
self.comboBox.setModelColumn(0)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.table)
layout.addWidget(self.comboBox)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 200, 300, 300)
window.show()
sys.exit(app.exec_())
Upvotes: 1