Your code with minimal support to make it self-contained:
#include <QtGui>
#include <QDebug>
int main(int argc, char *argv[])
{
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
QString("Row:%0, Column:%1").
arg(r
).
arg(c
) );
if( c == 0 )
for( int i=0; i<3; i++ )
model.setItem(r, c, item);
}
view.setModel(&model);
view.show();
return app.exec();
}
#include <QtGui>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model( 5, 2 );
for( int r=0; r<5; r++ )
for( int c=0; c<2; c++)
{
QStandardItem *item = new QStandardItem(
QString("Row:%0, Column:%1").arg(r).arg(c) );
if( c == 0 )
for( int i=0; i<3; i++ )
item->appendRow( new QStandardItem( QString("Item %0").arg(i) ) );
model.setItem(r, c, item);
}
QTableView view;
view.setModel(&model);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
The result:
example.png
Clearly it can work (although your attempt to build a tree is wasted on a table view).
You need to ask yourself why it is not working in your case. Consider the lifetime of the model (and ignore Qt... this is pure C++ understanding).
Bookmarks