Issues with QStyledItemDelegate
-
Hi experts!
I searched for this topic in the forum and I didn't find it, so I'm creating this one.
I made a very simple custom widget (LED) that I tested and works fine:

Now, I'd like to make a custom dialog which uses this widget to show the values of my QAbstractTableModel.
From what I read, I think that I have to use the QStyledItemDelegate class.
Is this correct?
Assuming that "yes it is", I went through the Star Delegate Example.I basically replicate the example, but when I run the code I suddenly discovered that the QTableView widget that I'm using doen't support the setItemDelegate.
Ok, then I replace it with the (used in the Star Delegate Example) QTableWidget widget which... doesn't support the setModel method. 🤦♂️
So before diving into the code etc, etc, is there someone that could please explain me the steps to achieve what I need and the classes to use?
As usual, many thanks to all! 🙇♂️
Kind regards,
AGA -
Hi @JonB,
sorry, ok, I'll report the error code next time.
LedDelegateinherits the QStyledItemDelegate and it is defined as:class LedDelegate(QStyledItemDelegate): def __init__(self, parent=None) -> None: super().__init__(parent) def paint( self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex ) -> None: if index.column == 1: self.led_numeric = Led(index.data(), color=YELLOW) elif index.column == 2: self.led_bool_ok = Led(index.data(), color=GREEN) elif index.column == 3: self.led_bool_error = Led(index.data(), color=RED) else: QStyledItemDelegate.paint(self, painter, option, index) def sizeHint(self, option, index) -> QSize: if index.column()==1: self.led_numeric=Led(index.data(), color=YELLOW) return self.led_numeric.sizeHint() elif index.column()==2: self.led_bool_ok=Led(index.data(), color=GREEN) return self.led_bool_ok.sizeHint() elif index.column()==3: self.led_bool_error=Led(index.data(), color=RED) return self.led_bool_error.sizeHint() else: QStyledItemDelegate.sizeHint(self,option,index)Which is basically the copy of the Star Delegate Example.
Thanks.
-
Hi experts!
I searched for this topic in the forum and I didn't find it, so I'm creating this one.
I made a very simple custom widget (LED) that I tested and works fine:

Now, I'd like to make a custom dialog which uses this widget to show the values of my QAbstractTableModel.
From what I read, I think that I have to use the QStyledItemDelegate class.
Is this correct?
Assuming that "yes it is", I went through the Star Delegate Example.I basically replicate the example, but when I run the code I suddenly discovered that the QTableView widget that I'm using doen't support the setItemDelegate.
Ok, then I replace it with the (used in the Star Delegate Example) QTableWidget widget which... doesn't support the setModel method. 🤦♂️
So before diving into the code etc, etc, is there someone that could please explain me the steps to achieve what I need and the classes to use?
As usual, many thanks to all! 🙇♂️
Kind regards,
AGA -
@superaga
setItemDelegate(QAbstractItemDelegate *) is there.
https://doc.qt.io/qt-5.15/qtableview-members.htmlyour Qt version?
-
Hi @JoeCFD, many thanks for your quick reply!
I'm using the latest PyQt6 (6.4.2).
But you linked me the QAbstractItemView class, which I'm not directly using it.
Should I then replace my QtableView with this?Thanks!
AGA -
@superaga https://doc.qt.io/qt-6/qtableview-members.html
it is there as well. QTableView is a subclass of QAbstractItemView.
What problem do you have when you call setItemDelegate?
setItemDelegate is supported in QTableView. -
Hi @JoeCFD,
I've got this error:

In my view code table_data it is simply defined as:
self.table_data = QTableView()Do I made some other mistake then?
Many thanks!
AGA -
@superaga
Please prefer to paste code rather than screenshots.What is the type of the value assigned to your
LedDelegate?Hi @JonB,
sorry, ok, I'll report the error code next time.
LedDelegateinherits the QStyledItemDelegate and it is defined as:class LedDelegate(QStyledItemDelegate): def __init__(self, parent=None) -> None: super().__init__(parent) def paint( self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex ) -> None: if index.column == 1: self.led_numeric = Led(index.data(), color=YELLOW) elif index.column == 2: self.led_bool_ok = Led(index.data(), color=GREEN) elif index.column == 3: self.led_bool_error = Led(index.data(), color=RED) else: QStyledItemDelegate.paint(self, painter, option, index) def sizeHint(self, option, index) -> QSize: if index.column()==1: self.led_numeric=Led(index.data(), color=YELLOW) return self.led_numeric.sizeHint() elif index.column()==2: self.led_bool_ok=Led(index.data(), color=GREEN) return self.led_bool_ok.sizeHint() elif index.column()==3: self.led_bool_error=Led(index.data(), color=RED) return self.led_bool_error.sizeHint() else: QStyledItemDelegate.sizeHint(self,option,index)Which is basically the copy of the Star Delegate Example.
Thanks.
-
Hi @JonB,
sorry, ok, I'll report the error code next time.
LedDelegateinherits the QStyledItemDelegate and it is defined as:class LedDelegate(QStyledItemDelegate): def __init__(self, parent=None) -> None: super().__init__(parent) def paint( self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex ) -> None: if index.column == 1: self.led_numeric = Led(index.data(), color=YELLOW) elif index.column == 2: self.led_bool_ok = Led(index.data(), color=GREEN) elif index.column == 3: self.led_bool_error = Led(index.data(), color=RED) else: QStyledItemDelegate.paint(self, painter, option, index) def sizeHint(self, option, index) -> QSize: if index.column()==1: self.led_numeric=Led(index.data(), color=YELLOW) return self.led_numeric.sizeHint() elif index.column()==2: self.led_bool_ok=Led(index.data(), color=GREEN) return self.led_bool_ok.sizeHint() elif index.column()==3: self.led_bool_error=Led(index.data(), color=RED) return self.led_bool_error.sizeHint() else: QStyledItemDelegate.sizeHint(self,option,index)Which is basically the copy of the Star Delegate Example.
Thanks.
-
@superaga
Yes, soLedDelegateis a class and you have to pass an instance. Hence the (admittedly cryptic) error message.Hi @JonB,
many thanks! Yes that solved the issue (kudos!).
Sorry, my bad 🤦♂️
I'm so "addicted" to these powerful syntax code analysis that, when they don't provide me the error, I think to something else... Anyway, yes the error message was to cryptic to understand the root cause...Here the trivial solution... A couple of parenthesis () because it was a class, not a property!
... view = MyView(model_table=model_table, model_list=model_list) view.table_data.setItemDelegate(LedDelegate()) ...P.S. probably best to rename this topic from Custom widgets within Model/View to Issues with QStyledItemDelegate.
Also because I was thinking to use the first title for my next question 😅.
Is it ok? Or best is to find another topic title?Many thanks! 🙏
AGA -
S superaga has marked this topic as solved on
-
Hi @JonB,
many thanks! Yes that solved the issue (kudos!).
Sorry, my bad 🤦♂️
I'm so "addicted" to these powerful syntax code analysis that, when they don't provide me the error, I think to something else... Anyway, yes the error message was to cryptic to understand the root cause...Here the trivial solution... A couple of parenthesis () because it was a class, not a property!
... view = MyView(model_table=model_table, model_list=model_list) view.table_data.setItemDelegate(LedDelegate()) ...P.S. probably best to rename this topic from Custom widgets within Model/View to Issues with QStyledItemDelegate.
Also because I was thinking to use the first title for my next question 😅.
Is it ok? Or best is to find another topic title?Many thanks! 🙏
AGA