簡體   English   中英

Qt5 C ++:設置特定表列的Spinbox委托

[英]Qt5 C++: Set Spinbox Delegate For Specific Table Column

我正在嘗試將Spinbox項目委托添加到表中的特定列。 看完Qt中的示例后,我復制了大部分代碼並實現了它,但是當我調用setItemDelegateForColumn()我的應用程序崩潰了。 列索引有效。 有任何想法我做錯了嗎?

主要呼叫方式:

BinarySpinboxDelegate binarySpinboxDelegate;
ui->UsersTable->setItemDelegateForColumn(users->at(0).size()-1 ,&binarySpinboxDelegate);

自定義Spinbox實施:

#include "binaryspinboxdelegate.h"
#include <QSpinBox>

BinarySpinboxDelegate::BinarySpinboxDelegate(QObject *parent) : QStyledItemDelegate(parent)
{

}

QWidget* BinarySpinboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
    QSpinBox* editor = new QSpinBox(parent);
    editor->setFrame(false);
    editor->setMinimum(0);
    editor->setMaximum(1);

    return editor;
}

void BinarySpinboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int value = index.model()->data(index, Qt::EditRole).toInt();

    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}

void BinarySpinboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();

    model->setData(index, value, Qt::EditRole);
}

void BinarySpinboxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
{
    editor->setGeometry(option.rect);
}

而且我是個白痴-_-事實證明我很傻,無法在表的初始化函數中聲明委托,然后在函數完成后超出范圍。 將其移動到頭文件中,以便在對象的整個生命周期中都存在並且很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM