PDA

View Full Version : Set model not working!!



aurora
29th May 2012, 09:45
I am using QSqlQuerModel in my program and using table view to display the contents of the model.
But when i set the model nothing displays in the table other than two brown lines at the horizontal and verticla headers.....
my program code....





void win::on_pushButton_clicked()
{

QStringList signalList;
QString filename;
filename="C:/Users/Desktop/abc.txt";
headersList=GetheaderNames(filename);

QFileInfo file(filename);
createTable(file.baseName(),headersList);
GetFileContent(filename);
QSqlQueryModel plainModel2;
initializeModel(&plainModel2,file.baseName(),headersList);

ui->tableView_2->setModel(&plainModel2);
}




initialiseModel function


void initializeModel(QSqlQueryModel *model,QString filename, QStringList headersList)
{
QString queryString=QString("select * from %1").arg(filename);

model->setQuery(queryString);
for(int i=0;i<headersList.count();i++)
{
model->setHeaderData(i, Qt::Horizontal, (headersList.at(i)));
}

}



plz help me to sort out the problm here!!

Jonny174
29th May 2012, 10:53
Is it work?


model->setHeaderData(i, Qt::Horizontal, (headersList.at(i)));

Output headersList.at(i) with qDebug();
Try create plainModel2 in heap.


QSqlQueryModel *plainModel2 = new QSqlQueryModel;
initializeModel( plainModel2, file.baseName(), headersList );


Added after 5 minutes:

Check errors


model->setQuery(queryString);
if (model->lastError().isValid())
qDebug() << model->lastError().text();

aurora
29th May 2012, 12:22
Thank u jonny......as u identified, i created it on stack......now its working....:)

Santosh Reddy
29th May 2012, 12:35
You are creating the model on stack, which will not work in your case, as it will go out of scope after on_pushButton_clicked(0 is exited.

Create the model on heap.

QSqlQueryModel* plainModel2 = new QSqlQueryModel(...)