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. Remove row button in QTableview

Remove row button in QTableview

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 5.0k 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.
  • guidupasG Offline
    guidupasG Offline
    guidupas
    wrote on last edited by SGaist
    #1

    Hello all!

    I am having some problems with a QPushButton used inside a QTableview. This button needs to remove the row but when I remove one row its changes the index and the button starts to remove the wrong row.

    How can I pass the right parameter to void "MainWindow::on_excluir_clicked(int linha)" ?

    Thanks a lot.

    Code below:
    @
    void MainWindow::adicionarLinha(QStandardItemModel *modelo, QTableView *tabela)
    {
    spinBoxDelegar = new spinBoxDelegate(this);

    QPushButton *excluirButton = new QPushButton(*iconeExcluirButton, "", 0);
    excluirButton = new QPushButton(*iconeExcluirButton, "", 0);
    excluirButton->setStyleSheet("background-color: rgb(255, 255, 255); color: rgb(255, 255, 255); border: 0");
    QSignalMapper *mapearSinalBotaoExcluir = new QSignalMapper(this);
    
    modelo->insertRow(modelo->rowCount());
    modelo->setData(modelo->index(modelo->rowCount()-1,1), 1);
    tabela->setItemDelegateForColumn(1, spinBoxDelegar);
    tabela->setIndexWidget(modelo->index(modelo->rowCount()-1,2), excluirButton);
    tabela->edit(modelo->index(modelo->rowCount()-1,0));
    
    mapearSinalBotaoExcluir->setMapping(excluirButton, modelo->rowCount()-1);
    connect(excluirButton, SIGNAL(clicked()), mapearSinalBotaoExcluir, SLOT(map()));
    connect(mapearSinalBotaoExcluir, SIGNAL(mapped(int)), this, SLOT(on_excluir_clicked(int)));
    

    }

    void MainWindow::on_excluir_clicked(int linha)
    {
    this->modelo1->removeRow(linha);
    }
    @

    Att.
    Guilherme Cortada Dupas

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Every time you remove a row, the other will have their index changed so your buttons will be completely off.

      You should rather scan your table until you find the line with the button you just clicked. Look for sender() and indexWidget().

      Hope it helps

      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
      • guidupasG Offline
        guidupasG Offline
        guidupas
        wrote on last edited by
        #3

        Could you help me with some code?

        Thanks

        Att.
        Guilherme Cortada Dupas

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean the for loop that goes from row 0 to count() -1 ?

          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
          • guidupasG Offline
            guidupasG Offline
            guidupas
            wrote on last edited by
            #5

            No. I would like to know how to compare the two objects to know if they are the same.

            Thanks you for the answers.

            Att.
            Guilherme Cortada Dupas

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can simply compare the pointer returned by sender() to the pointer given by indexWidget()

              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
              • guidupasG Offline
                guidupasG Offline
                guidupas
                wrote on last edited by
                #7

                SGaist, thank you very much. It worked. Very big help.

                Att.
                Guilherme Cortada Dupas

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You're welcome !

                  If this solves your question, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                  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
                  • L Offline
                    L Offline
                    Legnain
                    wrote on last edited by
                    #9

                    Hello
                    Could you share the solution. I have the same issue, but I am using pyqt.

                    thanks

                    1 Reply Last reply
                    0
                    • guidupasG Offline
                      guidupasG Offline
                      guidupas
                      wrote on last edited by
                      #10

                      Hello @Legnain !

                      Here it goes:

                      void MainWindow::on_excluir_clicked1()
                      {
                          QPushButton  *clicado = qobject_cast<QPushButton *>(sender());
                          for(int i = 0; i < this->modelo1->rowCount(); i++)
                          {
                              if(clicado == this->ui->tableView->indexWidget(this->modelo1->index(i,5)))
                              {
                                  this->modelo1->removeRow(i);
                              }
                          }
                      }
                      

                      Att.
                      Guilherme Cortada Dupas

                      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