2

QTableWidget の項目委任エディターとして QComboBox に問題があります。QTableWidget は SqlTypeDelegate をアイテム デリゲートとして使用します。

(QGraphicsProxyWidget を介して) QGraphicsScene 内で QTableWidget を描画すると、使用可能なアイテムの QComboBox ポップアップ リストが表示されません。しかし、QTableWidget を通常のウィジェット (QGraphicsScene\View ではなく描画) として使用している場合、QComboBox の動作は正常です。項目リストが表示されます。

QComboBox に項目リストを強制的に表示するにはどうすればよいですか?

以下のサンプルコード:

main.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);
}
4

2 に答える 2

2

最後に解決策を見つけました: イベントフィルター! 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;
}
于 2013-03-12T12:24:04.630 に答える
1

私は同じ問題を抱えており、私の回避策は createEditor(...) に 2 つの行コードを追加することです。

editor->move(option.rect.x(),option.rect.y());
editor->showPopup();

次に、テーブル項目をダブルクリックすると、QComboBox は、2 番目のマウス ボタンを押すことによってトリガーされるポップアップ項目を表示し、2 番目のマウス ボタンを放すことによってトリガーされるポップアップ項目を非表示にします。これは Qt 4.8 で動作します。

Qt5.0 も試してみましたが、この回避策の有無にかかわらずアプリケーションがクラッシュしました。この問題をバグとして報告しました。

于 2013-01-25T04:07:05.347 に答える