Reputation: 2691
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.
Upvotes: 2
Views: 3006
Reputation: 11513
The model is no longer valid after the constructor is executed!
You create a local object currencyModel
that 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:
The view does not take ownership of the model unless it is the model's parent object because the view may be shared between many different views. (QTableView doc)
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:
CurrencyModel *currencyModel = new CurrencyModel(ui->tableView_currencies);
Upvotes: 5
Reputation: 49
I met and you the same question king_nak is right your currencyModel is Temporary variables;
QxCurrencyModel* currencyModel = new QxCurrencyModel;
Upvotes: -1
Reputation: 39807
I've used QTableWidget rather than QTableView with great success in designer.
If you really want to understand why the *View's aren't working while *Widgets are, you should use designer/moc to produce the code and compare them to each other, then compare to the working *View examples. Personally, I was satisfied when the *Widget types worked so I stopped looking; *Widget inherits from *View.
Upvotes: 0