recently, I try to implement checkbox on the Header of QTableWidget.
finally, I made checkbox only in first row and try to change row of checkbox but failed.
here is my code :
m_tableWidget = new TableWidget(ui.frame, this);
m_pAnimation = new QPropertyAnimation(ui.frame_2, "geometry");
QSizePolicy policy = m_tableWidget->sizePolicy();
policy.setVerticalPolicy(QSizePolicy::Fixed);
m_tableWidget->setSizePolicy(policy);
m_tableWidget->setColumnCount(5);
pCheckBoxHeaderApplication = new CheckBoxHeader(Qt::Horizontal, m_tableWidget);
m_tableWidget->setHorizontalHeader(pCheckBoxHeaderApplication);
m_tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("X"));
m_tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Y"));
m_tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Z"));
m_tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("Velocity"));
m_tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("Use"));
m_tableWidget->setItemDelegateForColumn(4, new CheckBoxDelegate(m_tableWidget));
m_tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
m_tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
m_tableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
m_tableWidget->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
m_tableWidget->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
m_tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
m_tableWidget->verticalHeader()->setDefaultSectionSize(30);
I want to make this :
and I referenced this code from Checkbox in a header cell in QTableView
I fixed problem like this
void CheckBoxHeader::paintSection(QPainter* painter, const QRect& rect, int nLogicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, nLogicalIndex);
painter->restore();
if (nLogicalIndex == m_nIndex)
{
QStyleOptionButton option;
option.rect = QRect(m_nCurruentIndexPosX, 3, 20, 20);
option.state = QStyle::State_Enabled | QStyle::State_Active;
if (m_bIsChecked)
option.state |= QStyle::State_On;
else
option.state |= QStyle::State_Off;
option.state |= QStyle::State_Off;
style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.