gpt4 book ai didi

c++ - QComboBox 不显示其项目列表

转载 作者:搜寻专家 更新时间:2023-10-31 01:51:04 26 4
gpt4 key购买 nike

我对 QComboBox 作为 QTableWidget 的项目委托(delegate)编辑器有疑问。QTableWidget 使用 SqlTypeDelegate 作为项目委托(delegate)。

当我在 QGraphicsScene 中绘制 QTableWidget(通过 QGraphicsProxyWidget)时,QComboBox 弹出的可用项目列表没有显示。但是,如果我将 QTableWidget 用作普通小部件(不是通过 QGraphicsScene\View 绘制),那么 QComboBox 行为是正常的 - 它显示项目列表。

我应该怎么做才能强制 QComboBox 显示它的项目列表?

示例代码如下:

主要.cpp:

#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTableWidget>
#include "sqltypedelegate.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
QTableWidget *table = new QTableWidget(4,2);
table->setItemDelegate(new SqlTypeDelegate(table));
QGraphicsProxyWidget *proxy = scene.addWidget(table);
QGraphicsView view(&scene);
view.show();
return app.exec();
}

sqltypedelegate.h:

#include <QItemDelegate>
#include <QStyledItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QComboBox>

class SqlTypeDelegate : public QItemDelegate
{
Q_OBJECT
public:
SqlTypeDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

sqltypedelegate.cpp:

#include <QtGui>
#include "sqltypedelegate.h"

SqlTypeDelegate::SqlTypeDelegate(QObject *parent)
: QItemDelegate(parent)
{}

QWidget *SqlTypeDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem(QString("decimal(50)"));
editor->addItem(QString("integer"));
editor->addItem(QString("varchar(50)"));
editor->addItem(QString("char"));

editor->setEditable(true);
return editor;
}

void SqlTypeDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->setEditText(value);
}

void SqlTypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
model->setData(index, comboBox->currentText(), Qt::EditRole);
}

void SqlTypeDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->setGeometry(option.rect);
}

最佳答案

最后我找到了解决方案:事件过滤器!只需潜伏 QEvent::MouseButtonRelease,然后调用 showPopup。

bool SqlTypeDelegate::eventFilter(QObject *object, QEvent *event)
{
QComboBox * comboBox = dynamic_cast<QComboBox*>(object);
if (comboBox)
{
if (event->type() == QEvent::MouseButtonRelease)
{
comboBox->showPopup();
return true;
}
}
else
{
return QItemDelegate::eventFilter( object, event );
}
return false;
}

关于c++ - QComboBox 不显示其项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248036/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com