2013-01-09 68 views
1

我有QComboBox作爲QTableWidget項目delegeate編輯器的問題。 QTableWidget使用SqlTypeDelegate作爲項目委託。QComboBox不顯示其項目列表

當我在QGraphicsScene(通過QGraphicsProxyWidget)中繪製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); 
} 

回答

2

Finaly我找到解決方案:事件過濾器! 只需潛伏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; 
} 
+0

你確定這可以嗎?在Python(PyQt4)中,我從來沒有得到過'QEvent :: MOuseButtonRelease',所以所有的'comboBox'事件都被過濾掉了。我認爲這是造成問題的'comboBox'的'QtCore.QEvent.FocusOut'。 – Onlyjus

1

我得到了同樣的問題我的解決方法是添加噸wo行代碼創建編輯器(...)。

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

然後,當雙擊表項時,QComboBox將顯示由2鼠標按鍵按下觸發彈出的項目和隱藏由2鼠標按鍵釋放觸發彈出式項目。這適用於Qt 4.8。

我也試過Qt5.0和應用程序崩潰有或沒有此解決方法。 我已將此問題報告爲一個錯誤。

+0

我找到了解決辦法,看看我的答案。 – msnxcp