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.
  • 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 Offline
        Christian EhrlicherC Offline
        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 Online
              JonBJ Online
              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