Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Issues with QStyledItemDelegate
Qt 6.11 is out! See what's new in the release blog

Issues with QStyledItemDelegate

Scheduled Pinned Locked Moved Solved Qt for Python
10 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • superagaS Offline
    superagaS Offline
    superaga
    wrote on last edited by superaga
    #1

    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:

    customLEDs.gif

    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

    JoeCFDJ 1 Reply Last reply
    0
    • superagaS superaga

      Hi @JonB,

      sorry, ok, I'll report the error code next time.

      LedDelegate inherits 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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #8

      @superaga
      Yes, so LedDelegate is a class and you have to pass an instance. Hence the (admittedly cryptic) error message.

      superagaS 1 Reply Last reply
      1
      • superagaS superaga

        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:

        customLEDs.gif

        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

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #2

        @superaga
        setItemDelegate(QAbstractItemDelegate *) is there.
        https://doc.qt.io/qt-5.15/qtableview-members.html

        your Qt version?

        superagaS 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @superaga
          setItemDelegate(QAbstractItemDelegate *) is there.
          https://doc.qt.io/qt-5.15/qtableview-members.html

          your Qt version?

          superagaS Offline
          superagaS Offline
          superaga
          wrote on last edited by
          #3

          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

          JoeCFDJ 1 Reply Last reply
          0
          • superagaS superaga

            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

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #4

            @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.

            superagaS 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @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.

              superagaS Offline
              superagaS Offline
              superaga
              wrote on last edited by
              #5

              Hi @JoeCFD,

              I've got this error:

              35de9016-4cc1-4285-9742-89697d019744-image.png

              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

              JonBJ 1 Reply Last reply
              0
              • superagaS superaga

                Hi @JoeCFD,

                I've got this error:

                35de9016-4cc1-4285-9742-89697d019744-image.png

                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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @superaga
                Please prefer to paste code rather than screenshots.

                What is the type of the value assigned to your LedDelegate?

                superagaS 1 Reply Last reply
                0
                • JonBJ JonB

                  @superaga
                  Please prefer to paste code rather than screenshots.

                  What is the type of the value assigned to your LedDelegate?

                  superagaS Offline
                  superagaS Offline
                  superaga
                  wrote on last edited by
                  #7

                  Hi @JonB,

                  sorry, ok, I'll report the error code next time.

                  LedDelegate inherits 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.

                  JonBJ 1 Reply Last reply
                  0
                  • superagaS superaga

                    Hi @JonB,

                    sorry, ok, I'll report the error code next time.

                    LedDelegate inherits 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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @superaga
                    Yes, so LedDelegate is a class and you have to pass an instance. Hence the (admittedly cryptic) error message.

                    superagaS 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @superaga
                      Yes, so LedDelegate is a class and you have to pass an instance. Hence the (admittedly cryptic) error message.

                      superagaS Offline
                      superagaS Offline
                      superaga
                      wrote on last edited by superaga
                      #9

                      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

                      JonBJ 1 Reply Last reply
                      0
                      • superagaS superaga has marked this topic as solved on
                      • superagaS superaga

                        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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @superaga
                        Yes, you have it right now. This is where Python only detects at runtime what a C++ compiler would reject at compile-time so it would have been clearer.

                        I wouldn't worry about your topic title, it's fine.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved