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. Show integer value in qtable widget
Forum Updated to NodeBB v4.3 + New Features

Show integer value in qtable widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 7.2k 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.
  • Q QTlearner90

    HI..,
    I am new to QT. i have to display an int value in qtable widget.I tried following code. but it doesnot showing the value in table widget.

    rgnt.R_id=1;
    ui->tableWidget_3->setItem(0,1,new QTableWidgetItem((rgnt.R_id)));

    here rgnt is a structure object.
    can anyone tell how can i show this value in tablewidget??

    Edit raven-worx: moved topic

    Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #2

    @QTlearner90

    ui->tableWidget_3->setItem(0, 1, new QTableWidgetItem(QString::number(rgnt.R_id) ) );
    

    https://www.google.ge/search?q=number+to+qstring&oq=number+to+qstring&aqs=chrome.0.0j69i60l3j0l2.4535j0j7&sourceid=chrome&ie=UTF-8

    Do what you want.

    Q 1 Reply Last reply
    3
    • Q QTlearner90

      HI..,
      I am new to QT. i have to display an int value in qtable widget.I tried following code. but it doesnot showing the value in table widget.

      rgnt.R_id=1;
      ui->tableWidget_3->setItem(0,1,new QTableWidgetItem((rgnt.R_id)));

      here rgnt is a structure object.
      can anyone tell how can i show this value in tablewidget??

      Edit raven-worx: moved topic

      Venkatesh VV Offline
      Venkatesh VV Offline
      Venkatesh V
      wrote on last edited by
      #3

      @QTlearner90

      Hi,
      QTableWidgetItem wont take integer as a input so try as @Taz742 has posted.

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #4

        Guys, there are other methods in a class beyond the constructor!

        auto widItem = new QTableWidgetItem;
        widItem->setData(Qt::EditRole,rgnt.R_id);
        ui->tableWidget_3->setItem(0,1,widItem);
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        Taz742T 1 Reply Last reply
        4
        • VRoninV VRonin

          Guys, there are other methods in a class beyond the constructor!

          auto widItem = new QTableWidgetItem;
          widItem->setData(Qt::EditRole,rgnt.R_id);
          ui->tableWidget_3->setItem(0,1,widItem);
          
          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by Taz742
          #5

          @VRonin
          The difference is big?
          Can you give specific answers?

          Do what you want.

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #6

            Huge!

            Just for a laugh, try filling a column with the numbers from 1 to 100 using your method and then sort the column.

            on top of that, QString::number and QString::toInt should never be used for things that are shown on the ui. Always use QLocale::toString and QLocale::toInt with the locale of the widget you are presenting the value to

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            artwawA 1 Reply Last reply
            4
            • VRoninV VRonin

              Huge!

              Just for a laugh, try filling a column with the numbers from 1 to 100 using your method and then sort the column.

              on top of that, QString::number and QString::toInt should never be used for things that are shown on the ui. Always use QLocale::toString and QLocale::toInt with the locale of the widget you are presenting the value to

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #7

              @VRonin said in Show integer value in qtable widget:

              QString::number and QString::toInt should never be used for things that are shown on the ui.

              I apologize for hijacking the thread but what if you have just plain integers and you are sure that values are locale independent?

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #8

                While we think arab numbers are universal, they are not. Chinese locales, for example, have special unicode chars for numbers (0 = 零, 1 = 一) . Even arabic has different chars for arab numbers

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                artwawA Taz742T 2 Replies Last reply
                4
                • VRoninV VRonin

                  While we think arab numbers are universal, they are not. Chinese locales, for example, have special unicode chars for numbers (0 = 零, 1 = 一) . Even arabic has different chars for arab numbers

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #9

                  @VRonin Thank you!

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0
                  • Taz742T Taz742

                    @QTlearner90

                    ui->tableWidget_3->setItem(0, 1, new QTableWidgetItem(QString::number(rgnt.R_id) ) );
                    

                    https://www.google.ge/search?q=number+to+qstring&oq=number+to+qstring&aqs=chrome.0.0j69i60l3j0l2.4535j0j7&sourceid=chrome&ie=UTF-8

                    Q Offline
                    Q Offline
                    QTlearner90
                    wrote on last edited by
                    #10

                    @Taz742 thank you.. it works..

                    Taz742T 1 Reply Last reply
                    0
                    • Q QTlearner90

                      @Taz742 thank you.. it works..

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #11

                      @QTlearner90
                      Yes it works. But review the comments and then make a decision.
                      I think it's better if you use another method what @VRonin said.

                      auto widItem = new QTableWidgetItem;
                      widItem->setData(Qt::EditRole,rgnt.R_id);
                      ui->tableWidget_3->setItem(0,1,widItem);
                      

                      Do what you want.

                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        While we think arab numbers are universal, they are not. Chinese locales, for example, have special unicode chars for numbers (0 = 零, 1 = 一) . Even arabic has different chars for arab numbers

                        Taz742T Offline
                        Taz742T Offline
                        Taz742
                        wrote on last edited by
                        #12

                        @VRonin
                        Why does not it take Qt::Checked or Qt::Unchecked?

                        Do what you want.

                        VRoninV 1 Reply Last reply
                        0
                        • Taz742T Taz742

                          @VRonin
                          Why does not it take Qt::Checked or Qt::Unchecked?

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #13

                          @Taz742 said in Show integer value in qtable widget:

                          @VRonin
                          Why does not it take Qt::Checked or Qt::Unchecked?

                          You need to adjust the flag too for that role to matter widItem->setFlags(widItem->flags() | Qt::ItemIsUserCheckable);

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          Taz742T 1 Reply Last reply
                          3
                          • VRoninV VRonin

                            @Taz742 said in Show integer value in qtable widget:

                            @VRonin
                            Why does not it take Qt::Checked or Qt::Unchecked?

                            You need to adjust the flag too for that role to matter widItem->setFlags(widItem->flags() | Qt::ItemIsUserCheckable);

                            Taz742T Offline
                            Taz742T Offline
                            Taz742
                            wrote on last edited by Taz742
                            #14

                            @VRonin Can not I give it my function?

                            QTableWidgetItem *dlgGroups::GetNewItem(const QVariant value){
                                auto c_item = new QTableWidgetItem;
                                     c_item->setData(Qt::UserRole, value);
                                return (QTableWidgetItem*) c_item;
                            }
                            
                            Contact *cont = globalall->GetConact(CUID);
                            
                            m_table->insertRow(0);
                            m_table->setItem(0, 0, GetNewItem(InsertQry.lastInsertId().toString()));
                            m_table->setItem(0, 1, GetNewItem(cont->UID));
                            m_table->setItem(0, 2, GetNewItem(Qt::Unchecked)); // ?
                            m_table->setItem(0, 3, GetNewItem(cont->Name + " " + cont->FamilyName));
                            m_table->setItem(0, 4, GetNewItem(cont->Mobile));
                            m_table->setItem(0, 5, GetNewItem(cont->Type ? cont->Identification : cont->PersonID));
                            

                            Do what you want.

                            VRoninV 1 Reply Last reply
                            0
                            • Taz742T Taz742

                              @VRonin Can not I give it my function?

                              QTableWidgetItem *dlgGroups::GetNewItem(const QVariant value){
                                  auto c_item = new QTableWidgetItem;
                                       c_item->setData(Qt::UserRole, value);
                                  return (QTableWidgetItem*) c_item;
                              }
                              
                              Contact *cont = globalall->GetConact(CUID);
                              
                              m_table->insertRow(0);
                              m_table->setItem(0, 0, GetNewItem(InsertQry.lastInsertId().toString()));
                              m_table->setItem(0, 1, GetNewItem(cont->UID));
                              m_table->setItem(0, 2, GetNewItem(Qt::Unchecked)); // ?
                              m_table->setItem(0, 3, GetNewItem(cont->Name + " " + cont->FamilyName));
                              m_table->setItem(0, 4, GetNewItem(cont->Mobile));
                              m_table->setItem(0, 5, GetNewItem(cont->Type ? cont->Identification : cont->PersonID));
                              
                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #15

                              @Taz742

                              1. you still did not set the flag
                              2. you are saving everything in Qt::UserRole which is never used by Qt's standard views
                              QTableWidgetItem *dlgGroups::GetNewItem(const QVariant& value, int role = Qt::EditRole){
                                  auto c_item = new QTableWidgetItem;
                                  c_item->setData(role , value);
                                  if(role==Qt::CheckStateRole)
                                      c_item->setFlags(c_item->flags() | Qt::ItemIsUserCheckable);
                                  return c_item;
                              }
                              

                              then you can use m_table->setItem(0, 2, GetNewItem(Qt::Unchecked,Qt::CheckStateRole));

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              Taz742T 1 Reply Last reply
                              2
                              • VRoninV VRonin

                                @Taz742

                                1. you still did not set the flag
                                2. you are saving everything in Qt::UserRole which is never used by Qt's standard views
                                QTableWidgetItem *dlgGroups::GetNewItem(const QVariant& value, int role = Qt::EditRole){
                                    auto c_item = new QTableWidgetItem;
                                    c_item->setData(role , value);
                                    if(role==Qt::CheckStateRole)
                                        c_item->setFlags(c_item->flags() | Qt::ItemIsUserCheckable);
                                    return c_item;
                                }
                                

                                then you can use m_table->setItem(0, 2, GetNewItem(Qt::Unchecked,Qt::CheckStateRole));

                                Taz742T Offline
                                Taz742T Offline
                                Taz742
                                wrote on last edited by
                                #16

                                @VRonin
                                Many Thank!!!

                                Do what you want.

                                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