Sorry, I've posted my hint without refresh the page with your post.
Sorry, I've posted my hint without refresh the page with your post.
"ui.cueTable->setModel(*model);"
Doesn't work for me either. What syntax do you suggest? I'm not very comfortable with pointers yet.
Thank you for the help thus far
There are 2 errors in your code (both of which Wysota mentioned).
i) you must pass a POINTER in the call to setModel
ii) you must ensure that that pointer actually lives long enough
The easiest way to to do that, is:
Qt Code:
<< replace your calls to "model." with "model->" >> setModel(model);To copy to clipboard, switch view to plain text mode
If the method you posted should not go out of scope (e.g. if it is followed by a QApplication::exec() or so...); then you could leave your code as is and just write otherwise you will leave your view with a dangling pointer and the code will crash in all probability.
HTH
Christoph
Thanks for your response, caduel, I think I understand it better now.
I've moved everything back into my ui class, just to make it simpler for now. I put a
QSqlTableModel *model;
in the public declaration for that class. In the implementation, I have:
Qt Code:
cuedb.setHostName("westdalelighting"); cuedb.setDatabaseName("Cues"); cuedb.setUserName("Westdale"); cuedb.setPassword("westdale"); bool ok = cuedb.open(); int row = 1; model->setTable("employee"); model->setFilter("salary > 50000"); model->setSort(2, Qt::DescendingOrder); model->select(); for (int i = 0; i < model->rowCount(); ++i) { int salary = model->record(i).value("salary").toInt(); qDebug() << name << salary; } model->insertRows(row, 1); model->setData(model->index(row, 0), 1013); model->setData(model->index(row, 1), "Peter Gordon"); model->setData(model->index(row, 2), 68500); model->submitAll(); cueTable->setModel(model);To copy to clipboard, switch view to plain text mode
Which compiles successfully, but fails to put any items into my cueTable widget. The cueTable widget was constructed by QDesigner-- am I perhaps missing something there?
Where do you actually create that model?
A line of
just declares a pointer. When your class is instantiated, all you have is an (uninitialized, i.e. random!) pointer to a model.
You need to have a real model, i.e. a line like
somewhere.Qt Code:
To copy to clipboard, switch view to plain text mode
(Side note: On the other hand, if you are willing to move the model (pointer) into your class, then you could of course put the real model there. Then you could revert to your original code in most places.)
I want the model to be accessible to other functions within that class, so I'll need to declare *model in the class definition, no?
Now I have "QSqlTableModel *model;" in the public declaration of the class, and
Qt Code:
void MainWindowImpl::cueNewShowClick(){ cuedb.setHostName("westdalelighting"); cuedb.setDatabaseName("Cues"); cuedb.setUserName("Westdale"); cuedb.setPassword("westdale"); bool ok = cuedb.open(); int row = 1; model->setTable("employee"); model->setFilter("salary > 50000"); model->setSort(2, Qt::DescendingOrder); model->select(); for (int i = 0; i < model->rowCount(); ++i) { int salary = model->record(i).value("salary").toInt(); qDebug() << name << salary; } model->insertRows(row, 1); model->setData(model->index(row, 0), 1013); model->setData(model->index(row, 1), "Peter Gordon"); model->setData(model->index(row, 2), 68500); model->submitAll(); cueTable->setModel(model); }To copy to clipboard, switch view to plain text mode
Still though, nothing appears in the QTableView. This event is executed when a "populate table" button is pressed.
Does such table exist?
J-P Nurmi
try to print the output of your database calls
i.e. do something like
Do the same for all calls that might fail.Qt Code:
bool ok = cuedb.open(); qDebug() << "open: ok="<<ok;To copy to clipboard, switch view to plain text mode
Bookmarks