QTreeView and QTabWidget to display only selected items of QTableViews

475 Views Asked by At

Given two SQLite tables addresses and messages, what is the best way to map them to a QTreeView and QTabWidget in such a way, that if I select one row in a QTableView (which is either mapped to addresses or messages), the selected item is

  1. opened as a new tab to display its contents, and
  2. inserted as an item in the QTreeView to represent an "opened" item.

I've managed to open new tabs by creating two custom QWidgets, one for addresses and one for messages. On selecting a row in the QTableView, the correct (either addresses or messages) QWidget is created and fed with the SQL model and the index. The widget then creates a QDataWidgetMapper and displays the given index. AddressWidget example:

AddressWidget::AddressWidget(QSqlRelationalTableModel *model, QModelIndex &index, QWidget *parent) :
    QWidget(parent)
{
    // ...

    // set up widget mapper
    mapper = new QDataWidgetMapper(this);
    mapper->setModel(this->model);
    mapper->addMapping(streetEdit, this->model->fieldIndex("street"));
    mapper->addMapping(houseNumberEdit, this->model->fieldIndex("houseNumber"));
    mapper->addMapping(zipEdit, this->model->fieldIndex("zip"));
    mapper->addMapping(cityEdit, this->model->fieldIndex("city"));
    mapper->setCurrentModelIndex(index);
}

How can I extend this to the QTreeView, so that the tree displays opened items? Each tab in the QTabWidget should have a corresponding item in the QTreeView.

If there is a right way of doing this, please add it or replace my scenario entirely.

0

There are 0 best solutions below