QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 "moc_qgsfieldcombobox.cpp"
18#include "qgsfieldproxymodel.h"
19#include "qgsmaplayer.h"
20#include "qgsvectorlayer.h"
21#include "qgsfieldmodel.h"
22
24 : QComboBox( parent )
25{
26 mFieldProxyModel = new QgsFieldProxyModel( this );
27 setModel( mFieldProxyModel );
28
29 connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsFieldComboBox::indexChanged );
30}
31
33{
34 mFieldProxyModel->setFilters( filters );
35}
36
38{
39 mFieldProxyModel->sourceFieldModel()->setAllowEmptyFieldName( allowEmpty );
40}
41
43{
44 return mFieldProxyModel->sourceFieldModel()->allowEmptyFieldName();
45}
46
48{
49 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
50 mFieldProxyModel->sourceFieldModel()->setLayer( vl );
51}
52
54{
55 return mFieldProxyModel->sourceFieldModel()->layer();
56}
57
59{
60 mFieldProxyModel->sourceFieldModel()->setFields( fields );
61}
62
64{
65 return mFieldProxyModel->sourceFieldModel()->fields();
66}
67
68void QgsFieldComboBox::setField( const QString &fieldName )
69{
70 const QString prevField = currentField();
71 const QModelIndex idx = mFieldProxyModel->sourceFieldModel()->indexFromName( fieldName );
72 if ( idx.isValid() )
73 {
74 const QModelIndex proxyIdx = mFieldProxyModel->mapFromSource( idx );
75 if ( proxyIdx.isValid() )
76 {
77 setCurrentIndex( proxyIdx.row() );
78 }
79 else
80 {
81 setCurrentIndex( -1 );
82 }
83 }
84 else
85 {
86 setCurrentIndex( -1 );
87 }
88
89 if ( prevField != currentField() )
90 emit fieldChanged( currentField() );
91}
92
94{
95 const int i = currentIndex();
96
97 const QModelIndex proxyIndex = mFieldProxyModel->index( i, 0 );
98 if ( !proxyIndex.isValid() )
99 {
100 return QString();
101 }
102
103 QString name = mFieldProxyModel->data( proxyIndex, static_cast< int >( QgsFieldModel::CustomRole::FieldName ) ).toString();
104 return name;
105}
106
108{
109 Q_UNUSED( i )
110 const QString name = currentField();
111 emit fieldChanged( name );
112}
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:76
Represents a vector layer which manages a vector based data sets.