QTableWidget只能对表格中的单元格设置复选框,而且只能进行比较有限的控制,如果需要设置图标,显示居中等等,可能需要自定义Item或可以利用setCellWidget将单元格的控件设置为自定义控件,而表头如果是使用自带的表头则无法通过设置显示出复选框,必须进行自定义表头,自定义表头显示复选框有两种方法,具体如下。
1.采用绘制图片的方式在表头绘制复选框
核心代码如下
class CheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
public:
CheckBoxHeaderView(int checkColumnIndex, Qt::Orientation orientation, QWidget* parent = 0) : QHeaderView(orientation, parent)
{
m_checkColIndex = checkColumnIndex;
m_iChecked = Qt::UnChecked;
}
void setCheckState(int state)
{
m_iChecked = state;
this->updateSection(m_checkColIndex);
}
protected:
void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if(logicalIndex == m_checkColIndex)
{
int width = 10;
for(int i = 0; i < logicalIndex; i++)
{
width += sectionSize(i);
}
Qpixmap pix;
if(Qt::Checked == m_iChecked)
{
///<pix加载选中时的图片
}
else if(Qt::Unchecked == m_iChecked)
{
///<pix加载未选中时的图片
}
else if(Qt::PartiallyChecked == m_iChecked)
{
///<pix加载部分选中时的图片
}
this->style()->drawItemPixmap(painter, rect, Qt::AlignHCenter | Qt::AlignVCenter, pix);
}
}
void mousePressEvent(QMouseEvent* event)
{
if(visualIndexAt(event->pos().x()) == m_checkColIndex)
{
if(Qt::PartiallyChecked == m_iChecked || Qt::Unchecked == m_iChecked)
{
m_iChecked = Qt::Checked;
}
else if(Qt::Checked == m_iChecked)
{
m_iChecked = Qt::Unchecked;
}
this->updateSection(m_iChecked);
emit checkStatusChange(m_iChecked );
}
QHeaderView::mousePressEvent(event);
}
signals:
void checkStatusChange(int);
protected:
int m_checkColIndex;
int m_iChecked;
};
2.采用绘制控件的方式显示复选框文章来源:https://www.toymoban.com/news/detail-504628.html
核心代码如下:文章来源地址https://www.toymoban.com/news/detail-504628.html
class CheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
public:
CheckBoxHeaderView(int checkColumnIndex, QPoint topLeft, QSize size, Qt::Orientation orientation, QWidget* parent = 0) : QHeaderView(orientation, parent)
{
m_checkColIndex = checkColumnIndex;
m_topLeft = topLeft;
m_checkSize = size;
m_iChecked = Qt::UnChecked;
m_pCheckBox = new QCheckBox();
}
void setCheckState(int state)
{
m_iChecked = state;
this->updateSection(m_checkColIndex);
}
protected:
void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if(logicalIndex == m_checkColIndex)
{
QStyleOptionButton option;
int width = 10;
for(int i = 0; i < logicalIndex; i++)
{
width += sectionSize(i);
}
option.rect = QRect(m_topLeft.x(), m_topLeft.y(), m_checkSize.width(), m_checkSize.height());
if(Qt::Checked == m_iChecked)
{
option.state = QStyle::State_On;
}
else if(Qt::Unchecked == m_iChecked)
{
option.state = QStyle::State_Off;
}
else if(Qt::PartiallyChecked == m_iChecked)
{
option.state = QStyle::State_NoChange;
}
if(nullptr != m_pCheckBox)
{
m_pcheckBox->setVisible(false);
m_pCheckBox->setCheckState((Qt::CheckState)m_iChecked);
this->style()->drawControl(Qstyle::CE_CheckBox, &option, painter, m_pCheckBox);
}
}
}
void mousePressEvent(QMouseEvent* event)
{
if(visualIndexAt(event->pos().x()) == m_checkColIndex)
{
if(Qt::PartiallyChecked == m_iChecked || Qt::Unchecked == m_iChecked)
{
m_iChecked = Qt::Checked;
}
else if(Qt::Checked == m_iChecked)
{
m_iChecked = Qt::Unchecked;
}
this->updateSection(m_iChecked);
emit checkStatusChange(m_iChecked );
}
QHeaderView::mousePressEvent(event);
}
signals:
void checkStatusChange(int);
protected:
int m_checkColIndex;
int m_iChecked;
QPoint m_topLeft;
QSize m_checkSize;
QCheckBox* m_pCheckBox = nullptr;
};
到了这里,关于Qt的QTableWidget如何在表头增加复选框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!