当前位置:网站首页>QTableWidget / QTableView实用技巧

QTableWidget / QTableView实用技巧

2022-06-10 17:26:00 rainbow_lucky0106

表头增加复选框QCheckBox(单列)

  1. 重写QHeaderView
DCheckBoxHeaderView::DCheckBoxHeaderView(Qt::Orientation orientation, QWidget *parent)
    :QHeaderView(orientation, parent)
{
    
    m_bPressed = false;  // 鼠标按下
    m_bChecked = false;  // 是否选中
    m_bTristate = false;  // 三状态框
    m_bNoChange = false;
	m_bMoving = false;
    m_iCheckboxColumn = 0;  // 待添加复选框的列
    m_sizeIconSize = QSize(20, 20);
    m_strText = "";
    setSectionsClickable(true);  // 响应表头事件
}
void DCheckBoxHeaderView::SetCheckBoxColumn(int iCheckBoxColumn)
{
    
    m_iCheckboxColumn = iCheckBoxColumn;
}

void DCheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();

    // 要画复选框的列
    if (logicalIndex == m_iCheckboxColumn)
    {
    
        QStyleOptionButton option;
        option.initFrom(this);
        option.text = m_strText;

        if (m_bChecked)
            option.state |= QStyle::State_Sunken;

        if (m_bTristate && m_bNoChange)
            option.state |= QStyle::State_NoChange;
        else
            option.state |= m_bChecked ? QStyle::State_On : QStyle::State_Off;
        // if (testAttribute(Qt::WA_Hover) && underMouse()) {
    
        // if (m_bMoving)
        // option.state |= QStyle::State_MouseOver;
        // else
        // option.state &= ~QStyle::State_MouseOver;
        // }

        option.iconSize = m_sizeIconSize;
        option.rect = rect;
        // style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, &checkBox);
        // style()->drawItemPixmap(painter, rect, Qt::AlignCenter, QPixmap(":/images/checkBoxChecked"));
        style()->drawControl(QStyle::CE_CheckBox, &option, painter, this);
    }
}

// 鼠标按下表头
void DCheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
{
    
    int nColumn = logicalIndexAt(event->pos());
    if ((event->buttons() & Qt::LeftButton) && (nColumn == m_iCheckboxColumn))
        m_bPressed = true;
    else
        QHeaderView::mousePressEvent(event);
}

// 鼠标从表头释放,发送信号,更新model数据
void DCheckBoxHeaderView::mouseReleaseEvent(QMouseEvent *event)
{
    
    if (m_bPressed)
    {
    
        if (m_bTristate && m_bNoChange)
        {
    
            m_bChecked = true;
            m_bNoChange = false;
        }
        else
        {
    
            m_bChecked = !m_bChecked;
        }
        update();
        Qt::CheckState state = m_bChecked ? Qt::Checked : Qt::Unchecked;
        emit stateChanged(state);
    }
    else
    {
    
        QHeaderView::mouseReleaseEvent(event);
    }

    m_bPressed = false;
}

// 槽函数,用于更新复选框状态
void DCheckBoxHeaderView::onStateChanged(int state)
{
    
    if (state == Qt::PartiallyChecked) {
    
        m_bTristate = true;
        m_bNoChange = true;
    } else {
    
        m_bNoChange = false;
    }
    m_bChecked = (state != Qt::Unchecked);
    update();
}

// 鼠标滑过、离开,更新复选框状态
bool DCheckBoxHeaderView::event(QEvent *event)
{
    
    if (event->type() == QEvent::Enter || event->type() == QEvent::Leave)
    {
    
        QMouseEvent *pEvent = static_cast<QMouseEvent *>(event);
        int nColumn = logicalIndexAt(pEvent->x());
        if (nColumn == m_iCheckboxColumn)
        {
    
            m_bMoving = (event->type() == QEvent::Enter);
            update();
            return true;
        }
    }
    return QHeaderView::event(event);
}
  1. 设进QTableWidget
QStringList m_Header2;
m_Header2<<QString("1")<<QString("2")<<QString("3")<<QString("4")<<QString("5");
DCheckBoxHeaderView *headerview = new DCheckBoxHeaderView();
headerview->SetCheckBoxColumn(3);  // 前四列增加复选框
this->setHorizontalHeader(headerview);
this->setHorizontalHeaderLabels(m_Header2);//添加横向表头

https://blog.csdn.net/liang19890820/article/details/50772562

https://blog.csdn.net/weixin_44429308/article/details/106093685

https://www.cnblogs.com/bclshuai/p/9800550.html

https://blog.csdn.net/u014597198/article/details/77579070

原网站

版权声明
本文为[rainbow_lucky0106]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_21980099/article/details/125170661