PDA

View Full Version : Main Window Layout treeview item



qwerty1234
30th March 2011, 15:41
Hi folks

I am having a bit of difficulty trying to get a treeview model to appear on my main window. I can get a blank treeview to appear but I want it to be used as a file directory and do not know how to link the blank treeview in my window to my directory model.

I can create a suitable file directory on it's own dialogue box but I'm struggling to get it to link with my blank treeview in the main page.

This is the code I use for making the working treeview directory in it's own dialogue box in the main.cpp.



QFileSystemModel *TreeModel = new QFileSystemModel();
TreeModel->setRootPath("/Users/John/Documents");
QTreeView tree;
tree.setModel(TreeModel);
QModelIndex idx = TreeModel->index("/Users/John/Documents");
tree.setRootIndex(idx);
tree.show();


For the mainwindow.cpp




MainWindow::MainWindow()
{

QWidget *widget = new QWidget;
setCentralWidget(widget);

QWidget *rightFiller = new QWidget;
rightFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

QTreeView *tree = new QTreeView;


QHBoxLayout *layout = new QHBoxLayout;

layout->addWidget(tree);
layout->addWidget(rightFiller);
widget->setLayout(layout);



Any advice would be greatly appreciated in helping me get this sorted

Added after 38 minutes:

Got it working. The mainwindow.cpp now looks like



MainWindow::MainWindow()

QWidget *widget = new QWidget;
setCentralWidget(widget);

QWidget *rightFiller = new QWidget
rightFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

QFileSystemModel *TreeModel = new QFileSystemModel();
TreeModel->setRootPath("/Users/John/Documents");
QModelIndex idx = TreeModel->index("/Users/John/Documents");

QTreeView *tree = new QTreeView;
tree->setModel(TreeModel);
tree->setRootIndex(idx);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(tree);
layout->addWidget(rightFiller);
widget->setLayout(layout);


Will probably be back soon enough asking for more help