简体   繁体   English

QtableWidget标题上的复选框

[英]checkbox on QtableWidget's header

How to set checkbox on QTableWidget Header. 如何在QTableWidget标题上设置复选框。 How To Add Select All Checkbox in QHeaderView.. It does not displays a checkbox.. 如何在QHeaderView中添加选择全部复选框..它不显示复选框..

 QTableWidget* table = new QTableWidget();
 QTableWidgetItem *pItem = new QTableWidgetItem("All");
 pItem->setCheckState(Qt::Unchecked);
 table->setHorizontalHeaderItem(0, pItem);

Here, at Qt Wiki, it says there is no shortcut for it and you have to subclass headerView yourself. 在这里,在Qt Wiki,它说没有它的快捷方式,你必须自己子类化headerView。

Here is a summary of that wiki answer: 以下是该wiki答案的摘要:

"Currently there is no API to insert widgets in the header, but you can paint the checkbox yourself in order to insert it into the header. “目前没有API在标题中插入小部件,但您可以自己绘制复选框以将其插入标题中。

What you could do is to subclass QHeaderView, reimplement paintSection() and then call drawPrimitive() with PE_IndicatorCheckBox in the section where you want to have this checkbox. 您可以做的是继承QHeaderView,重新实现paintSection() ,然后在要使用此复选框的部分中使用PE_IndicatorCheckBox调用drawPrimitive()

You would also need to reimplement the mousePressEvent() to detect when the checkbox is clicked, in order to paint the checked and unchecked states. 您还需要重新实现mousePressEvent()以检测何时单击该复选框,以便绘制已选中和未选中状态。

The example below illustrates how this can be done: 以下示例说明了如何完成此操作:

#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();
}

Instead of above solution, You can simply put Push button in place of select all check box and give a name push button to "select all". 您可以简单地将“按钮”替换为“全选”复选框,并使用名称按钮来“全选”,而不是上述解决方案。

So If you press select all button then it called push button and go to whenever you go with push button(Here select all). 因此,如果您按下全部选择按钮,则会调出按钮,然后按下按钮(此处全选)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM