13.2.2.  Multiple Views

[ fromfile: qtmodelview.xml id: multipleviews ]

Example 13.2. src/modelview/multiview/createModel.cpp

#include <QtGui> 

QStandardItemModel* createModel(QObject* parent, int rows, 
                               int cols, int childNodes) {
    QStandardItemModel* 
        model = new QStandardItemModel(rows, cols, parent); 
    for( int r=0; r<rows; r++ ) 
        for( int c=0; c<cols; c++)  {
            QStandardItem* item = new QStandardItem(
                QString("Row:%0, Column:%1").arg(r).arg(c) ); 
            if( c == 0 ) 1
                for( int i=0; i<childNodes; i++ ) {                    
                    QStandardItem* child = new QStandardItem(
						QString("Item %0").arg(i) );
                    item->appendRow( child );
                }
            model->setItem(r, c, item);
        }
    model->setHorizontalHeaderItem( 0, new QStandardItem( "Name" ));
    model->setHorizontalHeaderItem( 1, new QStandardItem( "Value" ) );
    return model;
}

1

Add child nodes to elements in the first column


Example 13.3. src/modelview/multiview/multiview.cpp

[ . . . . ]

#include "createModel.h"

int main( int argc, char** argv ) {
  QApplication app( argc, argv );
  QStandardItemModel* model = createModel(&app);
  QSplitter vsplitter(Qt::Vertical);
  QSplitter hsplitter;                    1
  
  QListView list;
  QTableView table;
  QTreeView tree;
  QColumnView columnView;
  [ . . . . ]

  list.setModel( model );
  table.setModel( model );
  tree.setModel( model );	                           2
  columnView.setModel (model);
  [ . . . . ]

  list.setSelectionModel( tree.selectionModel() );
  table.setSelectionModel( tree.selectionModel() );  3
  columnView.setSelectionModel (tree.selectionModel());
  table.setSelectionBehavior( QAbstractItemView::SelectRows );
  table.setSelectionMode( QAbstractItemView::SingleSelection );
  

1

By default, children layout horizontally.

2

Share the same model.

3

Common selection model.


Figure 13.6.  Multiple Views, One Model

Multiple Views, One Model

Example 13.4. src/modelview/multiview/multiview.cpp

[ . . . . ]

  hsplitter.addWidget( &list );
  hsplitter.addWidget( &table );
  vsplitter.addWidget( &hsplitter );
  vsplitter.addWidget ( &tree );
  vsplitter.addWidget ( &columnView );

  vsplitter.setGeometry(300, 300, 500, 500);
  vsplitter.setWindowTitle("Multiple Views - Editable Model");