Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Displaying my data to QTableWidget
Qt 6.12 LTS is out! See what's new in the release blog

Displaying my data to QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.1k 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.
  • VineelaV Offline
    VineelaV Offline
    Vineela
    wrote on last edited by
    #1

    I have created a table widget with headers RYOTNUMBER ,PLOTNUMBER and CUTTINGORDERCODE , well my code is

    sn "1"
    SEASONCODE "1920"
    RYOTNUMBER "INKA010601001004"
    VILLAGECODE "INKA010601001"
    OFFICERCODE "MD001 "
    DATE "2019-01-30"
    TIME "14:50"
    OFFICERNAME "Y T VODDAGALLAIA"
    PLOTNUMBER "123"
    CUTTINGORDERCODE "0051"
    GANGLEADERCODE "GNL0001"
    MODEOFTRANSPORT "Own"
    CROPTYPE "HSV"
    COB "2019-06-17"
    
    sn "2"
    SEASONCODE "1920"
    RYOTNUMBER "INKA010601001004"
    VILLAGECODE "INKA010601001"
    OFFICERCODE "MD001 "
    DATE "2019-01-30"
    TIME "17:30"
    OFFICERNAME "Y T VODDAGALLAIA"
    PLOTNUMBER "421"
    CUTTINGORDERCODE "009"
    GANGLEADERCODE "GNL0001"
    MODEOFTRANSPORT "Company"
    CROPTYPE "HSV"
    COB "2019-03-9"
    

    where it keeps on going so now my question is I need these RYOTNUMBER ,PLOTNUMBER and CUTTINGORDERCODE three values to be entered in my table. Any help?

    Thanks and regards.

    jsulmJ 1 Reply Last reply
    0
    • VineelaV Vineela

      I have created a table widget with headers RYOTNUMBER ,PLOTNUMBER and CUTTINGORDERCODE , well my code is

      sn "1"
      SEASONCODE "1920"
      RYOTNUMBER "INKA010601001004"
      VILLAGECODE "INKA010601001"
      OFFICERCODE "MD001 "
      DATE "2019-01-30"
      TIME "14:50"
      OFFICERNAME "Y T VODDAGALLAIA"
      PLOTNUMBER "123"
      CUTTINGORDERCODE "0051"
      GANGLEADERCODE "GNL0001"
      MODEOFTRANSPORT "Own"
      CROPTYPE "HSV"
      COB "2019-06-17"
      
      sn "2"
      SEASONCODE "1920"
      RYOTNUMBER "INKA010601001004"
      VILLAGECODE "INKA010601001"
      OFFICERCODE "MD001 "
      DATE "2019-01-30"
      TIME "17:30"
      OFFICERNAME "Y T VODDAGALLAIA"
      PLOTNUMBER "421"
      CUTTINGORDERCODE "009"
      GANGLEADERCODE "GNL0001"
      MODEOFTRANSPORT "Company"
      CROPTYPE "HSV"
      COB "2019-03-9"
      

      where it keeps on going so now my question is I need these RYOTNUMBER ,PLOTNUMBER and CUTTINGORDERCODE three values to be entered in my table. Any help?

      Thanks and regards.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Vineela It is shown in the documentation http://doc.qt.io/qt-5/qtablewidget.html

      QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
      (row+1)*(column+1)));
      tableWidget->setItem(row, column, newItem);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      VineelaV 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Vineela It is shown in the documentation http://doc.qt.io/qt-5/qtablewidget.html

        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
        (row+1)*(column+1)));
        tableWidget->setItem(row, column, newItem);
        
        VineelaV Offline
        VineelaV Offline
        Vineela
        wrote on last edited by Vineela
        #3

        @jsulm well check out this code i know its not correct plz correct me,

        QSqlQuery q;
            q.prepare("SELECT * FROM cuttorderTable WHERE flag = '1' ");
            q.exec();
            while (q.next())
            {
             qDebug() << "Hi thread2";
        
               QString sn = q.value(0).toString();
                qDebug() << "sn" << sn;
                 QString i1 = q.value(1).toString();
                  qDebug() << "SEASONCODE" << i1;
                   QString mn = q.value(2).toString();
                    qDebug() << "RYOTNUMBER" << mn;
                     QString j = q.value(3).toString();
                      qDebug() << "VILLAGECODE" << j;
                     QString k = q.value(4).toString();
                      qDebug() << "OFFICERCODE" << k;
                        QString l = q.value(5).toString();
                         qDebug() << "DATE" << l;
                         QString m = q.value(6).toString();
                          qDebug() << "TIME" << m;
                          QString n = q.value(7).toString();
                           qDebug() << "OFFICERNAME" << n;
                          QString o = q.value(8).toString();
                           qDebug() << "PLOTNUMBER" << o;
                           QString p = q.value(9).toString();
                            qDebug() << "CUTTINGORDERCODE" << p;
                            QString t = q.value(10).toString();
                             qDebug() << "GANGLEADERCODE" << t;
                             QString u = q.value(11).toString();
                              qDebug() << "TRANSPORTERCODE" << u;
                              QString v = q.value(12).toString();
                               qDebug() << "MODEOFTRANSPORT" << v;
                               QString w = q.value(13).toString();
                                qDebug() << "CROPTYPE" << w;
                                QString a = q.value(14).toString();
                                 qDebug() << "COB" << a;
        
                    
                                  QTableWidgetItem *mn1 = new QTableWidgetItem(tr("%1").arg(
                                   (0+1)*(0+1)));
                                  ui->tableWidget->setItem(0, 0, mn1);
        
                                  QTableWidgetItem *o1 = new QTableWidgetItem(tr("%1").arg(
                                   (0+1)*(1+1)));
                                  ui->tableWidget->setItem(0, 1, o1);
        
                                  QTableWidgetItem *a1 = new QTableWidgetItem(tr("%1").arg(
                                   (0+1)*(2+1)));
                                  ui->tableWidget->setItem(0, 2, a1);
        
        }
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          Slow down, you are overcomplicating massively.

          • Replace the QTableWidget with a QTableView
          • use the code below:
          QSqlTableModel* model = new QSqlTableModel(this);
          model->setTable(QStringLiteral("cuttorderTable"));
          model->setFilter(QStringLiteral("flag = '1'"));
          model->select();
          ui->tableView->setModel(model);
          

          P.S.

          qDebug() << "Hi thread2";

          The ui can only be changed by the ui thread and databases can be used only in the thread that created them so be very careful if you are using multiple threads

          "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

          VineelaV 2 Replies Last reply
          3
          • VRoninV VRonin

            Slow down, you are overcomplicating massively.

            • Replace the QTableWidget with a QTableView
            • use the code below:
            QSqlTableModel* model = new QSqlTableModel(this);
            model->setTable(QStringLiteral("cuttorderTable"));
            model->setFilter(QStringLiteral("flag = '1'"));
            model->select();
            ui->tableView->setModel(model);
            

            P.S.

            qDebug() << "Hi thread2";

            The ui can only be changed by the ui thread and databases can be used only in the thread that created them so be very careful if you are using multiple threads

            VineelaV Offline
            VineelaV Offline
            Vineela
            wrote on last edited by
            #5

            @VRonin thanks ,yes i've changed it to table view but the whole table is displaying so how can i select only those three columns ?

            1 Reply Last reply
            0
            • VRoninV VRonin

              Slow down, you are overcomplicating massively.

              • Replace the QTableWidget with a QTableView
              • use the code below:
              QSqlTableModel* model = new QSqlTableModel(this);
              model->setTable(QStringLiteral("cuttorderTable"));
              model->setFilter(QStringLiteral("flag = '1'"));
              model->select();
              ui->tableView->setModel(model);
              

              P.S.

              qDebug() << "Hi thread2";

              The ui can only be changed by the ui thread and databases can be used only in the thread that created them so be very careful if you are using multiple threads

              VineelaV Offline
              VineelaV Offline
              Vineela
              wrote on last edited by
              #6

              @VRonin well I've added this line

              ui->tableView->hideColumn(1);
              

              to hide the columns which i didn't need of. Thanks by the way for your response.

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

                If you don't need editing then you can use QSqlQueryModel and directly select only the columns you need in the select statement

                "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

                1 Reply Last reply
                4

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved