永发信息网

Qt treewidget setheaderitem为什么只显示文字

答案:2  悬赏:0  手机版
解决时间 2021-03-20 19:57
我的代码:

group0 = new QTreeWidgetItem();

group0->setText(0, codec1->toUnicode("全部"));
group0->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
group0->setCheckState(0, Qt::Unchecked);

treeWidget->setHeaderItem(group0);

结果:



最佳答案
继承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
全部回答
可以私聊我~
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
魔兽世界影月谷乌鲁洛斯的爪牙能捕吗???
明太鱼有什么营养
好风景家居渭南专卖店地址在什么地方,想过去
我电脑开机的时候 开到一半会突然蓝屏一下只
黎明杀机存档丢失怎么解决cmd不行
老广场地址在什么地方,想过去办事
我第一次写小说,刚刚上架,但是根本没有人看
建行房贷申请条件
ThinkPad E545 CPU和内存问题 希望高手解答。
大红沙滩地址在哪,我要去那里办事
简单的问题
欲钱买忠心耿耿的动物解什么动物?
电线走墙,横着开槽没问题么
找规律,填数字,4,5,9,14,23,(),(
慢吞吞的近义词
推荐资讯
剑侠情缘2武少怎么PK最强!
这个怎么修?我体重是60公斤,电子秤显示9000
电脑的图标文字左边都有像彗星尾巴的阴影,请
老公和别人打架。 那人用啤酒瓶打老公。别人
棒女郎真的管那么多妇科病吗
镀金是真金吗
EMS能寄移动硬盘到台湾吗?
西游记中三打白骨精的主要内容50字
买车贷款,我银行卡近六个月收入超4000,可是每
紫色马丁靴配什么颜色衣服
单选题质量相等的石墨和金刚石所含质子数()
怎样查公积金个人账户
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?