使委托QtComboBox能够检测点击次数

时间:2014-05-04 04:17:42

标签: c++ qt qcombobox qitemdelegate

我是Qt的新手。我有一个表与委托组合框作为第二列。 我想检测单击组合框。

我想到了一种方法:在ComboBoxDelegate中添加组合框作为私有变量,并添加公共广告位

void on_cb_currentIndexChanged ( const QString & text );

但是,由于createEditor是const,我无法在此方法中重新分配变量cb。 有没有其他方法可以做到这一点?

class ComboBoxDelegate : public QItemDelegate
{
     Q_OBJECT

 public:
     ComboBoxDelegate(std::vector<std::string> values, QObject *parent = 0);
     ~ComboBoxDelegate();
     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;

   private:
    std::vector<std::string> values;
  }

1 个答案:

答案 0 :(得分:1)

正如您所说,您可以将组合框作为委托类的成员,并在createEditor中创建组合框时将组合框的currentIndexChanged信号连接到某个槽。你应该将组合框声明为可变的,以便在createEditor中新建它,它是常量。如果数据成员被声明为可变,那么从const成员函数为该数据成员赋值是合法的:

#include <QItemDelegate>

#include <QComboBox>

class ComboBoxDelegate: public QItemDelegate
{
 Q_OBJECT
public:
    ComboBoxDelegate(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;

    QStringList comboItems;

    mutable QComboBox *combo;

private slots:

    void setData(int val);

};

ComboBoxDelegate::ComboBoxDelegate(QObject *parent ):QItemDelegate(parent)
{
        comboItems<<"Item 1"<<"Item 2"<<"Item 3";
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    combo = new QComboBox( parent );
    QObject::connect(combo,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
    combo->addItems(comboItems);
    combo->setMaxVisibleItems(comboItems.count());
    return combo;
}

void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString text = index.model()->data( index, Qt::DisplayRole ).toString();

    int comboIndex = comboItems.indexOf(QRegExp(text));

    if(comboIndex>=0)
        (static_cast<QComboBox*>( editor ))->setCurrentIndex(comboIndex);
}

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


void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry( option.rect );
}

void ComboBoxDelegate::setData(int val)
{
    emit commitData(combo);
    //emit closeEditor(combo);
}

这里我将combobox的currentIndexChanged信号连接到setData槽,后者将数据提交给模型。您也可以将该信号连接到您想要的任何插槽。