I have found example how to use QTableView:
http://doc.trolltech.com/4.5/sql-querymodel.html
It works fine. Data is shown in QTableView.
But QTableView in this example is created dynamically in main.cpp file.
In my application I have main form and I added QTableView in designer. I try to populate this QTableView in constructor but without result:
MainApplication::MainApplication(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainApplication)
{
ui->setupUi(this);
QMap<QString, double> currencyMap;
currencyMap.insert("AUD", 1.3259);
currencyMap.insert("CHF", 1.2970);
currencyMap.insert("CZK", 24.510);
CurrencyModel currencyModel;
currencyModel.setCurrencyMap(currencyMap);
ui->tableView_currencies->setModel(¤cyModel);
ui->tableView_currencies->setAlternatingRowColors(true);
ui->tableView_currencies->setWindowTitle(QObject::tr("Currencies"));
ui->tableView_currencies->show();
}
QTableView is showing on main form empty, only columns and rows headers are visible. And data is not shown.
Does anybody know of a site with examples where components like QTableView, QListView are added in designer? In trolltech (nokia) tutorials all components are created dynamically.
The model is no longer valid after the constructor is executed!
You create a local object
currencyModelthat will be destroyed when it goes out of scope (at the end of the c’tor), but pass a pointer to it as the model for the table view!The table view doesn’t deep-copy the given model, and in fact doesn’t even take ownership of the passed pointer:
You should simply allocate the model no the heap (using
new) and set the view as it’s parent object. in that way, the table view will also handle its deletion: