Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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/8/qt/6.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++ QComboBox不调用委托方法_C++_Qt_Delegates_Qcombobox_Model View - Fatal编程技术网

C++ QComboBox不调用委托方法

C++ QComboBox不调用委托方法,c++,qt,delegates,qcombobox,model-view,C++,Qt,Delegates,Qcombobox,Model View,我想通过插入QWidgets(而不是字符串)通过模型和委托来定制QComboBox: QComboBox *cb = new QComboBox(this); FeatureModel *featureModel = new FeatureModel(cb); cb->setModel(featureModel); ComboBoxItemDelegate *comboBoxItemDelegate = new ComboBoxItemDelegate(cb); cb->setI

我想通过插入
QWidgets
(而不是字符串)通过模型和委托来定制
QComboBox

QComboBox *cb = new QComboBox(this);

FeatureModel *featureModel = new FeatureModel(cb);
cb->setModel(featureModel);

ComboBoxItemDelegate *comboBoxItemDelegate = new ComboBoxItemDelegate(cb);
cb->setItemDelegate(comboBoxItemDelegate);
FeatureModel继承自QAbstractListModel,ComboBoxItemDelegate继承自QStyledItemDelegate

问题是从未调用委托方法,因此没有插入我的自定义小部件(我只看到
FeatureModel
的字符串)。 但是,如果我使用
QTableView
而不是
QComboBox
,它会正常工作

有人知道错误在哪里吗? 我是否遗漏了QT模型/视图的一些重要方面

编辑: 这是我的代表。 除了构造函数之外(当然),没有调用以下方法(控制台上没有输出)

ComboBoxItemDelegate::ComboBoxItemDelegate(QObject*parent):
QStyledItemDelegate(父级)
{
qDebug()show();
返回控件;
}
void ComboBoxItemDelegate::setEditorData(QWidget*编辑器,常量QModelIndex和索引)常量
{

qDebug()我想我找到问题了

首先,确保
QComboBox
中的视图允许编辑:

cb->view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool MyModel::setData(const QModelIndex &index,
                           const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        // Modify data..

        emit dataChanged(index, index);
        return true;
    }
    return false;
}
我不确定这是否是一个好的做法,但这是我唯一能让它起作用的方法。
editTriggers
的默认值是
qabstractemView::NoEditTriggers

其次,确保您的模型允许编辑:

cb->view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool MyModel::setData(const QModelIndex &index,
                           const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        // Modify data..

        emit dataChanged(index, index);
        return true;
    }
    return false;
}
但是它有一个问题。当您第一次看到
组合框时,您可以更改当前项目文本,并且它不会调用委托方法进行编辑。您必须选择一个项目,然后才能对其进行编辑

无论如何,我发现对可编辑项使用
QComboBox
是违反直觉的。您确定需要
QComboBox
来完成此任务吗


希望它有帮助

它应该能正常工作,你能粘贴你的代理代码吗?非常感谢!它现在可以工作了,方法正在调用,小部件正在创建,它们也显示在组合框中。关于你的问题:组合框应该包含用户可以通过点击属于它的按钮来删除的项目到特定的项目。我也在考虑添加另一个按钮,以获得关于项目的一些信息和指示其类型的小图像。这就是为什么我需要一个额外的小部件。这听起来很奇怪,但是的,也许combobox是一种方式。很高兴它能工作