PDA

View Full Version : TreeView, TableView



rbrand
4th July 2006, 09:34
Hi,

I'm new with Qt and Icustomize the simpletreemodel example. I would like to display the model also in a tableview. In the tableview only the treeItems on the first level (parent=root) are displayed. Selecting a treeItem in another level in the tableview nothing changes. Is this behaviour correct ?

R. Brand

Here is my main-routine

#include <QtGui>

#include "ui_treetable.h"
#include "treemodel.h"
#include <QFile>

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(simpletreemodel);

QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(true);

QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();

QItemSelectionModel *selectionmodel;
QMainWindow *form = new QMainWindow;
Ui::MainWindow ui;
ui.setupUi(form);

ui.treeView->setModel(&model);
ui.tableView->setModel(&model);

selectionmodel = ui.treeView->selectionModel();
ui.tableView->setSelectionModel(selectionmodel);
form->show();
return app.exec();
}

jpn
4th July 2006, 09:54
All you have to do is to connect a few signals to enable the behaviour you want:


// make table follow tree clicks
QObject::connect(ui.treeView, SIGNAL(clicked(const QModelIndex&)),
ui.tableView, SLOT(setRootIndex(const QModelIndex&)));

// table navigation by double clicks
QObject::connect(ui.tableView, SIGNAL(doubleClicked(const QModelIndex&)),
ui.tableView, SLOT(setRootIndex(const QModelIndex&)));

// make tree update upon table navigation
QObject::connect(ui.tableView, SIGNAL(doubleClicked(const QModelIndex&)),
ui.treeView, SLOT(setCurrentIndex(const QModelIndex&)));