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?
Forum Updated to NodeBB v4.3 + New Features

[PyQt4] QTableWidget Readonly?

Scheduled Pinned Locked Moved Unsolved Qt for Python
22 Posts 6 Posters 12.8k 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.
  • D Offline
    D Offline
    Daniel26
    wrote on 12 Feb 2020, 09:23 last edited by Daniel26 2 Dec 2020, 09:25
    #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

    J J J 3 Replies Last reply 12 Feb 2020, 09:26
    0
    • D Daniel26
      12 Feb 2020, 09:23

      @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 Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 12 Feb 2020, 09:26 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
        12 Feb 2020, 09:23

        @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 Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 12 Feb 2020, 09:40 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 12 Feb 2020, 09:58
        0
        • J J.Hilk
          12 Feb 2020, 09:40

          @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 12 Feb 2020, 09:58 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

          J 1 Reply Last reply 12 Feb 2020, 10:02
          0
          • D Daniel26
            12 Feb 2020, 09:58

            @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

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 12 Feb 2020, 10:02 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
              12 Feb 2020, 09:23

              @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 Offline
              J Offline
              JonB
              wrote on 12 Feb 2020, 10:44 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 12 Feb 2020, 12:53
              1
              • D Offline
                D Offline
                Daniel26
                wrote on 12 Feb 2020, 11:07 last edited by
                #9

                @JonB
                "~" Removes? Ok, thanks.

                @jsulm Thanks for the link.

                1 Reply Last reply
                0
                • J JonB
                  12 Feb 2020, 10:44

                  @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 12 Feb 2020, 12:53 last edited by Daniel26 2 Dec 2020, 13:07
                  #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

                  J 1 Reply Last reply 12 Feb 2020, 14:25
                  0
                  • D Daniel26
                    12 Feb 2020, 12:53

                    @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

                    J Offline
                    J Offline
                    JonB
                    wrote on 12 Feb 2020, 14:25 last edited by JonB 2 Dec 2020, 14:26
                    #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 12 Feb 2020, 14:40 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?

                      S 1 Reply Last reply 12 Feb 2020, 14:55
                      0
                      • D Daniel26
                        12 Feb 2020, 14:40

                        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?

                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 12 Feb 2020, 14:55 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 12 Feb 2020, 15:34 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....

                          J 1 Reply Last reply 12 Feb 2020, 17:43
                          0
                          • D Daniel26
                            12 Feb 2020, 15:34

                            Ok,

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

                            I'm totally confused....

                            J Offline
                            J Offline
                            JonB
                            wrote on 12 Feb 2020, 17:43 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 18 Feb 2020, 10:23 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

                              J 1 Reply Last reply 18 Feb 2020, 10:30
                              0
                              • D Daniel26
                                18 Feb 2020, 10:23

                                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

                                J Offline
                                J Offline
                                JonB
                                wrote on 18 Feb 2020, 10:30 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 18 Feb 2020, 10:51 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.

                                  J 1 Reply Last reply 18 Feb 2020, 11:22
                                  0
                                  • D Daniel26
                                    18 Feb 2020, 10:51

                                    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.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 18 Feb 2020, 11:22 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 18 Feb 2020, 13:59
                                    0
                                    • J JonB
                                      18 Feb 2020, 11:22

                                      @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 18 Feb 2020, 13:59 last edited by
                                      #20

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

                                      J 1 Reply Last reply 18 Feb 2020, 14:32
                                      0
                                      • D Daniel26
                                        18 Feb 2020, 13:59

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

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 18 Feb 2020, 14:32 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
                                        • D Offline
                                          D Offline
                                          Daniel26
                                          wrote on 18 Feb 2020, 15:15 last edited by
                                          #22

                                          Maybe.....fu....I'm blind, sorry.

                                          1 Reply Last reply
                                          0

                                          12/22

                                          12 Feb 2020, 14:40

                                          • Login

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