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. [PyQt4] QTableWidget Readonly?
QtWS25 Last Chance

[PyQt4] QTableWidget Readonly?

Scheduled Pinned Locked Moved Unsolved Qt for Python
22 Posts 6 Posters 9.5k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Sure thing, use the setFlags method of QTableWidgetItem and Qt.ItemIsEditable flag.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    3
    • D Offline
      D Offline
      Daniel26
      wrote on last edited by Daniel26
      #3

      @SGaist said in [PyQt4] QTableWIdget Readonly?:

      Qt.ItemIsEditable

      Hello,

      so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
      Thats all?
      Is there an overview over the flags?
      Regards

      Daniel

      jsulmJ J.HilkJ JonBJ 3 Replies Last reply
      0
      • D Daniel26

        @SGaist said in [PyQt4] QTableWIdget Readonly?:

        Qt.ItemIsEditable

        Hello,

        so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
        Thats all?
        Is there an overview over the flags?
        Regards

        Daniel

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Daniel26 said in [PyQt4] QTableWidget Readonly?:

        Is there an overview over the flags?

        Yes, in the link @SGaist posted

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Daniel26

          @SGaist said in [PyQt4] QTableWIdget Readonly?:

          Qt.ItemIsEditable

          Hello,

          so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
          Thats all?
          Is there an overview over the flags?
          Regards

          Daniel

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #5

          @Daniel26 said in [PyQt4] QTableWidget Readonly?:

          so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?

          I assume this is a typo, because @SGaist said Qt.ItemIsEditable


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          D 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @Daniel26 said in [PyQt4] QTableWidget Readonly?:

            so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?

            I assume this is a typo, because @SGaist said Qt.ItemIsEditable

            D Offline
            D Offline
            Daniel26
            wrote on last edited by
            #6

            @J-Hilk I don't want it editable, so I didn't set Qt.ItemIsEditable.
            Or is that wrong?

            @jsulm In the link I Can't find an overview of the QT-Flags.

            Regards

            Daniel

            jsulmJ 1 Reply Last reply
            0
            • D Daniel26

              @J-Hilk I don't want it editable, so I didn't set Qt.ItemIsEditable.
              Or is that wrong?

              @jsulm In the link I Can't find an overview of the QT-Flags.

              Regards

              Daniel

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Daniel26 You need to click on Qt::ItemFlags: https://doc.qt.io/qt-5/qt.html#ItemFlag-enum

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Daniel26

                @SGaist said in [PyQt4] QTableWIdget Readonly?:

                Qt.ItemIsEditable

                Hello,

                so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
                Thats all?
                Is there an overview over the flags?
                Regards

                Daniel

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

                @Daniel26 said in [PyQt4] QTableWidget Readonly?:

                so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?

                You can do this, if you know you want it to be selectable and enabled. A more robust/generic to just switch off editable, no matter what other flags are already there, is:

                twi.setFlags(twi.flags() & ~QtCore.Qt.ItemIsEditable)
                
                D 1 Reply Last reply
                1
                • D Offline
                  D Offline
                  Daniel26
                  wrote on last edited by
                  #9

                  @JonB
                  "~" Removes? Ok, thanks.

                  @jsulm Thanks for the link.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Daniel26 said in [PyQt4] QTableWidget Readonly?:

                    so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?

                    You can do this, if you know you want it to be selectable and enabled. A more robust/generic to just switch off editable, no matter what other flags are already there, is:

                    twi.setFlags(twi.flags() & ~QtCore.Qt.ItemIsEditable)
                    
                    D Offline
                    D Offline
                    Daniel26
                    wrote on last edited by Daniel26
                    #10

                    @JonB
                    self.addip_tw.setFlags(self.addip_tw.flags() & ~QtCore.Qt.ItemIsEditable)
                    AttributeError: 'QTableWidget' object has no attribute 'setFlags'

                    What did I wrong? Is setFlags Qt5 only?

                    Regards

                    Daniel

                    JonBJ 1 Reply Last reply
                    0
                    • D Daniel26

                      @JonB
                      self.addip_tw.setFlags(self.addip_tw.flags() & ~QtCore.Qt.ItemIsEditable)
                      AttributeError: 'QTableWidget' object has no attribute 'setFlags'

                      What did I wrong? Is setFlags Qt5 only?

                      Regards

                      Daniel

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

                      @Daniel26
                      As @SGaist gave you the link earlier, https://doc.qt.io/qt-5/qtablewidgetitem.html#setFlags is a method of each QTableWidgetItem. I imagine your self.addip_tw is the whole of the QTableWidget, not a QTableWidgetItem as my variable twi is intended to be.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        Daniel26
                        wrote on last edited by
                        #12

                        Ok, sorry for that stupid question, but I'm new to pyQt and object orientated programming.
                        QTableWidgetItem is only a cell of the QTableWidget? is that correct?

                        I create an instance of QTableWidget as self.addip_tw and added the self.addip_tw to a QGridLayout.
                        So I have to do the setFlags for every cell in my Table?

                        SGaistS 1 Reply Last reply
                        0
                        • D Daniel26

                          Ok, sorry for that stupid question, but I'm new to pyQt and object orientated programming.
                          QTableWidgetItem is only a cell of the QTableWidget? is that correct?

                          I create an instance of QTableWidget as self.addip_tw and added the self.addip_tw to a QGridLayout.
                          So I have to do the setFlags for every cell in my Table?

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @Daniel26 said in [PyQt4] QTableWidget Readonly?:

                          QTableWidgetItem is only a cell of the QTableWidget? is that correct?

                          Not exactly, it's an object that will be stored in the model coming with QTableWidget and provide all the necessary data to be rendered by a cell of the table.

                          A QTableWidget is a QTableView + a model all packed in one.

                          You should check the Model/View Programming chapter in Qt's documentation for a good overview of how it is working.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            Daniel26
                            wrote on last edited by
                            #14

                            Ok,

                            so I set every Cell with an empty QWidgetItem so I can set it to readOnly?

                            I'm totally confused....

                            JonBJ 1 Reply Last reply
                            0
                            • D Daniel26

                              Ok,

                              so I set every Cell with an empty QWidgetItem so I can set it to readOnly?

                              I'm totally confused....

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

                              @Daniel26
                              You can indeed do it as @Denni-0 has said. To my mind, it doesn't really make the cells read-only, but it does stop the user doing anything to edit :) And it's only one line!

                              For making the items not-editable, you could look at the code in https://stackoverflow.com/questions/7727863/how-to-make-a-cell-in-a-qtablewidget-read-only/ which is the accepted solution. Other answers there also show @Denni-0 's approach.

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                Daniel26
                                wrote on last edited by
                                #16

                                hello @ all,

                                thanks for the help. I've got it, did it with the QTableWidget.
                                But how can I check, if a column is empty?
                                A simple "if self.addip_tw.item(x,y)" won't work :)

                                Regards

                                Daniel

                                JonBJ 1 Reply Last reply
                                0
                                • D Daniel26

                                  hello @ all,

                                  thanks for the help. I've got it, did it with the QTableWidget.
                                  But how can I check, if a column is empty?
                                  A simple "if self.addip_tw.item(x,y)" won't work :)

                                  Regards

                                  Daniel

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

                                  @Daniel26
                                  What do you mean by "if a column is empty"? Plus, why do you care if you are choosing to go for the simplest QTreeView.setEditTriggers(QAbstractItemView.NoEditTriggers)?

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    Daniel26
                                    wrote on last edited by
                                    #18

                                    Sorry, I ment if a cell is empty.

                                    I store the content of the cells into a dictionary, but only if the first cell of the line is not empty.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • D Daniel26

                                      Sorry, I ment if a cell is empty.

                                      I store the content of the cells into a dictionary, but only if the first cell of the line is not empty.

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

                                      @Daniel26
                                      The I don't understand why you say

                                      A simple "if self.addip_tw.item(x,y)" won't work :)

                                      since https://doc.qt.io/qt-5/qtablewidget.html#item does return nullptr for no item.

                                      D 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @Daniel26
                                        The I don't understand why you say

                                        A simple "if self.addip_tw.item(x,y)" won't work :)

                                        since https://doc.qt.io/qt-5/qtablewidget.html#item does return nullptr for no item.

                                        D Offline
                                        D Offline
                                        Daniel26
                                        wrote on last edited by
                                        #20

                                        @JonB
                                        if self.addip_tw.item[row,0]:
                                        TypeError: 'builtin_function_or_method' object has no attribute 'getitem'

                                        JonBJ 1 Reply Last reply
                                        0
                                        • D Daniel26

                                          @JonB
                                          if self.addip_tw.item[row,0]:
                                          TypeError: 'builtin_function_or_method' object has no attribute 'getitem'

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

                                          @Daniel26
                                          Please look at your code before saying things "don't work". Could this be because item() is a function and not an array?

                                          1 Reply Last reply
                                          2

                                          • Login

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