Hi folks... firstly let me appologize for such a newbie question.
I am transitioning from C++ Builder on Windows and am struggling a bit with the way things are done in QT.
I have a simple form with a QTableView on it and want to display some data from my database in the View.
I have followed some code from "The Book fo QT4" and have the data coming out of the query OK, but the View remains totally empty.
The code I'm using is as follows
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
QSqlQuery query
("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid");
while(query.next())
{
//loop
res = query.value(record.indexOf("sharename")).toString() + " : ";
res += query.value(record.indexOf("totalvalue")).toString();
qDebug() << query.at() << ": " << res;
}
queryModel.setQuery("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid");
if(queryModel.lastError().isValid())
qDebug() << queryModel.lastError();
ui->tableView->setModel(&queryModel);
queryModel.
setHeaderData(0, Qt
::Horizontal,
QObject::tr("heading 1"));
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
QSqlQuery query("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid");
QSqlRecord record = query.record();
while(query.next())
{
//loop
QString res;
res = query.value(record.indexOf("sharename")).toString() + " : ";
res += query.value(record.indexOf("totalvalue")).toString();
qDebug() << query.at() << ": " << res;
}
QSqlQueryModel queryModel;
queryModel.setQuery("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid");
if(queryModel.lastError().isValid())
qDebug() << queryModel.lastError();
ui->tableView->setModel(&queryModel);
queryModel.setHeaderData(0, Qt::Horizontal, QObject::tr("heading 1"));
}
To copy to clipboard, switch view to plain text mode
I know the query is working because the first loop to qDebug is putting out the correct data.
Now.. I know I'm not doing this correctly and its going to be stupidly easy.. but.. I simply can't work out how to play with widgets that i've put on the main window using QtCreator's GUI maker (or whatever its called).
Any advice please??
Thanks
Peter Nunn
Bookmarks