Qt ItemDelegate带有工具按钮:无法点击

时间:2014-05-07 06:26:36

标签: qt qitemdelegate

我为项目委托使用自定义小部件。

此小部件由组合框和工具按钮组成,请参阅下面的源代码。

现在,当我在项目委托中使用此窗口小部件时,如果组合框没有焦点,则按下工具按钮无效。 有关演示,请参阅此视频:http://youtu.be/o5AgjC4cCqY

知道怎么处理这个吗?

非常感谢!

小部件的来源:

QgsFieldExpressionWidget::QgsFieldExpressionWidget( QWidget *parent )
    : QWidget( parent )
{
  QHBoxLayout* layout = new QHBoxLayout( this );
  layout->setContentsMargins( 0, 0, 0, 0 );
  mCombo = new QComboBox( this );
  mCombo->setEditable( true );
  mCombo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );

  mButton = new QToolButton( this );
  mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
  mButton->setIcon( QgsApplication::getThemeIcon( "/mIconExpressionEditorOpen.svg" ) );

  layout->addWidget( mCombo );
  layout->addWidget( mButton );
}

代表来源:

QgsComposerColumnSourceDelegate::QgsComposerColumnSourceDelegate( QgsVectorLayer* vlayer, QObject* parent ) : QItemDelegate( parent ),
    mVectorLayer( vlayer )
{

}

QWidget* QgsComposerColumnSourceDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
  Q_UNUSED( option );
  Q_UNUSED( index );

  QgsFieldExpressionWidget *fieldExpression = new QgsFieldExpressionWidget( parent );
  fieldExpression->setLayer( mVectorLayer );

  connect( fieldExpression, SIGNAL( fieldChanged( QString ) ), this, SLOT( commitAndCloseEditor() ) );
  return fieldExpression;
}

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

//set the value for the field combobox
  QgsFieldExpressionWidget *fieldExpression = static_cast<QgsFieldExpressionWidget*>( editor );
  fieldExpression->setField( field );
}

void QgsComposerColumnSourceDelegate::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const
{
  QgsFieldExpressionWidget *fieldExpression = static_cast<QgsFieldExpressionWidget*>( editor );
  QString field = fieldExpression->currentField();

  model->setData( index, field, Qt::EditRole );
}

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

1 个答案:

答案 0 :(得分:3)

QgsFieldExpressionWidget创建后的任何位置,将以下行添加到QComboBox构造函数中:

setFocusProxy(mCombo);