PDA

View Full Version : [Solved] QSqlTableModel issues..



Nedlinin
29th November 2010, 22:26
I am having an issue with a school project. I am supposed to create a ballot server which can receive votes and tally then up. The storage is supposed to use SQL. One of the requirements is that I use a QSqlTableModel and QTableView to display the candidates and the amount of votes they current have. I am able to populate the table but am unable to view the data using a QSqlTableModel and QTableView. The window shows but it is completely empty.

The code is as follows:


void server::initializeModel(QSqlTableModel *model, const QString tableName){

qDebug() << database.open("awesomeUser", "awesomePassword");
model->setTable(tableName);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Votes"));
}

QTableView* server::createView(const QString &title, QSqlTableModel *model){

QTableView *view = new QTableView;
view->setModel(model);
view->setWindowTitle(title);
return view;
}

void server::votersTable(){

qDebug() << database.open("awesomeUser", "awesomePassword");

QSqlTableModel model;

initializeModel(&model, "Candidates");

QTableView *view = createView(QObject::tr("Voters"), &model);
view->show();
}

votersTable() is called from an on click signal from a button in the UI.

If anyone could tell me where I went wrong, I would very much appreciate it.

For reference, I know my Sql table has data inside. I run the following code:


{ query.exec("DROP TABLE Candidates"); query.exec("CREATE TABLE Candidates (candidate TEXT, votes INTEGER)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Buggs_Bunny', 0)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Daffy_Duck', 0)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Road_Runner', 0)"); query.exec("INSERT INTO Candidates (candidate, votes) VALUES ('Wile_E_Coyote', 0)"); query.exec("SELECT candidate, votes FROM Candidates");

}

qDebug() << "Candidates: "; while(query.next()){ qDebug() << query.value(0).toString() << " " << query.value(1).toString(); }


and receive the following output:


Candidates:
"Buggs_Bunny" "0"
"Daffy_Duck" "0"
"Road_Runner" "0"
"Wile_E_Coyote" "0"



I believe this is enough code to demonstrate the setup. For the complete code, pastebin has it at http://pastebin.com/r4tCtTcn

Please let me know if you can figure out whats going on with this. It would be greatly appreciated!

wysota
30th November 2010, 00:03
Your model is created on stack and when the votersTable() method returns it goes out of scope and is destroyed.

Nedlinin
30th November 2010, 00:10
Your model is created on stack and when the votersTable() method returns it goes out of scope and is destroyed.

I love you.

wysota
30th November 2010, 00:13
I love you.

I'm sorry I can't return the feeling :)

Nedlinin
30th November 2010, 00:14
I'm sorry I can't return the feeling :)

Haha. I appreciate the assistance. Sometimes the easiest reasons for the problems elude me.

weihua1984
4th November 2011, 15:06
i have a issues too when i use QSqlTableModel
setfilter("limit 4")