2

I'd like to paste all my data I stored in several QList variables into one central QTableWidget.

I have six QList<QString> variables with actually each length of them is 7. With the help of this routine, I'd like to write each element of my QList into a QTableWidgetItem. What is the easiest and efficient way to solve this?

 for (int ridx = 0; ridx < iRowCount; ridx++ )
 {
        tmptable = resultTable[ridx];
        for (int cidx = 0; cidx < iColumnCount; cidx++)
        {
          QTableWidgetItem* item = new QTableWidgetItem();

          item->setText(tmptable[ridx]);
          ui->tableWidget->setItem(ridx,cidx,item);    
        }    
 }
demonplus
  • 5,613
  • 12
  • 49
  • 68
jollepe
  • 71
  • 13
  • So what is wrong with that code? – hyde May 29 '16 at 07:38
  • Perhaps you would want a custom model, and use `QTableView`? Though it will be overall more complex, so above is fine if you are not running into performance problem or something. – hyde May 29 '16 at 07:41
  • I don't think I'm running into performance problems. It's just a standard QTableWidget shows all entries of a database. Each column of the database is stored in a QList variable. And I'd like to show all columns into one central QTableWidget. This code above doesn't work propertly, because I got forced close after starting the application (QList out of range). Is there an easy way to merge all QList into one QTableWidget? – jollepe May 29 '16 at 08:06

1 Answers1

0

I got it running. This is my final code to add all elements of each QList into the central QTableWidget. It's not that elegant solution. Might be there another (better) solution?

for (int cidx = 0; cidx < iColumnCount; cidx++)
{
    if (cidx==0)
    {
        // Column LoginName

        for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
        {
            QTableWidgetItem* item = new QTableWidgetItem();

            item->setText(ListLoginName[ridx]);
            ui->tableWidget->setItem(ridx,cidx,item);

        }

    }
    if (cidx==1)
    {

        //Column Lastname
        for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
        {
            QTableWidgetItem* item = new QTableWidgetItem();

            item->setText(ListLastname[ridx]);
            ui->tableWidget->setItem(ridx,cidx,item);

        }

    }
    if (cidx==2)
    {
        // Column Firstname
        for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
        {
            QTableWidgetItem* item = new QTableWidgetItem();

            item->setText(ListFirstname[ridx]);
            ui->tableWidget->setItem(ridx,cidx,item);

        }

    }
    if (cidx==3)
    {
        // COlumn Position
        for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
        {
            QTableWidgetItem* item = new QTableWidgetItem();

            item->setText(ListPosition[ridx]);
            ui->tableWidget->setItem(ridx,cidx,item);

        }

    }
    if (cidx==4)
    {
        // Column Email
        for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
        {
            QTableWidgetItem* item = new QTableWidgetItem();

            item->setText(ListEmail[ridx]);
            ui->tableWidget->setItem(ridx,cidx,item);

        }

    }
    if (cidx==5)
    {
        // Column Telephone
        for (int ridx = 0 ; ridx < iRowCount ; ridx++ )
        {
            QTableWidgetItem* item = new QTableWidgetItem();

            item->setText(ListTelephone[ridx]);
            ui->tableWidget->setItem(ridx,cidx,item);

        }

    }
}
jollepe
  • 71
  • 13