Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QtableWidget标题上的复选框_C++_Qt_Qt5.6 - Fatal编程技术网

C++ QtableWidget标题上的复选框

C++ QtableWidget标题上的复选框,c++,qt,qt5.6,C++,Qt,Qt5.6,如何在QTableWidget标题上设置复选框。如何在QHeaderView中添加全选复选框。。 它不显示复选框 QTableWidget* table = new QTableWidget(); QTableWidgetItem *pItem = new QTableWidgetItem("All"); pItem->setCheckState(Qt::Unchecked); table->setHorizontalHeaderItem(0, pItem); 它说没有捷径

如何在QTableWidget标题上设置复选框。如何在QHeaderView中添加全选复选框。。 它不显示复选框

 QTableWidget* table = new QTableWidget();
 QTableWidgetItem *pItem = new QTableWidgetItem("All");
 pItem->setCheckState(Qt::Unchecked);
 table->setHorizontalHeaderItem(0, pItem);
它说没有捷径,你必须自己将headerView子类化

以下是wiki答案的摘要:

目前没有API可以在标题中插入小部件,但是您可以自己绘制复选框以将其插入标题中

您可以做的是将QHeaderView子类化,重新实现,然后在您想要有此复选框的部分中使用PE_IndicatorCheckBox调用

您还需要重新实现以检测何时单击复选框,以便绘制选中和未选中状态

下面的示例说明了如何做到这一点:

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

代替上述解决方案,您可以简单地将按钮置于“全选”复选框的位置,并为“全选”指定一个名称“按钮”


因此,如果您按“全选”按钮,则它称为“全选”按钮,并在您按“全选”按钮时转到。

从表视图中调用标题内的复选框没有其他方法?我们必须自行绘制复选框以进行设计,并且必须从表视图中的标题调用。。谢谢,我将尝试此方法来实现复选框。