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 5.0k 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 Kuji

    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 );
        }
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    0
    • JonBJ JonB

      @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 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()?

      JonBJ 1 Reply Last reply
      0
      • K Kuji

        @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()?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        0
        • JonBJ JonB

          @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 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()

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • K Kuji

            @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()

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

              @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()

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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 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?

                JonBJ 1 Reply Last reply
                0
                • K Kuji

                  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?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  0
                  • JonBJ JonB

                    @Kuji

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

                    Something like that.

                    K Offline
                    K Offline
                    Kuji
                    wrote on last edited by Kuji
                    #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);
                    
                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • K Kuji

                      @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);
                      
                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 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
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @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 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 last edited by
                          #13

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

                          K 1 Reply Last reply
                          1
                          • M mpergand

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

                            K Offline
                            K Offline
                            Kuji
                            wrote on last edited by Kuji
                            #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
                            0
                            • K Kuji

                              @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 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
                              0
                              • M mpergand

                                @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 last edited by Kuji
                                #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
                                0
                                • K Kuji

                                  @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 last edited by
                                  #17

                                  @Kuji
                                  Qt::ItemIsSelectable|Qt::ItemIsEnabled

                                  K 1 Reply Last reply
                                  0
                                  • M mpergand

                                    @Kuji
                                    Qt::ItemIsSelectable|Qt::ItemIsEnabled

                                    K Offline
                                    K Offline
                                    Kuji
                                    wrote on 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
                                    0
                                    • K Kuji

                                      @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 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
                                      0
                                      • M mpergand

                                        @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 last edited by Kuji
                                        #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
                                        • M Offline
                                          M Offline
                                          mpergand
                                          wrote on last edited by
                                          #21

                                          QObject::connect(ui->tableWidgetAdminEmployee, &QTableWidget::clicked,

                                          Here you click on a table cell not on a button.
                                          So what button are you talking about ?

                                          K 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