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. Instantely updateing headerData of QItemModel (QTreeView) when changing selection
Forum Update on Monday, May 27th 2025

Instantely updateing headerData of QItemModel (QTreeView) when changing selection

Scheduled Pinned Locked Moved Unsolved General and Desktop
header dataupdateselectioninstantly
3 Posts 3 Posters 1.3k 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

    Hello,
    i have a QTreeView with differnt sorts of members in each row and therefore want to change the desctiption in the header according to the current selection.

    What i have come up with is following:

    QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
        if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
            QModelIndex index = m_tree_view->currentIndex();
            TreeItem* Item_selected = static_cast<TreeItem*>(index.internalPointer());
            std::string value;
            if(Item_selected != 0) {
                value = Item_selected->getDispName(section);
            } else{
                value = m_root_item->getDispName(section);
            }
            return QVariant(value.c_str());
        }
        return QVariant();
    }
    
    This works fine, however it often tankes some time till the headers get updated, (mouseover helps, but sometimes works without)
    Is there a way to re-draw the header instantainiously?
    
    
    1 Reply Last reply
    0
    • I Offline
      I Offline
      iota142
      wrote on last edited by iota142
      #2

      Hi,

      Do you emit the signal QAbstractItemModel::headerDataChanged() when you change your header datas?

      The documentation says: "When reimplementing the setHeaderData() function, this signal must be emitted explicitly."
      -> documentation of QAbstractItemModel::headerDataChanged.

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        https://en.wikipedia.org/wiki/Dependency_inversion_principle

        Basically what you are doing is evil.

        You should connect m_tree_view->selectionModel()'s currentIndexChanged() signal with a slot that calls setHeaderData in the model. that's all

        "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
        1

        • Login

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