Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Instantely updateing headerData of QItemModel (QTreeView) when changing selection

    General and Desktop
    header data update selection instantly
    3
    3
    945
    Loading More Posts
    • 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.
    • gde23
      gde23 last edited by

      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 Reply Quote 0
      • I
        iota142 last edited by iota142

        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 Reply Quote 2
        • V
          VRonin last edited by VRonin

          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 Reply Quote 1
          • First post
            Last post