Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Changing headerData in QTreeView depending on selection
Forum Updated to NodeBB v4.3 + New Features

Changing headerData in QTreeView depending on selection

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hi,

    i have a QTreeView, that shows some model derived from QAbstractItemModel.
    now i want to show different headerData on the TreeView depending on which type of Item in the view is selected.
    How do i implement this?

    My problem is, that normally MyTreeModel does not know the TreeView and therefore I do not know where to get the selection information from.

    I have tried to get the inof directly from the view, which kind of works, however when using a QSortFilterProxyModel it will crash.

    QVariant MyTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if(m_root_item != NULL){
            if (orientation == Qt::Horizontal && role == Qt::DisplayRole){
    
                // getting the current item from a pointer to the view stored in the model
                // this seems to be kind of a bad idea
                QModelIndex index = m_tree_view->currentIndex();
    
                if(!index.isValid()){
                    return QVariant();
                }
                
                Item* item_selected = static_cast<Item*>(index.internalPointer());
                if(item_selected != NULL){
                    std::string value;
                    value = item_selected->getHeader(section);
                    return QVariant(value.c_str());
                }
            }
        }
        return QVariant();
    }
    
    what is the right way to implement this?
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why not listening on QTreeView::selectionChanged somewhere and adjust the header data the way you need it?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by gde23
        #3

        @Christian-Ehrlicher Thanks for the hint.

        The easiest way to implement this i can imagine atm is something as follows:

        • A slot in MyTreeView that is called when selection changes.
        • From there i call the TreeModel::data() for the current index and add a new role to get the headerData from the items.
        • Then i call setHeaderData for all the columns and store the data in the TreeModel.
        • This data is returned when headerData() is called.

        I guess this will work, however it sound not like the straight forward approach for such a common application?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          This is also the way I would solve it. But I don't see this as a common situation. I've no application in mind which changes the header based on the current (selected) index ...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            If you have a working implementation of setHeaderData and headerData in your model you can just:

            QObject::connect(treeView->selectionModel(),&QItemSelectionModel::selectionChanged,treeView->model(),[=](const QItemSelection &selected, const QItemSelection &deselected)->void{
            const auto selectedIndexes = selected.indexes();
            const auto deselectedIndexes = deselected.indexes();
            for(auto&& idx : selectedIndexes){
            treeView->model()->setHeaderData(idx.column(),Qt::Horizontal,QStringLiteral("%1 Selected").arg(idx.column()));
            }
            for(auto&& idx : deselectedIndexes ){
            treeView->model()->setHeaderData(idx.column(),Qt::Horizontal,QString::number(idx.column()));
            }
            });
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved