QHeaderView induvidual background colors
-
Hello, I need a QTableView with induvidual dynamic Header Coloring.
Stylesheeds dont help because they syle all colums with the same style.I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.
What can I do ?
Is there a methode with rules for the header ?
-
@Andy314 said in QHeaderView induvidual background colors:
I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.
You can re-implement
paintSection
, add your things you want to change and then call the base implementationQHeaderView::paintSection
to preserve the default look where you do not make changes. -
@Pl45m4 said in QHeaderView induvidual background colors:
@Andy314 said in QHeaderView induvidual background colors:
I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.
You can re-implement
paintSection
, add your things you want to change and then call the base implementationQHeaderView::paintSection
to preserve the default look where you do not make changes.This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.
-
@Andy314 said in QHeaderView induvidual background colors:
This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.
These two are your friends :)
Save the current
painter
, call the base impl., restore the painter and paint your stuff on top of it -
@Pl45m4 said in QHeaderView induvidual background colors:
@Andy314 said in QHeaderView induvidual background colors:
This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.
These two are your friends :)
Save the current
painter
, call the base impl., restore the painter and paint your stuff on top of itOk, this should work, for add additdional things over the standard draw - for background color not.
Mybe I should take the whole original code an name the class to a new name and break the private members.A problem in this context while create a custom QHeaderView I found. I made my own QTableView with a custom QHeaderView.
class AdvHeaderView : public QHeaderView; AdvTblView::AdvTblView(QWidget *parent): QTableView(parent) { HeaderView= new AdvHeaderView(Qt::Horizontal, this); setHorizontalHeader(HeaderView); QObject::connect(HeaderView, &QHeaderView::sectionClicked, this, &AdvTblView::headerClicked);
The sectionClicked event does not fire for the custom HeaderView. For the standard HeaderView all works.
-
@Andy314 said in QHeaderView induvidual background colors:
QObject::connect(HeaderView,
&QHeaderView::sectionClicked,
this,
&AdvTblView::headerClicked);Try
&AdvHeaderView::sectionClicked
notQHeaderView
since yourHeaderView
obj is from typeAdvHeaderView
. -
@Pl45m4 said in QHeaderView induvidual background colors:
@Andy314 said in QHeaderView induvidual background colors:
QObject::connect(HeaderView,
&QHeaderView::sectionClicked,
this,
&AdvTblView::headerClicked);Try
&AdvHeaderView::sectionClicked
notQHeaderView
since yourHeaderView
obj is from typeAdvHeaderView
.No, nothing works.
I have moved the code outside of the constructor for simplification. All 3 methodes gave no successd.QObject::connect( ui->TV->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(headerClicked(int))); QObject::connect( ui->TV->horizontalHeader(), &QHeaderView::sectionClicked, this, &VoyList_Frame::headerClicked); QObject::connect( ui->TV->horizontalHeader(), &AdvHeaderView::sectionClicked, this, &VoyList_Frame::headerClicked);
-
class AdvHeaderView : public QHeaderView;
You are finding that your derived
AdvHeaderView
is not producing any signals, is that right? Did you putQ_OBJECT
macro in your declaration/definition ofAdvHeaderView
? Not sure, that may be required here? -
@JonB said in QHeaderView induvidual background colors:
class AdvHeaderView : public QHeaderView;
You are finding that your derived
AdvHeaderView
is not producing any signals, is that right? Did you putQ_OBJECT
macro in your declaration/definition ofAdvHeaderView
? Not sure, that may be required here?I seen that it produces no signals. Unfortunately Q_OBJECT wasn't the problem
class AdvHeaderView : public QHeaderView { Q_OBJECT public: AdvHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) {} protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override; }
-
- Temporarily replace your
AdvHeaderView
with a plainQHeaderView
. Does the signal work? - Is it just the
sectionClicked()
signal which does not work or all signals do not work? - Make sure your
headerClicked()
has aqDebug()
so we are sure it not getting called and/or try a lambda with justqDebug()
in it as the slot.
- Temporarily replace your
-
@JonB said in QHeaderView induvidual background colors:
- Temporarily replace your
AdvHeaderView
with a plainQHeaderView
. Does the signal work? - Is it just the
sectionClicked()
signal which does not work or all signals do not work? - Make sure your
headerClicked()
has aqDebug()
so we are sure it not getting called and/or try a lambda with justqDebug()
in it as the slot.
If I reset the integrated HeaderView all works.
I had integrated debug messages.Signal SectionResized and GeomtryChaged work always. All Mouse Enter/Click/DoubleClick etc. dont work.
- Temporarily replace your
-
@Andy314
On yourAdvHeaderView
instance what does bool QHeaderView::sectionsClickable() const return? Do you need to call void QHeaderView::setSectionsClickable(bool clickable) withtrue
? -
@JonB said in QHeaderView induvidual background colors:
@Andy314
On yourAdvHeaderView
instance what does bool QHeaderView::sectionsClickable() const return? Do you need to call void QHeaderView::setSectionsClickable(bool clickable) withtrue
?Yeh, that was the problem. Thank you for the tip.
-
@JonB said in QHeaderView induvidual background colors:
as a base QHeaderView does.
Are you sure about that? Haven't checked the code but this from the documentation
Returns true if the header is clickable; otherwise returns false. A clickable header could be set up to allow the user to change the representation of the data in the view related to the header.
sounds like it's not the default in
QHeaderView
-
Are you sure about that?
No, I am not sure. I am relying on the OP's statement that with a non-subclassed
QHeaderView
in hisQTableView
clicking works (by default) while as soon as he sub-classes to shownAdvHeaderView
it does not work. And wondering how that would be. -
@JonB said in QHeaderView induvidual background colors:
And wondering how that would be.
It was enabled in the ui but since the header gets replaced in c++ it was overridden.
-
@Christian-Ehrlicher said in QHeaderView induvidual background colors:
It was enabled in the ui but since the header gets replaced in c++ it was overridden.
Ah, no wonder, at least that makes sense!
-
@JonB said in QHeaderView induvidual background colors:
@Andy314
What I do not know is why/how a derivedQHeaderView
does not get this set as a baseQHeaderView
does. Perhaps a Qt/C++ expert would care to enlighten us?That is exact the problem I struggled.
From the logic it can only be that the QTableView itself configures its QHeaderView correct.
Indeed I found the code for it:void QTableViewPrivate::init() { Q_Q(QTableView); q->setEditTriggers(editTriggers|QAbstractItemView::AnyKeyPressed); QHeaderView *vertical = new QHeaderView(Qt::Vertical, q); vertical->setSectionsClickable(true); vertical->setHighlightSections(true); q->setVerticalHeader(vertical); QHeaderView *horizontal = new QHeaderView(Qt::Horizontal, q); horizontal->setSectionsClickable(true); // Here is the code **************************** horizontal->setHighlightSections(true); q->setHorizontalHeader(horizontal); tabKeyNavigation = true; #if QT_CONFIG(abstractbutton) cornerWidget = new QTableCornerButton(q); cornerWidget->setFocusPolicy(Qt::NoFocus); QObject::connect(cornerWidget, SIGNAL(clicked()), q, SLOT(selectAll())); #endif }
-