Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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++ QHeaderView with checkbox-如何区分右键单击标题复选框与单击单元格中的任意位置_C++_Qt5_Qtableview_Model View_Qheaderview - Fatal编程技术网

C++ QHeaderView with checkbox-如何区分右键单击标题复选框与单击单元格中的任意位置

C++ QHeaderView with checkbox-如何区分右键单击标题复选框与单击单元格中的任意位置,c++,qt5,qtableview,model-view,qheaderview,C++,Qt5,Qtableview,Model View,Qheaderview,我在继承自QHeaderView的类中定义了一个自定义头QProduitCartoHeaderView 在我的标题中,我有一个复选框,用于选中/取消选中同一列中的所有复选框 对于所有数据行 当前,当我单击第一列(带有复选框的列)标题单元格中的任意位置时, 它选中标题复选框 我想做的是,仅在我单击该复选框时选中该复选框,并且如果我单击外部(,但仍在单元格内),则选中该复选框对第一列进行排序 我曾尝试调整复选框矩形的大小(如下面代码段中的QProduitCartoHeaderView::paint

我在继承自
QHeaderView
的类中定义了一个自定义头
QProduitCartoHeaderView

在我的标题中,我有一个复选框,用于选中/取消选中同一列中的所有复选框 对于所有数据行

当前,当我单击第一列(带有复选框的列)标题单元格中的任意位置时, 它选中标题复选框

我想做的是,仅在我单击该复选框时选中该复选框,并且如果我单击外部(,但仍在单元格内),则选中该复选框对第一列进行排序

我曾尝试调整复选框矩形的大小(如下面代码段中的
QProduitCartoHeaderView::paintSection()
),但当我单击单元格中的任何位置时(不在复选框上),我仍然会选中它

注意:我已经设法使代码进行排序和检查。我不能做的是检查 我是单击复选框还是在单元格内的复选框之外

以下是我的自定义标题的一个片段:

  • QProduitCartoHeaderView.h
\ifndef qproduitecartoheaderview\u H
#定义QPRODUITCARTOHEADERVIEW\u H
#包括
#包括
#包括
#包括
QProduitCartoHeaderView类:公共QHeaderView
{
Q_对象
公众:
QProduitCartoHeaderView(QWidget*parent=0);
~QProduitCartoHeaderView();
单击的_部分无效(int logicalIndex);
bool所有选中的列(int logicalIndex=0);
bool所有第一列未选中(int logicalIndex=0);
无效设置(布尔值);
无效setPasTousLesMeme(布尔值);
受保护的:
虚拟虚空油漆部分(QPainter*油漆工、常量QRect&oRect、int index)常量;
虚空mouseReleaseEvent(QMouseEvent*e);
私人:
bool-isOn=false,pasTousLesMeme=false;
信号:
void dataModifiedSig();
};
#恩迪夫
  • QProduitCartoHeaderView.cpp(仅用于避免长文章的有用代码)
\ifndef qproduitecartoheaderview\u H
#包括“QProduitCartoHeaderView.h”
#恩迪夫
#ifndef QPRODUITCARTODATAMODEL\u H
#包括“QProduitCartoDataModel.h”
#恩迪夫
QProduitCartoHeaderView::QProduitCartoHeaderView(QWidget*父项):QHeaderView(Qt::水平,父项)
{
}
QProduitCartoHeaderView::~QProduitCartoHeaderView()
{
}
void QProduitCartoHeaderView::setIsOn(布尔值)
{
isOn=val;
}
void QProduitCartoHeaderView::setPasTousLesMeme(布尔值)
{
//设置所有复选框是否具有不同的值
pasTousLesMeme=val;
}
void QProduitCartoHeaderView::paintSection(QPainter*poPainter,const QRect&oRect,int index)const
{
poPainter->save();
QHeaderView::油漆部分(油漆工、oRect、索引);
poPainter->restore();
QSTYLEOPTION按钮选项;
QRect checkbox_rect=style()->subElementRect(QStyle::SE_CheckBoxIndicator,&option);
复选框_rect.moveCenter(oRect.center());
//倒科隆1号
如果(索引==0)
{
qDebug()更新();
//财务报告更新
int nbRow=this->model()->rowCount();
//在我看来,这是一种虚假,而不是一种真实
int nbTrue=0;
如果(所有列都已选中(logicalIndex))
{
对于(int r=0;rmodel()->index(r,logicalIndex);
_Model->setData(index,false,Qt::CheckStateRole);
}
}
其他的
{
//论抹杀堕胎
对于(int r=0;rmodel()->index(r,logicalIndex);
bool checked=this->model();
如果(选中)
_Model->setData(index,false,Qt::CheckStateRole);
}
//套间,在fait le flip/flop上
对于(int r=0;rmodel()->index(r,logicalIndex);
//更新每行实际数据
_Model->setData(index,true,Qt::CheckStateRole);
}
}
}
发出dataModifiedSig();
}

您可以跟踪鼠标并重新执行
MousePresseEvent(QMouseEvent*event)
mouseReleaseEvent(QMouseEvent*event)
以确定单击节的位置。我在下面创建了一个示例:

myheaderview.h

#ifndef MYHEADERVIEW_H
#define MYHEADERVIEW_H

#include <QHeaderView>

class MyHeaderView : public QHeaderView
{
public:
    MyHeaderView(QWidget *parent = nullptr);

    QRect visualRectOfColumn(int column) const;

protected:
    virtual void paintSection(QPainter *painter, const QRect &rect, int index) const;
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);

private:
    int press_column_{-1};
    bool checkbox_pressed_{false};
};

