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. Updating QTablWidget correctly
QtWS25 Last Chance

Updating QTablWidget correctly

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetqwidget
9 Posts 4 Posters 1.5k 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.
  • T Offline
    T Offline
    TUStudi
    wrote on 16 Nov 2020, 20:59 last edited by
    #1

    Hi,

    I am using a QTableWidget with 2 columns and 3 rows in my GUI. This table is updated approximately 5 times per second. For updating the QTableWidget I have the following function, which takes a QMap as an argument which in turn contains the data to be printed in the QTableWidget. It is called in a slot:

    void Ui::updateTable(QMap<QString, float> data)
    {
        ui->table->setItem(0,0, new QTableWidgetItem(QString::number(data["a"])));
        ui->table->setItem(1,0, new QTableWidgetItem(QString::number(data["b"])));
        ui->table->setItem(2,0, new QTableWidgetItem(QString::number(data["c"])));
        ui->table->setItem(0,1, new QTableWidgetItem(QString::number(data["d"])));
        ui->table->setItem(1,1, new QTableWidgetItem(QString::number(data["e"])));
        ui->table->setItem(2,1, new QTableWidgetItem(QString::number(data["f"])));
    }
    

    Is that correct? I have a strange feeling, especially since this "new QTableWidgetItem" is called every time - so there are no memory problems (leak), right?

    Or would this be the correct way:

    void Ui::updateTable(QMap<QString, float> data)
    {
        ui->table->item(0,0)->setText(QString::number(data["a"]));
        ui->table->item(1,0)->setText(QString::number(data["b"]));
        ui->table->item(2,0)->setText(QString::number(data["c"]));
        ui->table->item(0,1)->setText(QString::number(data["d"]));
        ui->table->item(1,1)->setText(QString::number(data["e"]));
        ui->table->item(2,1)->setText(QString::number(data["f"]));
    }
    

    Thank you!

    J 1 Reply Last reply 16 Nov 2020, 23:19
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Nov 2020, 21:57 last edited by
      #2

      Hi,

      The second solution is cleaner. You are needlessly creating and destroying objects with the first approach.

      The setItem documentation states that the widget takes ownership of the item so it should dispose of it properly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 17 Nov 2020, 10:31
      2
      • T TUStudi
        16 Nov 2020, 20:59

        Hi,

        I am using a QTableWidget with 2 columns and 3 rows in my GUI. This table is updated approximately 5 times per second. For updating the QTableWidget I have the following function, which takes a QMap as an argument which in turn contains the data to be printed in the QTableWidget. It is called in a slot:

        void Ui::updateTable(QMap<QString, float> data)
        {
            ui->table->setItem(0,0, new QTableWidgetItem(QString::number(data["a"])));
            ui->table->setItem(1,0, new QTableWidgetItem(QString::number(data["b"])));
            ui->table->setItem(2,0, new QTableWidgetItem(QString::number(data["c"])));
            ui->table->setItem(0,1, new QTableWidgetItem(QString::number(data["d"])));
            ui->table->setItem(1,1, new QTableWidgetItem(QString::number(data["e"])));
            ui->table->setItem(2,1, new QTableWidgetItem(QString::number(data["f"])));
        }
        

        Is that correct? I have a strange feeling, especially since this "new QTableWidgetItem" is called every time - so there are no memory problems (leak), right?

        Or would this be the correct way:

        void Ui::updateTable(QMap<QString, float> data)
        {
            ui->table->item(0,0)->setText(QString::number(data["a"]));
            ui->table->item(1,0)->setText(QString::number(data["b"]));
            ui->table->item(2,0)->setText(QString::number(data["c"]));
            ui->table->item(0,1)->setText(QString::number(data["d"]));
            ui->table->item(1,1)->setText(QString::number(data["e"]));
            ui->table->item(2,1)->setText(QString::number(data["f"]));
        }
        

        Thank you!

        J Offline
        J Offline
        JonB
        wrote on 16 Nov 2020, 23:19 last edited by JonB
        #3

        @TUStudi
        Separate from that question, if this is being called often

        void Ui::updateTable(QMap<QString, float> data)
        

        I would have thought

        void Ui::updateTable(const QMap<QString, float> &data)
        

        would be better?

        1 Reply Last reply
        2
        • S SGaist
          16 Nov 2020, 21:57

          Hi,

          The second solution is cleaner. You are needlessly creating and destroying objects with the first approach.

          The setItem documentation states that the widget takes ownership of the item so it should dispose of it properly.

          T Offline
          T Offline
          TUStudi
          wrote on 17 Nov 2020, 10:31 last edited by
          #4

          @SGaist said in Updating QTablWidget correctly:

          Hi,

          The second solution is cleaner. You are needlessly creating and destroying objects with the first approach.

          The setItem documentation states that the widget takes ownership of the item so it should dispose of it properly.

          This makes sense, thank you!

          @JonB said in Updating QTablWidget correctly:

          I would have thought

          void Ui::updateTable(const QMap<QString, float> &data)
          

          would be better?

          Why? Would that make a big difference?

          M 1 Reply Last reply 17 Nov 2020, 10:37
          0
          • T TUStudi
            17 Nov 2020, 10:31

            @SGaist said in Updating QTablWidget correctly:

            Hi,

            The second solution is cleaner. You are needlessly creating and destroying objects with the first approach.

            The setItem documentation states that the widget takes ownership of the item so it should dispose of it properly.

            This makes sense, thank you!

            @JonB said in Updating QTablWidget correctly:

            I would have thought

            void Ui::updateTable(const QMap<QString, float> &data)
            

            would be better?

            Why? Would that make a big difference?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 17 Nov 2020, 10:37 last edited by mrjj
            #5

            @TUStudi
            Hi
            The & means by reference so

            void Ui::updateTable(QMap<QString, float> data)
            

            This will make a copy of the original container you use with it.
            If it has many items, that can be expensive.
            But QMap uses sharing
            https://doc.qt.io/qt-5/implicit-sharing.html
            So its cheap to copy.
            But for other containers, it can make a huge difference.
            Also if it contains complex objects.

            void Ui::updateTable(const QMap<QString, float> &data)
            

            This does not make a copy but is a reference (like pointer) to the original
            map. So very fast.

            T 1 Reply Last reply 17 Nov 2020, 12:03
            2
            • M mrjj
              17 Nov 2020, 10:37

              @TUStudi
              Hi
              The & means by reference so

              void Ui::updateTable(QMap<QString, float> data)
              

              This will make a copy of the original container you use with it.
              If it has many items, that can be expensive.
              But QMap uses sharing
              https://doc.qt.io/qt-5/implicit-sharing.html
              So its cheap to copy.
              But for other containers, it can make a huge difference.
              Also if it contains complex objects.

              void Ui::updateTable(const QMap<QString, float> &data)
              

              This does not make a copy but is a reference (like pointer) to the original
              map. So very fast.

              T Offline
              T Offline
              TUStudi
              wrote on 17 Nov 2020, 12:03 last edited by TUStudi
              #6

              @mrjj Thank you very much, I completely ignored that.

              One more thing:
              In my previous version of updating the table
              ui->table->setItem(0,0, new QTableWidgetItem(QString::number(data["a"])));,
              I used a reset button, which calls ui->table->clearContent.
              With the new code
              ui->table->item(0,0)->setText(QString::number(data["a"]));
              , when I click the reset button and then start the updateData-Slot egain, there is a access-violation exception pointing to my first line

               ui->table->item(0,0)->setText(QString::number(data["a"]));
              

              When I comment out ui->table->clearContent, the exception does not occur. What's the issue here?

              J 1 Reply Last reply 17 Nov 2020, 12:11
              0
              • T TUStudi
                17 Nov 2020, 12:03

                @mrjj Thank you very much, I completely ignored that.

                One more thing:
                In my previous version of updating the table
                ui->table->setItem(0,0, new QTableWidgetItem(QString::number(data["a"])));,
                I used a reset button, which calls ui->table->clearContent.
                With the new code
                ui->table->item(0,0)->setText(QString::number(data["a"]));
                , when I click the reset button and then start the updateData-Slot egain, there is a access-violation exception pointing to my first line

                 ui->table->item(0,0)->setText(QString::number(data["a"]));
                

                When I comment out ui->table->clearContent, the exception does not occur. What's the issue here?

                J Offline
                J Offline
                JonB
                wrote on 17 Nov 2020, 12:11 last edited by JonB
                #7

                @TUStudi
                When you create a new QTableWidget, or call clearContents(), there are no items in the table! So ui->table->item(0,0) is nullptr.

                T 1 Reply Last reply 17 Nov 2020, 12:15
                2
                • J JonB
                  17 Nov 2020, 12:11

                  @TUStudi
                  When you create a new QTableWidget, or call clearContents(), there are no items in the table! So ui->table->item(0,0) is nullptr.

                  T Offline
                  T Offline
                  TUStudi
                  wrote on 17 Nov 2020, 12:15 last edited by
                  #8

                  @JonB said in Updating QTablWidget correctly:

                  @TUStudi
                  When you create a new QTableWidget, or call clearContents(), there are no items in the table! So ui->table->item(0,0) is nullptr.

                  Ahhh, okay. So actually I do not have to call clearContents, so I will just remove this line of code!
                  Thanks again =D

                  J 1 Reply Last reply 17 Nov 2020, 12:16
                  0
                  • T TUStudi
                    17 Nov 2020, 12:15

                    @JonB said in Updating QTablWidget correctly:

                    @TUStudi
                    When you create a new QTableWidget, or call clearContents(), there are no items in the table! So ui->table->item(0,0) is nullptr.

                    Ahhh, okay. So actually I do not have to call clearContents, so I will just remove this line of code!
                    Thanks again =D

                    J Offline
                    J Offline
                    JonB
                    wrote on 17 Nov 2020, 12:16 last edited by
                    #9

                    @TUStudi
                    Well, it's not necessarily that: you can create an item via void QTableWidget::setItem(int row, int column, QTableWidgetItem *item) instead.

                    1 Reply Last reply
                    1

                    5/9

                    17 Nov 2020, 10:37

                    • Login

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