PDA

View Full Version : TableView does not show (up) with QSqlTableModel



fulin
27th June 2011, 08:56
Hi,

I created a Qt window form (GUI) and put an empty tableView on the form in Qt Creator. When I started the GUI, the GUI::Initialization() function was called. But nothing happened with the tableView -- no header, no title, no row, no column. It was still empty. Why it did not show up? There was no any error message. What's wrong with my code? Can anybody help me? My code is as the following.

Also how to save the QSqlTableModel contents to a file and load it afterwards from the file?

Thank you in advance.

/Fulin



void GUI::Initialization()
{
QSqlTableModel model;
initializeModel(&model);
tableView_init(&model);
}
void GUI::initializeModel(QSqlTableModel *model)
{
model->setTable("person");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
}

void GUI::tableView_init(QSqlTableModel *model)
{
ui->tableView->setModel(model);
ui->tableView->show();
}

schnitzel
27th June 2011, 17:25
it looks like you started off with Qt's tablemodel example but then changed it and left out the connection to the sqlite db. If there is no data supplied, the table will be completely empty and the header won't be shown.

oh, and btw use code tags next time.

ChrisW67
27th June 2011, 23:03
Even if there was a database available: you initialise a QSQlQueryModel allocated in a local scope that is destroyed at the end of the
GUI::Initialization() method.

BTW: Are you the same person as sujan.dasmahapatra (http://www.qtcentre.org/members/10823-sujan.dasmahapatra) from 42722 or is it just a coincidence?

schnitzel
27th June 2011, 23:49
BTW: Are you the same person as sujan.dasmahapatra (http://www.qtcentre.org/members/10823-sujan.dasmahapatra) from 42722 or is it just a coincidence?

perhaps they are in the same class working on the same assignment :)

fulin
28th June 2011, 12:13
Thanks for the hints. I'm just a QT newbie and want to learn and use a simple predefined model/view for my 2D parameter data online tuning. My objectives: data can be saved to file and load from a file; data can be viewed and edited from the tableview. Next step is to put data in sql database.

I tried the QStandardItemModel also as the following, still nothing is seen from the tableview --empty. It seems that the qt model/view is really not so easy for a qt-newbie.

Any suggestions?:)


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);
}
ui->tableView->setModel(&model);
ui->tableView->show();

schnitzel
28th June 2011, 16:53
What is the point of bringing up more questions?
have you fixed your initial problem?
I *can* reproduce the empty header, empty table by commenting out the call to 'createConnection'.

Here is a suggestions... try the tablemodel example as is and see if that works for you.

ChrisW67
29th June 2011, 00:26
Your code with minimal support to make it self-contained:


#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();
}

The result:
6619
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).

fulin
30th June 2011, 10:16
Thank you, ChrisW67 and Schnitzel .

You are right. Now I got the QStandardItemModel work. Great ;)! I'll try to add QSqlQueryModel and to make QSqlTableModel work.