You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, October 25th 2011, 7:10pm

Populating a TableWidget…Best Way of doing it?

I have a data set, in the form of a matrix… column names are people names, row names are certain attributes… each cell could be a list of 3 radio buttons OR a dropdown box. So i was thinking of using a TableWidget for all of this.
column names are read from file, row names are passed from another function.
should i use TableWidgetItem to add the cells and information? or use the StandardItemModel to add cells and information?
and can somebody show me some sample code in adding such information??
thanks a bunch!
I have this so far but i dont know why it doesnt work
i've tried to add tableWidgetItem in the loop, but nothing comes out, i've tried to add header with modelItem, nothing comes out.....

QFile file("../engD.txt");
if (!file.exists())
{
QMessageBox error;
error.setText("error");
error.exec();
}
if (!file.open(QFile::ReadOnly|QFile::Text))
{
qDebug();
}
QTextStream stream(&file);
QString mText;
while (!stream.atEnd())
{
for (int row =0; row<4; row++)
{
for (int col =0; col<4;col++)
{
mText= stream.readLine();
QTableWidgetItem *newItem = new QTableWidgetItem;
newItem->setText(mText);
ui->tableWidget->setItem(row, col, newItem);
}
}
}

QStandardItemModel *model = new QStandardItemModel(4,4);
model->setHeaderData(0,Qt::Horizontal,mText);
ui->tableWidget->setModel(model);
file.close();

2

Tuesday, October 25th 2011, 7:24pm

seems strange to set model after adding the items. Is that definitely correct?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Tuesday, October 25th 2011, 8:22pm

even if i set the model before adding items
the error was that the setModel of TableWidget is a private function...

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

4

Wednesday, October 26th 2011, 4:17pm

This might help...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void MainWindow::createTable()
{
    // add column headers from file (names.txt)
    QStringList colList;
    QFile file( "names.txt" );
    if( file.open( QIODevice::ReadOnly ) ){
        QTextStream textStream( &file );
        while( !textStream.atEnd() ){
            colList.append( textStream.readLine() );
        }
        file.close();
    } else {
        QMessageBox::critical( this, "File Missing", "Unable to load file: names.txt" );
        exit( 0 );
    }

    // add row headers from file (attributes.txt)
    file.setFileName( "attributes.txt" );
    QStringList rowList;

    if( file.open( QIODevice::ReadOnly ) ){
        QTextStream textStream( &file );
        while( !textStream.atEnd() ){
            rowList.append( textStream.readLine() );
        }
        file.close();
    } else {
        QMessageBox::critical( this, "File Missing", "Unable to load file: attributes.txt" );
        exit( 0 );
    }

    // Setup tableWidget based on file contents and
    // add items based on col and row counts
    tableWidget->setColumnCount( colList.count() );
    tableWidget->setHorizontalHeaderLabels( colList );
    tableWidget->setRowCount( rowList.count() );
    tableWidget->setVerticalHeaderLabels( rowList );

    // create items
    for( int i=0; i < rowList.count(); ++i ){
        for( int j=0; j < colList.count(); ++j ){
            // create items here
            tableWidget->setCellWidget( i,j, new QLabel( QString( " widget %1,%2 " ).arg(i). arg(j)
) );
        }
    }

    tableWidget->resizeColumnsToContents();
}