Important: Please read the Qt Code of Conduct -

How can I write data into table grids?

  • Hi,

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


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

  • Assuming your 'tableWidget' is a "QTableWidget": then you need to construct a "QTableWidgetItem": for each cell and then call "setItem": as clearly described in the documentation "here": 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)

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

  • 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)'@


  • Made it already:
    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

Log in to reply