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
Qt tableview中的复选框和itemdelegate_Qt_Qtableview_Qitemdelegate_Qcheckbox - Fatal编程技术网

Qt tableview中的复选框和itemdelegate

Qt tableview中的复选框和itemdelegate,qt,qtableview,qitemdelegate,qcheckbox,Qt,Qtableview,Qitemdelegate,Qcheckbox,我正在执行一个从QitemDelegate继承的复选框的实现,以将其放入QTableView中 问题是,当我插入左对齐时,我需要它居中 据我所知,油漆的使用方法。我写的如下: void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { bool checkValue; QStyleOptionButton

我正在执行一个从QitemDelegate继承的复选框的实现,以将其放入QTableView中

问题是,当我插入左对齐时,我需要它居中

据我所知,油漆的使用方法。我写的如下:

void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool checkValue;

QStyleOptionButton BtnStyle;
BtnStyle.state = QStyle::State_Enabled;

if(index.model()->data(index, Qt::DisplayRole).toBool() == true)
{
BtnStyle.state |= QStyle::State_On;
checkValue = true;
}else{
BtnStyle.state |= QStyle::State_Off;
checkValue = false;
}


BtnStyle.direction = QApplication::layoutDirection();
BtnStyle.rect = option.rect;
QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
}
在居中显示时缺少什么

因此,我授权:

h

.cpp

void CheckBoxDelegate::已更改(布尔值)
{
BooleanWidget*复选框=static_cast(sender());
发出提交数据(复选框);
发出关闭编辑器(复选框);
}
QWidget*CheckBoxDelegate::createEditor(QWidget*父项,常量QStyleOptionViewItem&/*选项*/,常量QModelIndex&/*索引*/)常量
{
BooleanWidget*编辑器=新的BooleanWidget(父项);
连接(编辑器、信号(切换(bool))、此、插槽(更改(bool));
返回编辑器;
}
void CheckBoxDelegate::setEditorData(QWidget*编辑器,常量QModelIndex和索引)常量
{
int value=index.model()->data(index,Qt::DisplayRole).toInt();
BooleanWidget*复选框=static_cast(编辑器);
如果(值==1)
{
复选框->设置选中(true);
}
其他的
{
复选框->设置选中(假);
}
}
void CheckBoxDelegate::setModelData(QWidget*编辑器、QBStractItemModel*模型、常量QModelIndex和索引)常量
{
BooleanWidget*复选框=qobject_cast(编辑器);
Qt::CheckState值;
如果(复选框->isChecked())
值=Qt::已检查;
其他的
value=Qt::未选中;
模型->设置数据(索引、值、Qt::DisplayRole);
}
void CheckBoxDelegate::绘制(QPainter*painter,常量QStyleOptionViewItem&option,常量QModelIndex&index)常量
{
drawCheck(painter、option、option.rect、index.data().toBool()?Qt::Checked:Qt::Unchecked);
drawFocus(painter、option、option.rect);
}

如果您要扩展
QItemDelegate
类,它有一个函数,它将为您画一个九个居中的复选框。可以在paint()函数中使用它

编辑:

下面是一个示例,假设您有一个名为
BooleanEditor
的类,继承自:

要在进入编辑模式时保持复选框居中,可以执行以下操作:

class BooleanWidget : public QWidget
{
    Q_OBJECT
    QCheckBox * checkBox;

    public:
    BooleanWidget(QWidget * parent = 0)
    {
        checkBox = new QCheckBox(this);
        QHBoxLayout * layout = new QHBoxLayout(this);
        layout->addWidget(checkBox,0, Qt::AlignCenter);
    }

    bool isChecked(){return checkBox->isChecked();}
    void setChecked(bool value){checkBox->setChecked(value);}
};
在ItemDelegates createEditor()方法中,返回此BooleanWidget类的实例。在setModelData()和setEditorData()中,您现在可以将输入小部件强制转换到此BooleanWidget:

BooleanWidget * widget = qobject_cast<BooleanWidget*>(editor);
BooleanWidget*widget=qobject_cast(编辑器);

然后使用is/setChecked方法。

解决了以下问题:

void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool checkValue;

QStyleOptionButton BtnStyle;
BtnStyle.state = QStyle::State_Enabled;

if(index.model()->data(index, Qt::DisplayRole).toBool() == true)
{
BtnStyle.state |= QStyle::State_On;
checkValue = true;
}else{
BtnStyle.state |= QStyle::State_Off;
checkValue = false;
}


BtnStyle.direction = QApplication::layoutDirection();
BtnStyle.rect = option.rect;
QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
}
认为继承自itemDelegate QStyledItemDelegate和重新实现的方法“paint”和“editorEvent”

在方法“editorEvent”中,发出一个信号,指示选择了哪一行

