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. Modify QtableWidget from a slots

Modify QtableWidget from a slots

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.1k Views 2 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.
  • Y Offline
    Y Offline
    Yoyotl
    wrote on last edited by
    #1

    Hello !
    I have a problem that I can't solve :
    I have a QTableWidget, and I wan't to modify randomly the text in the rows.
    So I have made this :

    mainwinow.cpp

        QTableWidget* table = new QTableWidget(this);
        table->setColumnCount(1);
        table->setEditTriggers(QAbstractItemView::NoEditTriggers); 
    
        for (int i=0; i<1; i++) 
        {
            nb = rand() % 500 + 10;
            table->insertRow(table->rowCount());
            table->setItem(0, 0, new QTableWidgetItem(QString::number(nb)));
        }
    

    and I have a slot in mainwondow.cpp that I call when I press a Button

    void MainWindow::changeNbRandom()
    {
        int rowToChange = 0;
        rowToChange = rand() % 5;
        int newNb = 5;
        newNb = rand() % 500 + 10;
    
        table->item(rowToChange, 0)->setText(QString::number(newNb));
    

    But when I press the Button, the application crash.
    The debug say that the error is on the last line of the slots.
    But if I put the line in dirrectly in mainwindow it works...

    Please, help me, I don't understand

    JonBJ 1 Reply Last reply
    0
    • Y Yoyotl

      Hello !
      I have a problem that I can't solve :
      I have a QTableWidget, and I wan't to modify randomly the text in the rows.
      So I have made this :

      mainwinow.cpp

          QTableWidget* table = new QTableWidget(this);
          table->setColumnCount(1);
          table->setEditTriggers(QAbstractItemView::NoEditTriggers); 
      
          for (int i=0; i<1; i++) 
          {
              nb = rand() % 500 + 10;
              table->insertRow(table->rowCount());
              table->setItem(0, 0, new QTableWidgetItem(QString::number(nb)));
          }
      

      and I have a slot in mainwondow.cpp that I call when I press a Button

      void MainWindow::changeNbRandom()
      {
          int rowToChange = 0;
          rowToChange = rand() % 5;
          int newNb = 5;
          newNb = rand() % 500 + 10;
      
          table->item(rowToChange, 0)->setText(QString::number(newNb));
      

      But when I press the Button, the application crash.
      The debug say that the error is on the last line of the slots.
      But if I put the line in dirrectly in mainwindow it works...

      Please, help me, I don't understand

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

      @Yoyotl
      Your for loop created/inserted just 1 row. Your table->item(rowToChange, 0) has 0 <= rowToChange < 5. If rowToChange > 0 that's not good.

      Also, your table->insertRow(table->rowCount()) inserts row at end, but your table->setItem(0, 0, new QTableWidgetItem(QString::number(nb))); is only ever setting row #0 to have a new QTableWidgetItem, you keep overwriting just that one item.

      1 Reply Last reply
      2
      • Y Offline
        Y Offline
        Yoyotl
        wrote on last edited by
        #3

        Alright, I change that, it was a mistake :

        for (int i=0; i<5; i++) /
            {
                nb = rand() % 500 + 10;
                table->insertRow(table->rowCount());
                table->setItem(i, 0, new QTableWidgetItem(QString::number(nb)));
            }
        

        So I have my 5 rows.
        I've tested differents sings with the debuggers, and it's seems like I just can't make any action on my QTableWidget table in my slot

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          Yoyotl
          wrote on last edited by
          #4

          It is possible ? Or how can I do otherwise ?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Yes it possible. and it normally just works

            ui->tableWidget_2->item(0,0)->setText("Test");

            changes the first row/col of the table widget

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              Yoyotl
              wrote on last edited by
              #6

              Yeah, I was thinking that too, but it doesn't work...
              Debugger sort that :
              ab27d849-3198-48fb-9695-e19075a22293-image.png

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mrjj said in Modify QtableWidget from a slots:

                ui->tableWidget_2->item(0,0)

                I would guess this returns a nullptr since there is no QTableWidgetItem set yet - you have to set one before accessing it.

                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
                4
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  You must assign items to the row/cols or else NULL is returned.
                  Try with

                  auto item = ui->tableWidget_2->item(row, 0);
                  if (item)
                      item->setText("Test");
                  else
                      qDebug() << "item is null";
                  
                  1 Reply Last reply
                  2
                  • Y Offline
                    Y Offline
                    Yoyotl
                    wrote on last edited by
                    #9

                    Itwork's if I put that in mainwindow, but if I put it in my slot like I want to do, the program crash again...

                    JonBJ 1 Reply Last reply
                    0
                    • Y Yoyotl

                      Itwork's if I put that in mainwindow, but if I put it in my slot like I want to do, the program crash again...

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

                      @Yoyotl
                      In your slot, put in debugging code to see how many rows/columns/what items your table has, before you try to dereference with table->item(rowToChange, 0). You are using the same table instance as you create with

                      QTableWidget* table = new QTableWidget(this);
                      

                      aren't you, because that looks like a local variable.... ? If you have a QMainWindow member variable named table, then statement QTableWidget* table = new QTableWidget(this); is not setting the member one...

                      mrjjM 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @Yoyotl
                        In your slot, put in debugging code to see how many rows/columns/what items your table has, before you try to dereference with table->item(rowToChange, 0). You are using the same table instance as you create with

                        QTableWidget* table = new QTableWidget(this);
                        

                        aren't you, because that looks like a local variable.... ? If you have a QMainWindow member variable named table, then statement QTableWidget* table = new QTableWidget(this); is not setting the member one...

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @JonB
                        Ahhh good spotted. The dangling pointer classic :)

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          Yoyotl
                          wrote on last edited by
                          #12

                          Thanks for your answers.
                          First, I create table in my mainwindow.cpp, and I declare it in mainwindow.h in public.
                          My slot is in the file mainwindow.cpp
                          And I can't even test how many rows/columns/item I have in it in my slot, because program crash on a segmentation fault every time that I try to access to table on my slot
                          Do I need to declare table in antoher place/file ?

                          mrjjM 1 Reply Last reply
                          0
                          • Y Yoyotl

                            Thanks for your answers.
                            First, I create table in my mainwindow.cpp, and I declare it in mainwindow.h in public.
                            My slot is in the file mainwindow.cpp
                            And I can't even test how many rows/columns/item I have in it in my slot, because program crash on a segmentation fault every time that I try to access to table on my slot
                            Do I need to declare table in antoher place/file ?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @Yoyotl

                            QTableWidget* table = new QTableWidget(this); // this would be wrong as its a local variable

                            table = new QTableWidget(this); // this would allocate the one you have in the class. (in the .h )

                            so make sure to remove the first one so there is ONLY the one in the class that you access in the slot

                            Im 99% sure that is why you crash.

                            1 Reply Last reply
                            4
                            • Y Offline
                              Y Offline
                              Yoyotl
                              wrote on last edited by
                              #14

                              IT WAS THAT !!
                              THANK YOU SO MUCH @mrjj !

                              So yes it was a problem in my declaration of table
                              That fix all my problems, thank you all !

                              mrjjM 1 Reply Last reply
                              0
                              • Y Yoyotl

                                IT WAS THAT !!
                                THANK YOU SO MUCH @mrjj !

                                So yes it was a problem in my declaration of table
                                That fix all my problems, thank you all !

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Yoyotl
                                Actually it was @JonB that spotted it but
                                super its fixed :)

                                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