QtableWidget 标题上的复选框
checkbox on QtableWidget's header
如何在qtableWidget标头上设置复选框。如何在qheaderview中添加选择所有复选框。它不显示复选框..
QTableWidget* table = new QTableWidget();
QTableWidgetItem *pItem = new QTableWidgetItem("All");
pItem->setCheckState(Qt::Unchecked);
table->setHorizontalHeaderItem(0, pItem);
在这里,在qt wiki上,它说没有快捷方式,您必须自己为标头表。
这是Wiki答案的摘要:
"当前没有API可以在标题中插入小部件,但是您可以自己绘制复选框以将其插入标题。
您可以做的是子类qheaderview,重新进化paintsection(),然后在您想要拥有此复选框的部分中使用pe_indicatorCheckbox调用drawprimitive()。
。您还需要重新完成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();
}
而不是上述解决方案,您只需按下按钮以取代所有复选框,然后给出一个名称按钮以"选择全部"。
因此,如果按下所有按钮,则将其称为按钮,然后随时使用按钮(此处选择全部)。
相关文章:
- 根据变量使Qt UI中的复选框为已选中/未选中
- "动态创建的事件处理程序"复选框
- 有没有办法在取消选中复选框时隐藏按钮?
- 如何删除Qt设计器中自定义插件的QString属性的"translatable"复选框?
- 设置复选框选项C++
- 如何在所有者描述的 MFC CMenu 派生菜单中显示复选框?
- 如何在MFC对话框应用程序中默认将复选框设置为选中?
- 如何使复选框独占
- AccessObjectFromPoint 返回客户端对象而不是复选框
- Windows 32 API:2个复选框作为一个
- 如何在 QT3 中将复选框添加到 QFileDialog 窗口
- 在 QComboBox 中切换复选框
- 调整QT创建者的复选框
- 如何在C 上创建复选框过滤器
- 如何设置按钮的背景颜色,包括复选框按钮
- 使用 ffh.getFieldType() 检查复选框 == "/Btn" 与 qpdf lib
- 如何在多行文本C++的左上角显示复选框
- Win32如何制作多个复选框并以不同的方式管理它们
- QtableWidget 标题上的复选框
- Qt c++ QToolBox:如何添加复选框/按钮标题