#endif // MYHEADERVIEW_H
\ifndef MYHEADERVIEW\u H
#定义MYHEADERVIEW\u H
#包括
MyHeaderView类:公共QHeaderView
{
公众:
MyHeaderView(QWidget*parent=nullptr);
QRect visualRectOfColumn(int column)常量;
受保护的:
虚拟虚空绘制部分(QPainter*painter,const QRect&rect,int index)const;
虚拟无效MousePresseEvent(QMouseEvent*事件);
虚拟无效mouseReleaseEvent(QMouseEvent*事件);
私人:
int press_列{-1};
bool复选框u按下{false};
};
#endif//MYHEADERVIEW\u H
myheaderview.cpp

#include "myheaderview.h"

#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

MyHeaderView::MyHeaderView(QWidget *parent) : QHeaderView(Qt::Horizontal, parent)
{
    this->setMouseTracking(true);
}

QRect MyHeaderView::visualRectOfColumn(int column) const
{
    int x = sectionViewportPosition(column);
    int y = 0;
    int h = this->height();
    int w = this->sectionSize(column);
    return QRect(x, y, w, h);
}

void MyHeaderView::paintSection(QPainter *painter, const QRect &rect, int index) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, index);
    painter->restore();

    if (index == 0)
    {
        QStyleOptionButton option;
        QRect checkbox_rect = style()->subElementRect(QStyle::SE_CheckBoxIndicator, &option);
        checkbox_rect.moveCenter(rect.center());

        option.state = QStyle::State_Enabled | QStyle::State_Active;
        option.rect = checkbox_rect;
        QHeaderView::paintSection(painter, checkbox_rect, index);
        this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
    }
}

void MyHeaderView::mousePressEvent(QMouseEvent *event)
{
    press_column_ = this->visualIndexAt(event->pos().x());
    if (press_column_ == -1)
    {
        checkbox_pressed_= false;
        return QHeaderView::mousePressEvent(event);;
    }

    QStyleOptionButton option;
    QRect checkbox_rect = style()->subElementRect(QStyle::SE_CheckBoxIndicator, &option);
    checkbox_rect.moveCenter(this->visualRectOfColumn(press_column_).center());
    checkbox_pressed_= checkbox_rect.contains(event->pos());

    QHeaderView::mousePressEvent(event);
}

void MyHeaderView::mouseReleaseEvent(QMouseEvent *event)
{
    int release_column = this->visualIndexAt(event->pos().x());
    if (release_column != -1 && press_column_ == release_column)
    {
        if (release_column != 0)
            qDebug() << "sort";
        else
        {
            QStyleOptionButton option;
            QRect checkbox_rect = style()->subElementRect(QStyle::SE_CheckBoxIndicator, &option);
            checkbox_rect.moveCenter(this->visualRectOfColumn(release_column).center());
            if (checkbox_pressed_&& checkbox_rect.contains(event->pos()))
                qDebug() << "checkbox";
            else if (!checkbox_pressed_&& !checkbox_rect.contains(event->pos()))
                qDebug() << "sort";
        }
    }

    press_column_ = -1;
    checkbox_pressed_= false;
    QHeaderView::mouseReleaseEvent(event);
}
#包括“myheaderview.h”
#包括
#包括
#包括
MyHeaderView::MyHeaderView(QWidget*父项):QHeaderView(Qt::水平,父项)
{
此->设置鼠标追踪(true);
}
QRect MyHeaderView::visualRectOfColumn(int列)常量
{
int x=截面视口位置(列);
int y=0;
int h=此->高度();
int w=此->截面尺寸(列);
返回QRect(x,y,w,h);
}
void MyHeaderView::paintSection(QPainter*painter,const QRect&rect,int index)const
{
画师->保存();
QHeaderView::paintSection(painter、rect、index);
画师->还原();
如果(索引==0)
{
QSTYLEOPTION按钮选项;
QRect checkbox_rect=style()->subElementRect(QStyle::SE_CheckBoxIndicator,&option);
复选框_rect.moveCenter(rect.center());
option.state=QStyle::state_Enabled | QStyle::state_Active;
option.rect=复选框\u rect;
QHeaderView::油漆部分(油漆工)