这是密码

class ItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

signals:
    void clickSignal(int);


public:
    ItemDelegate(QObject *parent = 0)
        : QStyledItemDelegate(parent)
    {
    }

    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
        QStyleOptionViewItemV4 viewItemOption(option);

        if (index.column() == 0) {
            const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
            QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
                                                QSize(option.decorationSize.width() + 5,option.decorationSize.height()),
                                                QRect(option.rect.x() + textMargin, option.rect.y(),
                                                      option.rect.width() - (2 * textMargin), option.rect.height()));
            viewItemOption.rect = newRect;
        }
        QStyledItemDelegate::paint(painter, viewItemOption, index);

    }

    virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
                             const QModelIndex &index)
    {
        Q_ASSERT(event);
        Q_ASSERT(model);

        // make sure that the item is checkable
        Qt::ItemFlags flags = model->flags(index);
        if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
            return false;
        // make sure that we have a check state
        QVariant value = index.data(Qt::CheckStateRole);
        if (!value.isValid())
            return false;
        // make sure that we have the right event type
        if (event->type() == QEvent::MouseButtonRelease) {
            const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
            QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
                                                  option.decorationSize,
                                                  QRect(option.rect.x() + (2 * textMargin), option.rect.y(),
                                                        option.rect.width() - (2 * textMargin),
                                                        option.rect.height()));

        } else {
            return false;
        }
        Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
                                ? Qt::Unchecked : Qt::Checked);




        emit(clickSignal(index.row()));

        return model->setData(index, state, Qt::CheckStateRole);
    }


};

使用标记的检查执行操作,这就是我对编辑器控件进行的居中对齐操作:

QWidget *checkBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    QCheckBox *editor = new QCheckBox(parent);
    editor->setTristate(allowTriState); //my class' variable
    // this does the trick :)
    editor->setStyleSheet("QCheckBox {margin-left: 43%; margin-right: 57%;}"); 
    // this should do it better
    // editor->setStyleSheet("QCheckBox {margin-left: auto; margin-right: auto;}");
    // but in my case the checkbox is slightly to the left of original
    return editor;
}

我使用以下项解决此问题:

booleanitemdelegate.h

#ifndef BOOLEANITEMDELEGATE_H
#define BOOLEANITEMDELEGATE_H

#include <QItemDelegate>



class BooleanItemDelegate : public QItemDelegate
{
public:
    BooleanItemDelegate(QObject *parent);

public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;public:
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};

#endif // BOOLEANITEMDELEGATE_H

我希望我能帮上忙。

不用太麻烦,直接设置rect:

BtnStyle.rect = option.rect;
构造一个新的QRect,并将x坐标向右移动,如您所愿:

QRect r = option.rect;
QRect r1(r.x() + 34, r.y(), r.width(), r.height());
BtnStyle.rect = r1;

Python中的解决方案“居中”复选框和“允许用户选中/取消选中”

可以给我举个例子吗?因为据我所见,函数的构造如下:drawCheck(qPaint*painter,const QStyleOptionViewItem&option,QRect&rect,Qt::state);我加了一个例子。如果要使其可编辑(而不仅仅是显示),那么还应该重写tablemodels flags()函数。好的,谢谢,我解决了部分问题。支票居中。现在的问题是,我在QTableView中看到了该复选框,单击该复选框后,该复选框将被移到左侧而未选中。我必须双击才能选择所有Qt itemdelegates编辑器模式仅在双击时激活。我试图自己找到一种方法来解决这个复选框问题,但这可能会比它的价值更麻烦。我如何停止在表中单击以将复选框移到左侧?对我来说,这是最好的答案,因为这是最简单的,而且效果很好。这是最好的答案之一。很简单,我也很喜欢。这是最好的答案。它必须放在顶部!
#ifndef BOOLEANITEMDELEGATE_H
#define BOOLEANITEMDELEGATE_H

#include <QItemDelegate>



class BooleanItemDelegate : public QItemDelegate
{
public:
    BooleanItemDelegate(QObject *parent);

public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;public:
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};

#endif // BOOLEANITEMDELEGATE_H
#include "booleanitemdelegate.h"

BooleanItemDelegate::BooleanItemDelegate(QObject *parent):
    QItemDelegate(parent)
{

}

void BooleanItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    drawCheck(painter, option, option.rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked);
    drawFocus(painter, option, option.rect);
}

bool BooleanItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if(event->type() == QEvent::MouseButtonRelease){
        model->setData(index, !model->data(index).toBool());
        event->accept();
    }
    return QItemDelegate::editorEvent(event, model, option, index);
}
BtnStyle.rect = option.rect;
QRect r = option.rect;
QRect r1(r.x() + 34, r.y(), r.width(), r.height());
BtnStyle.rect = r1;