Solved How to interact with widgets that are in cells of a tablewidget.
-
EDIT:
Read this intro on qt model/view it helped me out alot!
Further expansion on model/view
For my problem I also realized that I had embedded MyCell() into my tablewidget cells and I was trying to access the LineEdits. The better thing to try would be to edit MyCell() widget to convey the 2 lineedits signals/data to the tablewidget. But this wasnt a good idea to start off with, I should have started with a tableview and a model on my database instead.
I have made a widget with 2 lineedits and i have set some cells in my tablewidget to have that widget. Now I want to know how do i interact with the lineedits input/output?
I want to make a sort of calendar for taking attendance with the amount of overtime worked. So if someone enters any value into the cells where i have absent / present or 0 written. my tablewidget doesnt seem to be firing any signals for either cellactivated,pressed,clicked,selected, nothing. its as if the widget is being selected without selecting the cell itself?
from PySide2 import QtWidgets, QtGui,QtCore import sys class MyCell(QtWidgets.QWidget): def __init__(self): super().__init__() self.top = 'NOT SET' self.bot = 'NOT SET' layout = QtWidgets.QVBoxLayout() top = QtWidgets.QLineEdit() top.setAlignment(QtCore.Qt.AlignCenter) bot = QtWidgets.QLineEdit() bot.setAlignment(QtCore.Qt.AlignCenter) top.editingFinished.connect(lambda: self.submit(top.text(), 'top')) bot.editingFinished.connect(lambda: self.submit(bot.text(), 'bot')) layout.addWidget(top) layout.addWidget(bot) layout.setContentsMargins(0,0,0,0) layout.setSpacing(0) self.setLayout(layout) def submit(self, str, toporbot): if toporbot == 'top': self.top = str elif toporbot == 'bot': self.bot = str #class myDelegate(QtWidgets.QStyledItemDelegate): # def __init__(self): # super().__init__() # def paint(self, painter, option, index): # pass # def sizeHint(self, option, index): # pass #def createEditor(self, widget, option, index): # pass # def setEditorData(self, widget, index): # pass # def setModelData(self, widget, model, index): # pass class MyTableWidget(QtWidgets.QTableWidget): def __init__(self, month, whichhalf): super().__init__() self.month = month self.half = whichhalf self.setColumnCount(6) self.setRowCount(6) for row in range(self.rowCount()): for col in range(2, self.columnCount()): self.setCellWidget(row,col,MyCell()) self.resizeColumnsToContents() self.resizeRowsToContents() app = QtWidgets.QApplication() w = MyTableWidget(3,1) m = MyCell() m.show() w.show() exitcode = app.exec_() sys.exit(exitcode)
-
Hi and welcome to the forums.
The LineEdits have no idea that they are place over a cell/tablewidget so they will consume signal and events as normally and
not tell TableWidget about it.
I have not seen any good soultion when using setWidget other than finding the cell liek they do here
https://stackoverflow.com/questions/37523086/how-to-get-current-row-of-qtablewidget-if-i-clicked-on-its-childAlternative would be an https://doc.qt.io/qt-5/qstyleditemdelegate.html
where you would create your widgets in createEditor
and paint the texts in paint and handle the data in setModelData -
hmmm. Thanks a lot! I guess I will have to experiment with The delegate and model structure. I thought I might have to use it so i had comented it out. I have no experience with gui or C++ so the docs seem very cryptic to me. Do you know any resources where this delegate / model usage examples are given in Python not c++.
-
Hi
I understand. i have same feeling seeing the same in python :)
Im have seen some on stackoverflow.
Sadly im not good enough with python to write it.in any case you would just return your normal CellWidget with layout and 2 lineedits for createEditor
class ItemDelegate(QtGui.QStyledItemDelegate): def createEditor(self, parent, option, index): item_data = str(index.data().toString()) editor = MyCell(...) return editor
but im not sure with the other needed functions :(
-
@mrjj Thankyou very very much for your help! I guess its time for me to get my hands dirty and do some more
sufferingexperimentation :D