Unsolved Data not displaying on the QTreeView, QTableView,QListView classes
-
-
@JonB I did convert all the objects from stack memory to heap, I'm not getting what you need?
-
@JonB
And also I did modelTest, There was no error. -
@JonB changed all to the heap, I think they will stay till application die, right?
code I have changed:Widget::Widget(QWidget *parent) : QWidget(parent) { QFile file("path"); if(!file.open(QIODevice::ReadOnly|QIODevice::Text)) { qDebug()<<"can't access the file"; } QString *addresses=new QString(QString::fromUtf8(file.readAll())); Addressbookmodel *model=new Addressbookmodel(addresses,this); QListView *listView=new QListView(); listView->setModel(model); listView->setModelColumn(0); listView->show(); QTreeView *treeView=new QTreeView; treeView.setModel(model); treeView.show(); QTableView *tableView=new QTableView; tableView.setModel(model); tableView.show(); setMiniumSize(800,400); QVBoxLayout *Vlayout=new QVBoxLayout(this); Vlayout->addWidget(listView); Vlayout->addWidget(treeView); Vlayout->addWidget(tableView); }
-
Addressbookmodel *model=new Addressbookmodel(addresses,this); treeView.setModel(&model); tableView.setModel(&model);
Now that
model
is a pointer, why the&
s? -
@JonB Sorry, typo mistake.
-
@JonB I did copy and paste from the early reply, So
-
I'm surprised you don't get into stack overflow.
AddressbookModel::rowCount
andAddressbookModel::columnCount
form an infinitely deep tree.You should
return 0;
if theparent
argumentisValid()
-
@VRonin okay,
In theAddressbookmodel.cpp
I changed the code to :int Addressbookmodel::rowCount(const QModelIndex &parent) const { if(parent.isValid()) { return 0; } return addressbook.count-2; } int Addressbookmodel::columnCount(const QModelIndex &parent) const { if(parent.isValid()) { return 0; } return addressbook.at(0).count(); }
I don't know why it is not used in the book and why am not getting data on the widgets still?.
-
@VRonin Can share me an example that helps me to understand how to create
QTreeView
model and subclass it?, so from this I can able to create my own models successfully. -
@thippu said in Data not displaying on the QTreeView, QTableView,QListView classes:
Can share me an example that helps me to understand how to create QTreeView model and subclass it?
http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
-
@VRonin , Thank you.