27 #include <QStringListModel> 42 int cbxIdx = mComboBox->currentIndex();
45 v = mComboBox->currentData();
51 QStringList selection;
52 for (
int i = 0; i < mListWidget->count(); ++i )
54 QListWidgetItem *item = mListWidget->item( i );
55 if ( item->checkState() == Qt::Checked )
56 selection << item->data( Qt::UserRole ).toString();
59 v = selection.join( QStringLiteral(
"," ) ).prepend(
'{' ).append(
'}' );
66 if ( item.
value == mLineEdit->text() )
79 if (
config( QStringLiteral(
"AllowMulti" ) ).toBool() )
81 return new QListWidget( parent );
83 else if (
config( QStringLiteral(
"UseCompleter" ) ).toBool() )
88 return new QComboBox( parent );
96 mComboBox = qobject_cast<QComboBox *>( editor );
97 mListWidget = qobject_cast<QListWidget *>( editor );
98 mLineEdit = qobject_cast<QLineEdit *>( editor );
102 if (
config( QStringLiteral(
"AllowNull" ) ).toBool() )
104 mComboBox->addItem( tr(
"(no selection)" ), QVariant(
field().type() ) );
109 mComboBox->addItem( element.
value, element.
key );
112 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
115 else if ( mListWidget )
119 QListWidgetItem *item =
nullptr;
120 item =
new QListWidgetItem( element.
value );
121 item->setData( Qt::UserRole, element.
key );
123 mListWidget->addItem( item );
127 else if ( mLineEdit )
130 values.reserve( mCache.size() );
136 QStringListModel *m =
new QStringListModel( values, mLineEdit );
137 QCompleter *completer =
new QCompleter( m, mLineEdit );
138 completer->setCaseSensitivity( Qt::CaseInsensitive );
139 mLineEdit->setCompleter( completer );
141 connect( mLineEdit, &QLineEdit::textChanged,
this, [ = ](
const QString &
value ) { emit
valueChanged( value ); } );
147 return mListWidget || mLineEdit || mComboBox;
154 QStringList checkList;
155 if ( value.type() == QVariant::StringList )
156 checkList = value.toStringList();
157 else if ( value.type() == QVariant::String )
158 checkList = value.toString().remove( QChar(
'{' ) ).remove( QChar(
'}' ) ).split(
',' );
160 for (
int i = 0; i < mListWidget->count(); ++i )
162 QListWidgetItem *item = mListWidget->item( i );
163 item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
166 else if ( mComboBox )
168 mComboBox->setCurrentIndex( mComboBox->findData( value ) );
170 else if ( mLineEdit )
174 if ( i.
key == value )
176 mLineEdit->setText( i.
value );
187 mListWidget->blockSignals(
true );
188 for (
int i = 0; i < mListWidget->count(); ++i )
190 mListWidget->item( i )->setCheckState( Qt::PartiallyChecked );
192 mListWidget->blockSignals(
false );
194 else if ( mComboBox )
198 else if ( mLineEdit )
206 if ( mEnabled == enabled )
213 for (
int i = 0; i < mListWidget->count(); ++i )
215 QListWidgetItem *item = mListWidget->item( i );
218 item->setFlags( item->flags() | Qt::ItemIsEnabled );
220 item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Represents a vector layer which manages a vector based data sets.