C++ 如何在列表视图中显示每个项目的所有QComboxes?
我已经设置了一个QItemDelegate,它有一个编辑器,它是一个QComboBox。我已将此项委托设置为我的列表视图 目前,只有当我单击列表视图中的某个项目时,才会显示该特定项目的组合框。我通过这样做一次点击:C++ 如何在列表视图中显示每个项目的所有QComboxes?,c++,qt,C++,Qt,我已经设置了一个QItemDelegate,它有一个编辑器,它是一个QComboBox。我已将此项委托设置为我的列表视图 目前,只有当我单击列表视图中的某个项目时,才会显示该特定项目的组合框。我通过这样做一次点击: ui->suggestedListView->setEditTriggers( QAbstractItemView::AllEditTriggers ); 我想要的是,对于列表视图中的每个项目,显示其组合框,而不是让用户双击它来查看它 这是我的项目代表: #includ
ui->suggestedListView->setEditTriggers( QAbstractItemView::AllEditTriggers );
我想要的是,对于列表视图中的每个项目,显示其组合框,而不是让用户双击它来查看它
这是我的项目代表:
#include "include/gui/comboboxdelegate.h"
ComboBoxDelegate::ComboBoxDelegate( QObject *parent )
: QItemDelegate( parent )
{
}
QWidget *ComboBoxDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex & ) const
{
QComboBox *editor = new QComboBox( parent );
editor->addItem( "Address" );
editor->addItem( "Address2" );
editor->addItem( "City" );
editor->addItem( "Country" );
editor->addItem( "Date of Birth" );
editor->addItem( "Email" );
editor->addItem( "Fax Number" );
editor->addItem( "First Name" );
editor->addItem( "Gender" );
editor->addItem( "Last Activity Timestamp" );
editor->addItem( "Last Name" );
editor->addItem( "Middle Name" );
editor->addItem( "Mobile Number" );
editor->addItem( "Phone Number" );
editor->addItem( "Reference Code" );
editor->addItem( "Signup Category" );
editor->addItem( "IP Address" );
editor->addItem( "Signup Timestamp" );
editor->addItem( "Signup URL" );
editor->addItem( "State/Province/Region" );
editor->addItem( "Zip/Postal Code" );
editor->addItem( "Discard" );
return editor;
}
void ComboBoxDelegate::setEditorData( QWidget *editor,
const QModelIndex &index ) const
{
QString value = index.model()->data( index, Qt::EditRole ).toString();
QComboBox *cBox = static_cast<QComboBox *>( editor );
cBox->setCurrentIndex( cBox->findText( value ) );
}
void ComboBoxDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index ) const
{
QComboBox *cBox = static_cast<QComboBox *>( editor );
QString value = cBox->currentText();
model->setData( index, value, Qt::EditRole );
}
void ComboBoxDelegate::updateEditorGeometry( QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex & ) const
{
editor->setGeometry( option.rect );
}
这可能吗?如果不是,还有什么解决办法
QStyle::drawControl
::editorEvent
):创建编辑器(QComboBox
)并强制其显示下拉列表请注意,使用
QStyledItemDelegate
而不是QItemDelegate
这使我走上了正确的方向。非常感谢。我使用了完全相同的函数,只是将QItemDelegate更改为QStyledItemDelegate并添加了一个paint()函数。
ui->suggestedListView->setItemDelegate( new ComboBoxDelegate( ui->suggestedListView ) );
ui->suggestedListView->setEditTriggers( QAbstractItemView::AllEditTriggers );