Results 1 to 2 of 2

Thread: TreeView, TableView

  1. #1
    Join Date
    Jul 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default TreeView, TableView

    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();
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: TreeView, TableView

    All you have to do is to connect a few signals to enable the behaviour you want:
    Qt Code:
    1. // make table follow tree clicks
    2. QObject::connect(ui.treeView, SIGNAL(clicked(const QModelIndex&)),
    3. ui.tableView, SLOT(setRootIndex(const QModelIndex&)));
    4.  
    5. // table navigation by double clicks
    6. QObject::connect(ui.tableView, SIGNAL(doubleClicked(const QModelIndex&)),
    7. ui.tableView, SLOT(setRootIndex(const QModelIndex&)));
    8.  
    9. // make tree update upon table navigation
    10. QObject::connect(ui.tableView, SIGNAL(doubleClicked(const QModelIndex&)),
    11. ui.treeView, SLOT(setCurrentIndex(const QModelIndex&)));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. TreeView click item not connecting
    By Big Duck in forum Qt Programming
    Replies: 2
    Last Post: 9th June 2006, 19:38
  2. Modified tableview
    By dkite in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2006, 01:58
  3. How to set the alignment in a TableView?
    By vratojr in forum Qt Programming
    Replies: 1
    Last Post: 9th March 2006, 13:21
  4. Widgets in TableView
    By boonie in forum Qt Programming
    Replies: 4
    Last Post: 21st February 2006, 19:55
  5. I can't assign TableView signals...
    By tomek in forum Newbie
    Replies: 5
    Last Post: 9th January 2006, 21:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.