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. [SOLVED] Subclasing QHeaderView - mouseReleaseEvent() re-implamantaion issue
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Subclasing QHeaderView - mouseReleaseEvent() re-implamantaion issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.6k Views 1 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.
  • A Offline
    A Offline
    Aviad Rotstein
    wrote on last edited by Aviad Rotstein
    #1

    Hi,

    [QT 5.5.1 | OS: Linux]

    Here is my code:

    Header:

    class MyHeaderItem: public QHeaderView
    {
    public:
        MyHeaderItem(QWidget * parent = 0);
    
    protected:
        virtual void paintSection(QPainter* poPainter, const QRect& oRect, int iLogicalIndex) const;
    
        virtual void mouseReleaseEvent(QMouseEvent *e);
    }
    

    Source:

    MyHeaderItem::MyHeaderItem(QWidget *parent)
        :QHeaderView(Qt::Horizontal, parent)
    {
    }
    
    void MyHeaderItem::paintSection(QPainter *poPainter, const QRect &oRect, int iLogicalIndex) const
    {
            QHeaderView::paintSection(poPainter, oRect, iLogicalIndex);
            // Some code
    }
    
    void MyHeaderItem::mouseReleaseEvent(QMouseEvent *e)
    {
        QHeaderView::mouseReleaseEvent(e);
    }
    

    My Problem:
    User can't sort the table items by clicking the header sections.
    Hypothesis
    When the user click on header section, we enter to My mouseReleaseEvent(), but Its seem that the QHeaderView
    mouseReleaseEvent() is not executed.

    Any ideas?

    Thanx for advanced,
    Aviad

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

      Hi and welcome to devnet,

      What view are you using ? Did you make it sortable ?

      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
      • A Offline
        A Offline
        Aviad Rotstein
        wrote on last edited by
        #3

        Hi,
        Thanks for your rapid replay...

        I'm using QTableView, and yes, I make it sortable.
        Furthermore, the triangular sort sign is shown, but do not 'flip' at mouse clicked...

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

          Can you share the code you use to initialize everything ?

          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
          • A Offline
            A Offline
            Aviad Rotstein
            wrote on last edited by
            #5

            Something like this:

            //////////////////////////////////////////
            // Subclasing QAbstractTableModel - header
            //////////////////////////////////////////
            MyQAbstractTableModel : public QAbstractTableModel
            {
            public:
                QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
                QVariant headerData ( int section,Qt::Orientation orientation, int role) const;
            }
            
            //////////////////////////////////////////
            // Main code
            //////////////////////////////////////////
            
            // Allocate table view
            QTableView poTableView =
                        new QTableView;
            
            // Allocate header
            MyHeaderItem poHeaderView =
                        new MyHeaderItem(this);
            
            // Set table header
            poTableView->setHorizontalHeader(poHeaderView);
            
            // Allocate model
            MyQAbstractTableModel  * poModel =  new MyQAbstractTableModel;
            
            
            // Set table view model
            poTableView->setModel(poModel);
            
            // Set sorting enabled
            poTableView->setSortingEnabled(true);
            
            // Hide grid
            poTableView->setShowGrid(false);
            

            I hope I do not miss something important...

            Thanks

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Aviad Rotstein
              wrote on last edited by
              #6

              OK,
              all I want to do is to add icons to the header sections. Lets say I do it by the headerData() decorationRole.

              Now I have another problem - How can I set the alignment of the icon to the center?

              Thanks,
              Aviad

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Aviad Rotstein
                wrote on last edited by
                #7

                Now, I make my way like this, using this idea, when my drawControl function is:

                void drawControl(ControlElement oCtrElement, const QStyleOption * poStylrOptionption, QPainter * poPainter, const QWidget * poWidget = 0) const
                    {
                        if (oCtrElement == CE_HeaderLabel) {
                
                            QStyleOptionHeader *poStyleOptionHeader =
                                    (QStyleOptionHeader *) poStylrOptionption;
                
                            QIcon oIcon = qvariant_cast<QIcon>(poStyleOptionHeader->icon);
                
                            if(oIcon.isNull()){
                                QProxyStyle::drawControl(oCtrElement, poStylrOptionption, poPainter, poWidget);
                                return;
                            }
                
                            QSize oIconSize = QSize(15, 15);
                
                            QRect oRect = poStyleOptionHeader->rect;
                            QPixmap oIconPixmap = oIcon.pixmap(oIconSize.width(),oIconSize.height());
                
                            poPainter->drawPixmap(oRect.left() + (oRect.width() / 2), oRect.top(), oIconPixmap.width(), oIconPixmap.height(), oIconPixmap);
                            return;
                        }
                        QProxyStyle::drawControl(oCtrElement, poStylrOptionption, poPainter, poWidget);
                    }
                

                So, my first problem do not solved' but i found workaround. Hope Its will be useful for somebody...

                Aviad

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

                  Add:

                  setSectionsClickable(true);
                  setHighlightSections(true);
                  

                  to your custom header constructor.

                  On a side note MyHeaderItem is not really a good name for a QHeaderView, item is rather used for model elements.

                  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
                  1
                  • A Offline
                    A Offline
                    Aviad Rotstein
                    wrote on last edited by
                    #9

                    Yes!!
                    Exactly what I need.
                    Thanks allot!!

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

                      You're welcome !

                      By the way, no need to modify the thread title, just use the "Topic Tool" button to mark it as solved :)

                      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