我的代码:
group0 = new QTreeWidgetItem();
group0->setText(0, codec1->toUnicode("全部"));
group0->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
group0->setCheckState(0, Qt::Unchecked);
treeWidget->setHeaderItem(group0);
结果:
Qt treewidget setheaderitem为什么只显示文字
答案:2 悬赏:0 手机版
解决时间 2021-03-20 19:57
- 提问者网友:世勋超人
- 2021-03-20 13:48
最佳答案
- 五星知识达人网友:毛毛
- 2021-03-20 14:00
继承QHeaderView,自己实现。
#ifndef CHECKBOXHEADERVIEW_H
#define CHECKBOXHEADERVIEW_H
#include
class CCheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
public:
CCheckBoxHeaderView( int checkColumnIndex,
Qt::Orientation orientation,
QWidget * parent = 0) :
QHeaderView(orientation, parent)
{
m_checkColIdx = checkColumnIndex;
}
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (logicalIndex == m_checkColIdx)
{
QStyleOptionButton option;
int width = 10;
for (int i=0; i
width += sectionSize( i );
option.rect = QRect(width, 5, 15, 15);
if (isOn)
option.state = QStyle::State_On;
else
option.state = QStyle::State_Off;
// this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
void mousePressEvent(QMouseEvent *event)
{
if (visualIndexAt(event->pos().x()) == m_checkColIdx)
{
// setSortIndicatorShown( false );
isOn = !isOn;
this->updateSection(m_checkColIdx);
}
// else
// setSortIndicatorShown( true );
QHeaderView::mousePressEvent(event);
}
private:
bool isOn;
int m_checkColIdx;
};
#if 0
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTableWidget table;
table.setRowCount(4);
table.setColumnCount(3);
CCheckBoxHeaderView *myHeader = new CCheckBoxHeaderView(0, Qt::Horizontal, &table);
table.setHorizontalHeader(myHeader);
table.show();
return app.exec();
}
#endif
#endif
#ifndef CHECKBOXHEADERVIEW_H
#define CHECKBOXHEADERVIEW_H
#include
class CCheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
public:
CCheckBoxHeaderView( int checkColumnIndex,
Qt::Orientation orientation,
QWidget * parent = 0) :
QHeaderView(orientation, parent)
{
m_checkColIdx = checkColumnIndex;
}
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (logicalIndex == m_checkColIdx)
{
QStyleOptionButton option;
int width = 10;
for (int i=0; i
option.rect = QRect(width, 5, 15, 15);
if (isOn)
option.state = QStyle::State_On;
else
option.state = QStyle::State_Off;
// this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
void mousePressEvent(QMouseEvent *event)
{
if (visualIndexAt(event->pos().x()) == m_checkColIdx)
{
// setSortIndicatorShown( false );
isOn = !isOn;
this->updateSection(m_checkColIdx);
}
// else
// setSortIndicatorShown( true );
QHeaderView::mousePressEvent(event);
}
private:
bool isOn;
int m_checkColIdx;
};
#if 0
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTableWidget table;
table.setRowCount(4);
table.setColumnCount(3);
CCheckBoxHeaderView *myHeader = new CCheckBoxHeaderView(0, Qt::Horizontal, &table);
table.setHorizontalHeader(myHeader);
table.show();
return app.exec();
}
#endif
#endif
全部回答
- 1楼网友:一把行者刀
- 2021-03-20 14:20
可以私聊我~
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