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. How can I write data into table grids?
Forum Updated to NodeBB v4.3 + New Features

How can I write data into table grids?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.9k Views 1 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.
  • I Offline
    I Offline
    itsmemax
    wrote on last edited by
    #1

    Hi,

    I've got really no plan how to write text into grids of my tableWidget. I've tried it in many different ways...

    @ui->tableWidget->setText()
    ui->tableWidget->addText()
    ui->tableWidget->gridText
    ui->tableWidget->rowText
    ui->tableWidget->columnText
    ui->tableWidget->...@

    Nothing.. What's the correct function to add text to row X, column X of my tablewidget?

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      Assuming your 'tableWidget' is a "QTableWidget":http://qt-project.org/doc/qt-4.8/qtablewidget.html then you need to construct a "QTableWidgetItem":http://qt-project.org/doc/qt-4.8/qtablewidgetitem.html for each cell and then call "setItem":http://qt-project.org/doc/qt-4.8/qtablewidget.html#setItem as clearly described in the documentation "here":http://qt-project.org/doc/qt-4.8/qtablewidget.html#details. A brief snippet would be something like:

      @
      ...
      for(int r=0; r<num_of_rows; r++){
      for(int c=0; c<num_of_columns; c++){
      QTableWidgetItem *newItem = new QTableWidgetItem(QString("%1,%2").arg(r).arg(c));
      ui->tableWidget->setItem(r, c, newItem);
      }
      }
      ...
      @

      Untested, but you get the general picture. Hope this helps ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • I Offline
        I Offline
        itsmemax
        wrote on last edited by
        #3

        Yup, it helped!
        @QTableWidgetItem *item1 = new QTableWidgetItem(QString("Test"));
        ui->tableWidget->setItem(0, 1, item1);@
        Works like a charm, thx!

        1 Reply Last reply
        0
        • I Offline
          I Offline
          itsmemax
          wrote on last edited by
          #4

          I'm trying to put this thing in a loop, but I get an error. Here's the code:
          @ QTableWidgetItem *item[64];

          for (int r = 0; r < 64; ++r) {
              item[64] = new QTableWidgetItem(QStringList(sPinNames));
              ui->tableWidget->setItem(r, 1, item[64]);
          }@
          

          And the error:
          @no matching function for call to 'QTableWidgetItem::QTableWidgetItem(QStringList)'@

          Help?

          1 Reply Last reply
          0
          • I Offline
            I Offline
            itsmemax
            wrote on last edited by
            #5

            Made it already:
            @//Pinnames
            QStringList sPinNames;
            sPinNames << "I1/H1"<<"I2/H2"<<"I3/H3"<<"I4/H4"<<"I5/H5"<<"I6/H6"<<"I7/H7"<<"I8/H8"
            << "J1/G1"<<"J2/G2"<<"J3/G3"<<"J4/G4"<<"J5/G5"<<"J6/G6"<<"J7/G7"<<"J8/G8"
            << "K1/F1"<<"K2/F2"<<"K3/F3"<<"K4/F4"<<"K5/F5"<<"K6/F6"<<"K7/F7"<<"K8/F8"
            << "L1/E1"<<"L2/E2"<<"L3/E3"<<"L4/E4"<<"L5/E5"<<"L6/E6"<<"L7/E7"<<"L8/E8"
            << "M1/D1"<<"M2/D2"<<"M3/D3"<<"M4/D4"<<"M5/D5"<<"M6/D6"<<"M7/D7"<<"M8/D8"
            << "N1/C1"<<"N2/C2"<<"N3/C3"<<"N4/C4"<<"N5/C5"<<"N6/C6"<<"N7/C7"<<"N8/C8"
            << "O1/B1"<<"O2/B2"<<"O3/B3"<<"O4/B4"<<"O5/B5"<<"O6/B6"<<"O7/B7"<<"O8/B8"
            << "P1/A1"<<"P2/A2"<<"P3/A3"<<"P4/A4"<<"P5/A5"<<"P6/A6"<<"P7/A7"<<"P8/A8";

            int r = 0;
            QTableWidgetItem *item[64];
            
            for (r = 0; r < 64; ++r) {
                item[r] = new QTableWidgetItem(QString(sPinNames[r]));
                ui->tableWidget->setItem(r, 1, item[r]);
            }@
            

            Thx anyways

            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