This thread has been *very* useful, so I will add some points.
First, you can indeed use a setStyleSheet call in your table widget class to change the corner button border and background as follows:
setStyleSheet("QTableCornerButton::section {border: 2px outset red; background: green;}");
setStyleSheet("QTableCornerButton::section {border: 2px outset red; background: green;}");
To copy to clipboard, switch view to plain text mode
I did not need this functionality for what I wanted to do, but it is still a useful trick to know.
What I wanted to do was place an icon in the corner button to indicate that there is indeed a functional button at that location (many of my users did not know that clicking the corner will select all of the table cells). The 29th March 2007 posted by jpn was *extremely* helpful with a slight change. In jpn's example, he sets the button text in the TableWidget constructor like this:
btn->setText("Text");
opt.text = btn->text();
btn->setText("Text");
opt.text = btn->text();
To copy to clipboard, switch view to plain text mode
I set the button icon instead of the text:
QIcon myIcon
= getMyIcon
();
// this function returns a QIcon btn->setIcon(myIcon);
opt.icon = btn->icon();
QIcon myIcon = getMyIcon(); // this function returns a QIcon
btn->setIcon(myIcon);
opt.icon = btn->icon();
To copy to clipboard, switch view to plain text mode
The "opt.text = btn->text();" line in the eventFilter function also has to be changed to "opt.icon = btn->icon();"
I also changed the functionality of the corner button. Instead of simply selecting all of the cells, I altered the corner button to toggle between selecting all the cells and clearing the selection.
Here is the relevant code from my QTableWidget constructor:
// Take the click signal off of the corner widget and replace it with our own
// This enables toggling the selection when the corner button is clicked.
// This is more ergonomic than the default behavior of only selecting all of the cells.
m_toggleCornerSelection = false;
if (btn != NULL) {
btn->disconnect(SIGNAL(clicked()));
connect(btn, SIGNAL(clicked()), this, SLOT(handleCornerButtonClicked()));
MyIcons *iconList = MyIcons::getIcons(); // our class that handles all of our icons
QIcon selectIcon
= iconList
->getIconSet
(MyIcons
::SELECT_ALL);
btn->setIcon(selectIcon);
// btn->setText("text");
btn->setToolTip("Toggle selecting all table cells");
btn->installEventFilter(this);
// adjust the width of the vertical header to match the preferred corner button width
// (unfortunately QAbstractButton doesn't implement any size hinting functionality)
// opt.text = btn->text();
opt.icon = btn->icon();
QSize s
= (btn
->style
()->sizeFromContents
(QStyle::CT_HeaderSection,
&opt,
QSize(), btn
).
if (s.isValid()) {
verticalHeader()->setMinimumWidth(s.width());
}
}
// Take the click signal off of the corner widget and replace it with our own
// This enables toggling the selection when the corner button is clicked.
// This is more ergonomic than the default behavior of only selecting all of the cells.
m_toggleCornerSelection = false;
QAbstractButton *btn = findChild<QAbstractButton *>();
if (btn != NULL) {
btn->disconnect(SIGNAL(clicked()));
connect(btn, SIGNAL(clicked()), this, SLOT(handleCornerButtonClicked()));
MyIcons *iconList = MyIcons::getIcons(); // our class that handles all of our icons
QIcon selectIcon = iconList->getIconSet(MyIcons::SELECT_ALL);
btn->setIcon(selectIcon);
// btn->setText("text");
btn->setToolTip("Toggle selecting all table cells");
btn->installEventFilter(this);
// adjust the width of the vertical header to match the preferred corner button width
// (unfortunately QAbstractButton doesn't implement any size hinting functionality)
QStyleOptionHeader opt;
// opt.text = btn->text();
opt.icon = btn->icon();
QSize s = (btn->style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), btn).
expandedTo(QApplication::globalStrut()));
if (s.isValid()) {
verticalHeader()->setMinimumWidth(s.width());
}
}
To copy to clipboard, switch view to plain text mode
I then implemented the following code:
//-----------------------------------------------------------------------------
// Event filter used to draw an icon in the top-left corner of the table widget
//-----------------------------------------------------------------------------
{
if (e
->type
() == QEvent::Paint) { if (btn) {
// paint by hand (borrowed from QTableCornerButton)
opt.init(btn);
if (btn->isEnabled())
styleState |
= QStyle::State_Enabled;
if (btn->isActiveWindow())
styleState |
= QStyle::State_Active;
if (btn->isDown())
styleState |
= QStyle::State_Sunken;
opt.state = styleState;
opt.rect = btn->rect();
// opt.text = btn->text(); // this line is the only difference to QTableCornerButton
opt.icon = btn->icon(); // this line is the only difference to QTableCornerButton
painter.
drawControl(QStyle::CE_Header, opt
);
return true; // eat event
}
}
return false;
}
...
//-----------------------------------------------------------------------------
// Handle clicking the upper-left corner button. This action toggles the selection of all of the cells
// off and on.
//-----------------------------------------------------------------------------
void MySpreadsheet::handleCornerButtonClicked()
{
if (m_toggleCornerSelection) {
clearSelection();
} else {
selectAll();
}
m_toggleCornerSelection = !m_toggleCornerSelection;
}
//-----------------------------------------------------------------------------
// Event filter used to draw an icon in the top-left corner of the table widget
//-----------------------------------------------------------------------------
bool MySpreadsheet::eventFilter(QObject* o, QEvent* e)
{
if (e->type() == QEvent::Paint) {
QAbstractButton* btn = qobject_cast<QAbstractButton*>(o);
if (btn) {
// paint by hand (borrowed from QTableCornerButton)
QStyleOptionHeader opt;
opt.init(btn);
QStyle::State styleState = QStyle::State_None;
if (btn->isEnabled())
styleState |= QStyle::State_Enabled;
if (btn->isActiveWindow())
styleState |= QStyle::State_Active;
if (btn->isDown())
styleState |= QStyle::State_Sunken;
opt.state = styleState;
opt.rect = btn->rect();
// opt.text = btn->text(); // this line is the only difference to QTableCornerButton
opt.icon = btn->icon(); // this line is the only difference to QTableCornerButton
opt.position = QStyleOptionHeader::OnlyOneSection;
QStylePainter painter(btn);
painter.drawControl(QStyle::CE_Header, opt);
return true; // eat event
}
}
return false;
}
...
//-----------------------------------------------------------------------------
// Handle clicking the upper-left corner button. This action toggles the selection of all of the cells
// off and on.
//-----------------------------------------------------------------------------
void MySpreadsheet::handleCornerButtonClicked()
{
if (m_toggleCornerSelection) {
clearSelection();
} else {
selectAll();
}
m_toggleCornerSelection = !m_toggleCornerSelection;
}
To copy to clipboard, switch view to plain text mode
I hope that is helpful to those who want similar functionality.
Bookmarks