0
我試圖將spinbox項目委託添加到我表中的特定列。在查看了Qt中的示例之後,我複製了大部分代碼並實現了它,但是當我調用setItemDelegateForColumn()
時,我的應用程序崩潰。列索引是有效的。任何想法我做錯了什麼?Qt5 C++:爲特定表列設置Spinbox委託
主要調用方法:
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);
}
這也不是我第一次被垃圾收集器咬了。但你的回答仍然救了我。我正在PySide中編輯代表。 – foresightyj
我很高興我的麻煩可以讓別人受益哈哈:D – T3CHN0CR4T