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. Update QHeaderView section count to reflect changes in the model
Forum Updated to NodeBB v4.3 + New Features

Update QHeaderView section count to reflect changes in the model

Scheduled Pinned Locked Moved Solved General and Desktop
qtreeviewqheaderview
14 Posts 3 Posters 6.4k Views 3 Watching
  • 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.
  • kshegunovK kshegunov

    @skebanga
    Ok, I admit it was not a very clever suggestion. Maybe better is to try to signal the view (from the model) that header data should be updated, not emitting the view's signals directly. Have you tried the model's QAbstractItemModel::headerDataChanged signal? For example:

    void Model::signalAddedRows(Qt::Orientation orientation)
    {
        emit headerDataChanged(orientation, 0, columnCount());
    }
    
    S Offline
    S Offline
    skebanga
    wrote on last edited by
    #5

    That's it! Thank you!

    All that is required is to emit the headerDataChanged signal from the model

    if (_model.columnCount() != _tree.header()->count())
        emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
    

    The newly added columns appear now.

    Additional feedback of the change propagating is evidenced by connecting to the QHeaderView::sectionCountChanged slot:

    if (_model.columnCount() != _tree.header()->count())
    {
        // just to prove this is working
        connect(_tree.header(), &QHeaderView::sectionCountChanged, [](int old_count, int new_count)
                {
                    std::cout << "sectionCountChanged: " << old_count << " -> " << new_count << "\n";
                });
        emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
    }
    

    Results in the following on stdout:

    sectionCountChanged: 28 -> 31
    
    kshegunovK 1 Reply Last reply
    1
    • S skebanga

      That's it! Thank you!

      All that is required is to emit the headerDataChanged signal from the model

      if (_model.columnCount() != _tree.header()->count())
          emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
      

      The newly added columns appear now.

      Additional feedback of the change propagating is evidenced by connecting to the QHeaderView::sectionCountChanged slot:

      if (_model.columnCount() != _tree.header()->count())
      {
          // just to prove this is working
          connect(_tree.header(), &QHeaderView::sectionCountChanged, [](int old_count, int new_count)
                  {
                      std::cout << "sectionCountChanged: " << old_count << " -> " << new_count << "\n";
                  });
          emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
      }
      

      Results in the following on stdout:

      sectionCountChanged: 28 -> 31
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #6

      @skebanga
      You're welcome, I'm glad it worked.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #7

        Hi,

        Out of curiosity, are you emitting the signal from outside of the model ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Out of curiosity, are you emitting the signal from outside of the model ?

          S Offline
          S Offline
          skebanga
          wrote on last edited by
          #8

          @SGaist:

          Out of curiosity, are you emitting the signal from outside of the model ?

          I am, as detailed in my previous reply:

          emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
          

          AFAIK emit is just syntactic sugar, and doesn't actually do anything.

          So the same effect could be had by just doing:

          _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
          

          I leave emit in just to show that it's a signal I'm calling rather than a vanilla member function

          PS: Forgive the delay in responding, I don't have the required reputation on here to post more than 1 message every 500 seconds

          kshegunovK 1 Reply Last reply
          1
          • S skebanga

            @SGaist:

            Out of curiosity, are you emitting the signal from outside of the model ?

            I am, as detailed in my previous reply:

            emit _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
            

            AFAIK emit is just syntactic sugar, and doesn't actually do anything.

            So the same effect could be had by just doing:

            _model.headerDataChanged(Qt::Horizontal, 0, _model.columnCount());
            

            I leave emit in just to show that it's a signal I'm calling rather than a vanilla member function

            PS: Forgive the delay in responding, I don't have the required reputation on here to post more than 1 message every 500 seconds

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #9

            @skebanga said:

            Forgive the delay in responding, I don't have the required reputation on here to post more than 1 message every 500 seconds

            Now you do. Cheers.

            Read and abide by the Qt Code of Conduct

            S 1 Reply Last reply
            1
            • kshegunovK kshegunov

              @skebanga said:

              Forgive the delay in responding, I don't have the required reputation on here to post more than 1 message every 500 seconds

              Now you do. Cheers.

              S Offline
              S Offline
              skebanga
              wrote on last edited by
              #10

              @kshegunov

              :) thanks!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #11

                Indeed emit is a macro that turns empty. The point is that from a coding point of view, you don't "emit from another class". Signals are emitted from within the class or the sub-class you just wrote.

                Doing like you do right now makes the code confusing and also breaks the single responsibility principle i.e. why would your containing widget play with "internals" of your model ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 2 Replies Last reply
                0
                • SGaistS SGaist

                  Indeed emit is a macro that turns empty. The point is that from a coding point of view, you don't "emit from another class". Signals are emitted from within the class or the sub-class you just wrote.

                  Doing like you do right now makes the code confusing and also breaks the single responsibility principle i.e. why would your containing widget play with "internals" of your model ?

                  S Offline
                  S Offline
                  skebanga
                  wrote on last edited by
                  #12

                  @SGaist Fair enough - point noted, thanks for the suggestion

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Indeed emit is a macro that turns empty. The point is that from a coding point of view, you don't "emit from another class". Signals are emitted from within the class or the sub-class you just wrote.

                    Doing like you do right now makes the code confusing and also breaks the single responsibility principle i.e. why would your containing widget play with "internals" of your model ?

                    S Offline
                    S Offline
                    skebanga
                    wrote on last edited by
                    #13

                    @SGaist

                    one thing to note though, re emitting a signal from externally to the model, is that this is an interplay between the view and the model.

                    The model shouldn't need to know anything about the view, but in this case, it's the fact that the model's columnCount() has increased, whilst the view's hasn't.

                    In terms of best practice, would you suggest anything in this case?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Technically the "last number of columns" is rather a model specific data. Since that part can change, the model should check the last value and then emit the signal accordingly.

                      The section ordering part, on the other hand, is indeed view specific data and the model shouldn't do anything with them.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

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