本文整理汇总了C++中QgsVectorLayer::setFieldConstraint方法的典型用法代码示例。如果您正苦于以下问题:C++ QgsVectorLayer::setFieldConstraint方法的具体用法?C++ QgsVectorLayer::setFieldConstraint怎么用?C++ QgsVectorLayer::setFieldConstraint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QgsVectorLayer
的用法示例。
在下文中一共展示了QgsVectorLayer::setFieldConstraint方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testFieldConstraint
void TestQgsAttributeForm::testFieldConstraint()
{
// make a temporary vector layer
QString def = QStringLiteral( "Point?field=col0:integer" );
QgsVectorLayer* layer = new QgsVectorLayer( def, QStringLiteral( "test" ), QStringLiteral( "memory" ) );
layer->setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() ) );
// add a feature to the vector layer
QgsFeature ft( layer->dataProvider()->fields(), 1 );
ft.setAttribute( QStringLiteral( "col0" ), 0 );
// build a form for this feature
QgsAttributeForm form( layer );
form.setFeature( ft );
// testing stuff
QString validLabel = QStringLiteral( "col0<font color=\"green\">✔</font>" );
QString invalidLabel = QStringLiteral( "col0<font color=\"red\">✘</font>" );
QString warningLabel = QStringLiteral( "col0<font color=\"orange\">✘</font>" );
// set constraint
layer->setConstraintExpression( 0, QString() );
// get wrapper
QgsEditorWidgetWrapper *ww;
ww = qobject_cast<QgsEditorWidgetWrapper*>( form.mWidgets[0] );
// no constraint so we expect a label with just the field name
QLabel *label = form.mBuddyMap.value( ww->widget() );
QCOMPARE( label->text(), QString( "col0" ) );
// set a not null constraint
layer->setConstraintExpression( 0, QStringLiteral( "col0 is not null" ) );
// build a form for this feature
QgsAttributeForm form2( layer );
form2.setFeature( ft );
QSignalSpy spy( &form2, SIGNAL( attributeChanged( QString, QVariant ) ) );
ww = qobject_cast<QgsEditorWidgetWrapper*>( form2.mWidgets[0] );
label = form2.mBuddyMap.value( ww->widget() );
// set value to 1
ww->setValue( 1 );
QCOMPARE( spy.count(), 2 );
QCOMPARE( label->text(), validLabel );
// set value to null
spy.clear();
ww->setValue( QVariant() );
QCOMPARE( spy.count(), 2 );
QCOMPARE( label->text(), invalidLabel );
// set value to 1
spy.clear();
ww->setValue( 1 );
QCOMPARE( spy.count(), 2 );
QCOMPARE( label->text(), validLabel );
// set a soft constraint
layer->setConstraintExpression( 0, QStringLiteral( "col0 is not null" ) );
layer->setFieldConstraint( 0, QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft );
// build a form for this feature
QgsAttributeForm form3( layer );
form3.setFeature( ft );
ww = qobject_cast<QgsEditorWidgetWrapper*>( form3.mWidgets[0] );
label = form3.mBuddyMap.value( ww->widget() );
// set value to 1
ww->setValue( 1 );
QCOMPARE( label->text(), validLabel );
// set value to null
ww->setValue( QVariant() );
QCOMPARE( label->text(), warningLabel );
// set value to 1
ww->setValue( 1 );
QCOMPARE( label->text(), validLabel );
}
示例2: testOKButtonStatus
void TestQgsAttributeForm::testOKButtonStatus()
{
// make a temporary vector layer
QString def = QStringLiteral( "Point?field=col0:integer" );
QgsVectorLayer* layer = new QgsVectorLayer( def, QStringLiteral( "test" ), QStringLiteral( "memory" ) );
// add a feature to the vector layer
QgsFeature ft( layer->dataProvider()->fields(), 1 );
ft.setAttribute( QStringLiteral( "col0" ), 0 );
ft.setValid( true );
// set constraint
layer->setConstraintExpression( 0, QString() );
// build a form for this feature
QgsAttributeForm form( layer );
form.setFeature( ft );
QPushButton *okButton = form.mButtonBox->button( QDialogButtonBox::Ok );
// get wrapper
QgsEditorWidgetWrapper *ww;
ww = qobject_cast<QgsEditorWidgetWrapper*>( form.mWidgets[0] );
// testing stuff
QSignalSpy spy1( &form, SIGNAL( attributeChanged( QString, QVariant ) ) );
QSignalSpy spy2( layer, SIGNAL( editingStarted() ) );
QSignalSpy spy3( layer, SIGNAL( editingStopped() ) );
// no constraint but layer not editable : OK button disabled
QCOMPARE( layer->isEditable(), false );
QCOMPARE( okButton->isEnabled(), false );
// no constraint and editable layer : OK button enabled
layer->startEditing();
QCOMPARE( spy2.count(), 1 );
QCOMPARE( layer->isEditable(), true );
QCOMPARE( okButton->isEnabled(), true );
// invalid constraint and editable layer : OK button disabled
layer->setConstraintExpression( 0, QStringLiteral( "col0 = 0" ) );
QgsAttributeForm form2( layer );
form2.setFeature( ft );
ww = qobject_cast<QgsEditorWidgetWrapper*>( form2.mWidgets[0] );
okButton = form2.mButtonBox->button( QDialogButtonBox::Ok );
ww->setValue( 1 );
QCOMPARE( okButton->isEnabled(), false );
// valid constraint and editable layer : OK button enabled
layer->setConstraintExpression( 0, QStringLiteral( "col0 = 2" ) );
QgsAttributeForm form3( layer );
form3.setFeature( ft );
ww = qobject_cast<QgsEditorWidgetWrapper*>( form3.mWidgets[0] );
okButton = form3.mButtonBox->button( QDialogButtonBox::Ok );
ww->setValue( 2 );
QCOMPARE( okButton->isEnabled(), true );
// valid constraint and not editable layer : OK button disabled
layer->rollBack();
QCOMPARE( spy3.count(), 1 );
QCOMPARE( layer->isEditable(), false );
QCOMPARE( okButton->isEnabled(), false );
// set soft constraint
layer->setFieldConstraint( 0, QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthSoft );
QgsAttributeForm form4( layer );
form4.setFeature( ft );
ww = qobject_cast<QgsEditorWidgetWrapper*>( form4.mWidgets[0] );
okButton = form4.mButtonBox->button( QDialogButtonBox::Ok );
ww->setValue( 1 );
QVERIFY( !okButton->isEnabled() );
layer->startEditing();
// just a soft constraint, so OK should be enabled
QVERIFY( okButton->isEnabled() );
layer->rollBack();
QVERIFY( !okButton->isEnabled() );
}