Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?
Forum Updated to NodeBB v4.3 + New Features

How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 4 Posters 4.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.
  • K Offline
    K Offline
    Kuji
    wrote on 10 Dec 2022, 15:18 last edited by Kuji 12 Oct 2022, 15:18
    #1

    I have following solution everything works right, but when i click on enabled cell in console appears line "edit: editing failed"

        QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                         ui->tableWidgetAdminEmployee, QOverload<const QModelIndex&>::of(&QTableWidget::edit));
        for(int i = 0; i < ui->tableWidgetAdminEmployee->rowCount(); i++)
        {
            ui->tableWidgetAdminEmployee->item(i,0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
            ui->tableWidgetAdminEmployee->item(i,7)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
            ui->tableWidgetAdminEmployee->item(i,8)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
            ui->tableWidgetAdminEmployee->item(i,9)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
        }
    
    J 1 Reply Last reply 10 Dec 2022, 17:02
    0
    • K Kuji
      10 Dec 2022, 15:18

      I have following solution everything works right, but when i click on enabled cell in console appears line "edit: editing failed"

          QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                           ui->tableWidgetAdminEmployee, QOverload<const QModelIndex&>::of(&QTableWidget::edit));
          for(int i = 0; i < ui->tableWidgetAdminEmployee->rowCount(); i++)
          {
              ui->tableWidgetAdminEmployee->item(i,0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
              ui->tableWidgetAdminEmployee->item(i,7)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
              ui->tableWidgetAdminEmployee->item(i,8)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
              ui->tableWidgetAdminEmployee->item(i,9)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled );
          }
      
      J Offline
      J Offline
      JonB
      wrote on 10 Dec 2022, 17:02 last edited by
      #2

      @Kuji
      The only thing which strikes me is void QAbstractItemView::edit(const QModelIndex &index):

      To provide consistent navigation behavior, call setCurrentIndex() before this function with the same model index.

      Not saying that is your issue, but does it make any difference if you do this in the slot? Change to a lambda for the slot which calls setCurrentIndex() before edit(), this will also allow you to see whether the problem lies in the connection of clicked() signal to slot or in the call to edit() from the slot.

      K 1 Reply Last reply 10 Dec 2022, 17:45
      0
      • J JonB
        10 Dec 2022, 17:02

        @Kuji
        The only thing which strikes me is void QAbstractItemView::edit(const QModelIndex &index):

        To provide consistent navigation behavior, call setCurrentIndex() before this function with the same model index.

        Not saying that is your issue, but does it make any difference if you do this in the slot? Change to a lambda for the slot which calls setCurrentIndex() before edit(), this will also allow you to see whether the problem lies in the connection of clicked() signal to slot or in the call to edit() from the slot.

        K Offline
        K Offline
        Kuji
        wrote on 10 Dec 2022, 17:45 last edited by
        #3

        @JonB Sorry i don't understand can u explain about where i should do this: Change to a lambda for the slot which calls setCurrentIndex() before edit()?

        J 1 Reply Last reply 10 Dec 2022, 18:21
        0
        • K Kuji
          10 Dec 2022, 17:45

          @JonB Sorry i don't understand can u explain about where i should do this: Change to a lambda for the slot which calls setCurrentIndex() before edit()?

          J Offline
          J Offline
          JonB
          wrote on 10 Dec 2022, 18:21 last edited by
          #4

          @Kuji

              QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                               ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                  qDebug() << "In slot";
                  ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                  qDebug() << "Calling edit()";
                  ui->tableWidgetAdminEmployee->edit(index);
                  qDebug() << "Called edit()";
                  });
          

          Something like that (untested). I don't suppose doing setCurrentIndex(index) will solve your problem, but it's worth a try.

          K 1 Reply Last reply 10 Dec 2022, 18:29
          0
          • J JonB
            10 Dec 2022, 18:21

            @Kuji

                QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                                 ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                    qDebug() << "In slot";
                    ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                    qDebug() << "Calling edit()";
                    ui->tableWidgetAdminEmployee->edit(index);
                    qDebug() << "Called edit()";
                    });
            

            Something like that (untested). I don't suppose doing setCurrentIndex(index) will solve your problem, but it's worth a try.

            K Offline
            K Offline
            Kuji
            wrote on 10 Dec 2022, 18:29 last edited by
            #5

            @JonB Added, when i choose cell which is non-editable console writes:
            In slot
            Calling edit()
            edit: editing failed
            Called edit()

            When i choose editable cells console writes:
            In slot
            Calling edit()
            Called edit()

            C J 2 Replies Last reply 10 Dec 2022, 18:34
            0
            • K Kuji
              10 Dec 2022, 18:29

              @JonB Added, when i choose cell which is non-editable console writes:
              In slot
              Calling edit()
              edit: editing failed
              Called edit()

              When i choose editable cells console writes:
              In slot
              Calling edit()
              Called edit()

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 10 Dec 2022, 18:34 last edited by
              #6

              @Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:

              when i choose cell which is non-editable console writes:

              What do you expect otherwise? I mean - it just tells you that a non-editable item can not be edited. So either life with it or don't call edit() on such an item.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • K Kuji
                10 Dec 2022, 18:29

                @JonB Added, when i choose cell which is non-editable console writes:
                In slot
                Calling edit()
                edit: editing failed
                Called edit()

                When i choose editable cells console writes:
                In slot
                Calling edit()
                Called edit()

                J Offline
                J Offline
                JonB
                wrote on 10 Dec 2022, 18:40 last edited by JonB 12 Oct 2022, 18:42
                #7

                @Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:

                @JonB Added, when i choose cell which is non-editable console writes:

                I did not realize you were doing this on the non-editable cells! So it is behaving well/as it should, what is the issue? Earlier you wrote:

                but when i click on enabled cell in console appears line "edit: editing failed"

                Now you show that works fine on editable cells.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kuji
                  wrote on 10 Dec 2022, 18:48 last edited by
                  #8

                  How can i get rid of lines in console:edit: editing failed? By don't calling edit() for these cells then how exclude these cells?

                  J 1 Reply Last reply 10 Dec 2022, 18:52
                  0
                  • K Kuji
                    10 Dec 2022, 18:48

                    How can i get rid of lines in console:edit: editing failed? By don't calling edit() for these cells then how exclude these cells?

                    J Offline
                    J Offline
                    JonB
                    wrote on 10 Dec 2022, 18:52 last edited by
                    #9

                    @Kuji

                    if (ui->tableWidgetAdminEmployee->itemFromIndex(index)->flags() & Qt::ItemIsEnabled )
                        ui->tableWidgetAdminEmployee->edit(index);
                    

                    Something like that.

                    K 1 Reply Last reply 10 Dec 2022, 18:58
                    0
                    • J JonB
                      10 Dec 2022, 18:52

                      @Kuji

                      if (ui->tableWidgetAdminEmployee->itemFromIndex(index)->flags() & Qt::ItemIsEnabled )
                          ui->tableWidgetAdminEmployee->edit(index);
                      

                      Something like that.

                      K Offline
                      K Offline
                      Kuji
                      wrote on 10 Dec 2022, 18:58 last edited by Kuji 12 Oct 2022, 18:59
                      #10

                      @JonB Can't write instead of this code line because of en error, or i should write this in another place?

                      ui->tableWidgetAdminEmployee->edit(index);
                      
                      C 1 Reply Last reply 10 Dec 2022, 19:07
                      0
                      • K Kuji
                        10 Dec 2022, 18:58

                        @JonB Can't write instead of this code line because of en error, or i should write this in another place?

                        ui->tableWidgetAdminEmployee->edit(index);
                        
                        C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 10 Dec 2022, 19:07 last edited by
                        #11

                        @Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:

                        Can't write instead of this code line because of en error,

                        Then fixing the error would be a good start.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        K 1 Reply Last reply 10 Dec 2022, 19:35
                        0
                        • C Christian Ehrlicher
                          10 Dec 2022, 19:07

                          @Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:

                          Can't write instead of this code line because of en error,

                          Then fixing the error would be a good start.

                          K Offline
                          K Offline
                          Kuji
                          wrote on 10 Dec 2022, 19:35 last edited by
                          #12

                          @Christian-Ehrlicher i mean this error

                          error: 'itemFromIndex' is a protected member of 'QTableWidget'
                          

                          here

                              QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                                                   ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                                      ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                                      if (ui->tableWidgetAdminEmployee->itemFromIndex(index)->flags() & Qt::ItemIsEnabled )
                                          ui->tableWidgetAdminEmployee->edit(index);
                                      });
                          
                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mpergand
                            wrote on 10 Dec 2022, 20:20 last edited by
                            #13

                            try:
                            ui->tableWidgetAdminEmployee->item(index.row(), index.column())

                            K 1 Reply Last reply 10 Dec 2022, 20:43
                            1
                            • M mpergand
                              10 Dec 2022, 20:20

                              try:
                              ui->tableWidgetAdminEmployee->item(index.row(), index.column())

                              K Offline
                              K Offline
                              Kuji
                              wrote on 10 Dec 2022, 20:43 last edited by Kuji 12 Oct 2022, 21:41
                              #14

                              @mpergand This worked

                              QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                                        ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                              ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                              if(index.column()!=0&&index.column()!=7&&index.column()!=8&&index.column()!=9)
                              if (ui->tableWidgetAdminEmployee->item(index.row(), index.column())->flags() & Qt::ItemIsEnabled )
                              ui->tableWidgetAdminEmployee->edit(index);
                              });
                              

                              But now after making some columns editable i can't make them non-editable

                              M 1 Reply Last reply 10 Dec 2022, 21:03
                              0
                              • K Kuji
                                10 Dec 2022, 20:43

                                @mpergand This worked

                                QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                                          ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                                ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                                if(index.column()!=0&&index.column()!=7&&index.column()!=8&&index.column()!=9)
                                if (ui->tableWidgetAdminEmployee->item(index.row(), index.column())->flags() & Qt::ItemIsEnabled )
                                ui->tableWidgetAdminEmployee->edit(index);
                                });
                                

                                But now after making some columns editable i can't make them non-editable

                                M Offline
                                M Offline
                                mpergand
                                wrote on 10 Dec 2022, 21:03 last edited by
                                #15

                                @Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:

                                But now after making some columns editable i can't make them non-editable

                                Change the flag of the item accordingly.

                                K 1 Reply Last reply 10 Dec 2022, 21:46
                                0
                                • M mpergand
                                  10 Dec 2022, 21:03

                                  @Kuji said in How to set QTableWidget editable with a few non-editable columns, and editing by one click on cell?:

                                  But now after making some columns editable i can't make them non-editable

                                  Change the flag of the item accordingly.

                                  K Offline
                                  K Offline
                                  Kuji
                                  wrote on 10 Dec 2022, 21:46 last edited by Kuji 12 Oct 2022, 22:10
                                  #16

                                  @mpergand I tryed this, but after clicking on the cell it's editable and then it becomes unselectable

                                      QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                                                       ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                                          ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                                          ui->tableWidgetAdminEmployee->item(index.row(), index.column())->setFlags(Qt::ItemIsSelectable);
                                      });
                                  
                                  M 1 Reply Last reply 10 Dec 2022, 22:12
                                  0
                                  • K Kuji
                                    10 Dec 2022, 21:46

                                    @mpergand I tryed this, but after clicking on the cell it's editable and then it becomes unselectable

                                        QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,
                                                         ui->tableWidgetAdminEmployee, [=](const QModelIndex& index) {
                                            ui->tableWidgetAdminEmployee->setCurrentIndex(index);
                                            ui->tableWidgetAdminEmployee->item(index.row(), index.column())->setFlags(Qt::ItemIsSelectable);
                                        });
                                    
                                    M Offline
                                    M Offline
                                    mpergand
                                    wrote on 10 Dec 2022, 22:12 last edited by
                                    #17

                                    @Kuji
                                    Qt::ItemIsSelectable|Qt::ItemIsEnabled

                                    K 1 Reply Last reply 10 Dec 2022, 22:15
                                    0
                                    • M mpergand
                                      10 Dec 2022, 22:12

                                      @Kuji
                                      Qt::ItemIsSelectable|Qt::ItemIsEnabled

                                      K Offline
                                      K Offline
                                      Kuji
                                      wrote on 10 Dec 2022, 22:15 last edited by
                                      #18

                                      @mpergand cells are editable when i click on them and after that they are non-editable but write "edit: editing failed" in console

                                      M 1 Reply Last reply 10 Dec 2022, 22:22
                                      0
                                      • K Kuji
                                        10 Dec 2022, 22:15

                                        @mpergand cells are editable when i click on them and after that they are non-editable but write "edit: editing failed" in console

                                        M Offline
                                        M Offline
                                        mpergand
                                        wrote on 10 Dec 2022, 22:22 last edited by
                                        #19

                                        @Kuji
                                        To tell the truth, i don't understand what you're trying to do from the beginning.

                                        K 1 Reply Last reply 10 Dec 2022, 22:27
                                        0
                                        • M mpergand
                                          10 Dec 2022, 22:22

                                          @Kuji
                                          To tell the truth, i don't understand what you're trying to do from the beginning.

                                          K Offline
                                          K Offline
                                          Kuji
                                          wrote on 10 Dec 2022, 22:27 last edited by Kuji 12 Oct 2022, 22:35
                                          #20

                                          @mpergand I'm trying by pressing the button make editable some columns and make non-editable some columns in table, then after another button make all table non-editable.
                                          Also i can't disconnect, if i could i would just set table readonly

                                          1 Reply Last reply
                                          0

                                          1/24

                                          10 Dec 2022, 15:18

                                          • Login

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