Here, at Qt Wiki,它说是没有捷径的它,你必须继承headerView自己。
这里是维基答案的摘要:
“目前还没有API的报头中插入窗口小部件,但你可以自己绘制的复选框,以便将其插入到页眉
。你可以做的是细分QHeaderView,重新实现paintSection(),然后在你想拥有此复选框的部分叫drawPrimitive()与PE_IndicatorCheckBox。
您还需要重新实现mousePressEvent()检测被点击复选框时,为了为车漆新鲜和未经检查的状态。
下面的例子示出了如何可以做到这一点:
#include <QtGui>
class MyHeader : public QHeaderView
{
public:
MyHeader(Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent)
{}
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (logicalIndex == 0)
{
QStyleOptionButton option;
option.rect = QRect(10,10,10,10);
if (isOn)
option.state = QStyle::State_On;
else
option.state = QStyle::State_Off;
this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
}
}
void mousePressEvent(QMouseEvent *event)
{
if (isOn)
isOn = false;
else
isOn = true;
this->update();
QHeaderView::mousePressEvent(event);
}
private:
bool isOn;
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTableWidget table;
table.setRowCount(4);
table.setColumnCount(3);
MyHeader *myHeader = new MyHeader(Qt::Horizontal, &table);
table.setHorizontalHeader(myHeader);
table.show();
return app.exec();
}
有调用从表视图报头内的复选框没有可供选择的方法? –
我们必须画我们自己的复选框来设计,并且必须从表格视图的标题中调用..谢谢,我会尝试使用此方法来实现复选框。 –