QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgsfieldcombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldcombobox.cpp
3 --------------------------------------
4 Date : 01.04.2014
5 Copyright : (C) 2014 Denis Rouzaud
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
16#include "qgsfieldcombobox.h"
17#include "qgsfieldproxymodel.h"
18#include "qgsmaplayer.h"
19#include "qgsvectorlayer.h"
20#include "qgsfieldmodel.h"
21
23 : QComboBox( parent )
24{
25 mFieldProxyModel = new QgsFieldProxyModel( this );
26 setModel( mFieldProxyModel );
27
28 connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsFieldComboBox::indexChanged );
29}
30
32{
33 mFieldProxyModel->setFilters( filters );
34}
35
37{
38 mFieldProxyModel->sourceFieldModel()->setAllowEmptyFieldName( allowEmpty );
39}
40
42{
43 return mFieldProxyModel->sourceFieldModel()->allowEmptyFieldName();
44}
45
47{
48 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
49 mFieldProxyModel->sourceFieldModel()->setLayer( vl );
50}
51
53{
54 return mFieldProxyModel->sourceFieldModel()->layer();
55}
56
58{
59 mFieldProxyModel->sourceFieldModel()->setFields( fields );
60}
61
63{
64 return mFieldProxyModel->sourceFieldModel()->fields();
65}
66
67void QgsFieldComboBox::setField( const QString &fieldName )
68{
69 const QString prevField = currentField();
70 const QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
71 if ( idx.isValid() )
72 {
73 const QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
74 if ( proxyIdx.isValid() )
75 {
76 setCurrentIndex( proxyIdx.row() );
77 }
78 else
79 {
80 setCurrentIndex( -1 );
81 }
82 }
83 else
84 {
85 setCurrentIndex( -1 );
86 }
87
88 if ( prevField != currentField() )
89 emit fieldChanged( currentField() );
90}
91
93{
94 const int i = currentIndex();
95
96 const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
97 if ( !proxyIndex.isValid() )
98 {
99 return QString();
100 }
101
102 QString name = mFieldProxyModel->data( proxyIndex, static_cast< int >( QgsFieldModel::CustomRole::FieldName ) ).toString();
103 return name;
104}
105
107{
108 Q_UNUSED( i )
109 const QString name = currentField();
110 emit fieldChanged( name );
111}
void setFilters(QgsFieldProxyModel::Filters filters)
setFilters allows filtering according to the type of field
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void setFields(const QgsFields &fields)
Manually sets the fields to use for the combo box.
void setLayer(QgsMapLayer *layer)
Sets the layer for which fields are listed in the combobox.
QgsFieldProxyModel::Filters filters
QString currentField() const
Returns the currently selected field.
QgsFields fields() const
Returns the fields currently shown in the combobox.
QgsVectorLayer * layer() const
Returns the layer currently associated with the combobox.
void setField(const QString &fieldName)
setField sets the currently selected field
QgsFieldComboBox(QWidget *parent=nullptr)
QgsFieldComboBox creates a combo box to display the fields of a layer.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is shown in the combo box.
@ FieldName
Return field name if index corresponds to a field.
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
QgsFields fields() const
Returns the fields currently shown in the model.
void setFields(const QgsFields &fields)
Manually sets the fields to use for the model.
bool allowEmptyFieldName
QgsVectorLayer * layer
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer.
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
QFlags< Filter > Filters
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
Container of fields for a vector layer.
Definition qgsfields.h:46
Base class for all map layer types.
Definition qgsmaplayer.h:75
Represents a vector layer which manages a vector based data sets.