2013-01-09 69 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