PDA

View Full Version : QTableView and MySQL



fnmblot
19th December 2007, 20:54
Could someone point me to an example of how to populate a QTableView widget created with designer, getting its rows from a MySQL database? Would I just do something like:



QSqlQueryModel *tvdsModel = new QSqlQueryModel;
tvdsModel->setQuery("SELECT blah FROM main");

ui.tvdsView->setModel(tvdsModel);

fnmblot
20th December 2007, 03:41
I guess all I had to do was think about it and try it before I posted. If it is any consolation, here is what I did:



//testTable.cpp
#include "tableTest.h"
#include <QtSql>
//
TableTestDlg::TableTestDlg( QWidget * parent, Qt::WFlags f)
: QDialog(parent, f)
{
tableViewUi.setupUi(this);

QSqlQueryModel *tableViewModel = new QSqlQueryModel;
tableViewModel->setQuery("SELECT piece_name, nn, status FROM main WHERE nn !='0' ORDER BY uid");

tableViewModel->setHeaderData(0, Qt::Horizontal, "Piece Name");
tableViewModel->setHeaderData(1, Qt::Horizontal, "NN");
tableViewModel->setHeaderData(2, Qt::Horizontal, "Status");

tableViewUi.tableView->setModel(tableViewModel);
tableViewUi.tableView->resizeColumnsToContents();
tableViewUi.tableView->setAlternatingRowColors(true);
}
TableTestDlg::~TableTestDlg()
{}

jpn
20th December 2007, 06:54
I guess all I had to do was think about it and try it before I posted.
It's always a good idea. :) Also, notice that a bunch of SQL examples is shipped with Qt.

KnumSoft
30th August 2013, 16:15
Hey fnmblot:

You probably have forgotten about this post, but I would like to thank you for your original question, AND the courtesy of following up with the solution you came up with. This is very helpful to others (like me).