PDA

View Full Version : simple question;QSplitter,QTreeView



weixj2003ld
8th April 2009, 11:41
I create a splitter QtreeView TreeModel like this:
MainWindow::MainWindow()
{
....
ThreeModel model();

split=new QSplitter;
setCentralWidget(split);
view =new QTreeView(split);
view->setModel(&model);

QWidget * glw = new QWidget(split);
....
}
when I run my program,my three can not display?

but when change my code as follows,and run it,it is normal.
main()
{
......
ThreeModel model();
QTreeView view;
view.setModel(&model);
view.show();
.....
}
I don't know why?

spirit
8th April 2009, 11:49
allocate ThreeModel in the heap using operator new.


MainWindow::MainWindow()
{
....
ThreeModel *model = new ThreeModel();

split=new QSplitter;
setCentralWidget(split);
view =new QTreeView(split);
view->setModel(model);

QWidget * glw = new QWidget(split);
....
}

because your variable model will be deleted when we exitr from ctor, i.e. when out of scope will be reached.

weixj2003ld
8th April 2009, 11:59
Thank you for your answer.