Unsolved [SOLVED] Subclasing QHeaderView - mouseReleaseEvent() re-implamantaion issue
-
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 -
Hi and welcome to devnet,
What view are you using ? Did you make it sortable ?
-
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... -
Can you share the code you use to initialize everything ?
-
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
-
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 -
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
-
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.
-
Yes!!
Exactly what I need.
Thanks allot!! -
You're welcome !
By the way, no need to modify the thread title, just use the "Topic Tool" button to mark it as solved :)