@@ -89,6 +89,7 @@ class TestQgsLabelingEngine : public QObject
89
89
void testLineAnchorCurvedConstraints ();
90
90
void testLineAnchorHorizontal ();
91
91
void testLineAnchorHorizontalConstraints ();
92
+ void testShowAllLabelsWhenALabelHasNoCandidates ();
92
93
93
94
private:
94
95
QgsVectorLayer *vl = nullptr ;
@@ -3212,5 +3213,67 @@ void TestQgsLabelingEngine::testLineAnchorHorizontalConstraints()
3212
3213
QVERIFY ( imageCheck ( QStringLiteral ( " horizontal_strict_anchor_end" ), img, 20 ) );
3213
3214
}
3214
3215
3216
+ void TestQgsLabelingEngine::testShowAllLabelsWhenALabelHasNoCandidates ()
3217
+ {
3218
+ // test that showing all labels when a label has no candidate placements doesn't
3219
+ // result in a crash
3220
+ // refs https://github.com/qgis/QGIS/issues/38093
3221
+
3222
+ QgsPalLayerSettings settings;
3223
+ setDefaultLabelParams ( settings );
3224
+
3225
+ QgsTextFormat format = settings.format ();
3226
+ format.setSize ( 20 );
3227
+ format.setColor ( QColor ( 0 , 0 , 0 ) );
3228
+ settings.setFormat ( format );
3229
+
3230
+ settings.fieldName = QStringLiteral ( " 'xxxxxxxxxxxxxx'" );
3231
+ settings.isExpression = true ;
3232
+ settings.placement = QgsPalLayerSettings::Line;
3233
+ settings.lineSettings ().setPlacementFlags ( QgsLabeling::LinePlacementFlag::OnLine );
3234
+ settings.obstacleSettings ().setFactor ( 10 );
3235
+ settings.lineSettings ().setOverrunDistance ( 50 );
3236
+
3237
+ std::unique_ptr< QgsVectorLayer> vl2 ( new QgsVectorLayer ( QStringLiteral ( " LineString?crs=epsg:23700&field=l:string" ), QStringLiteral ( " vl" ), QStringLiteral ( " memory" ) ) );
3238
+ vl2->setRenderer ( new QgsNullSymbolRenderer () );
3239
+
3240
+ QgsFeature f;
3241
+ f.setAttributes ( QgsAttributes () << QVariant () );
3242
+
3243
+ f.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " LineString (-2446233 -5204828, -2342845 -5203825)" ) ) );
3244
+ QVERIFY ( vl2->dataProvider ()->addFeature ( f ) );
3245
+
3246
+ f.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " LineString (-2439207 -5198806, -2331302 -5197802)" ) ) );
3247
+ QVERIFY ( vl2->dataProvider ()->addFeature ( f ) );
3248
+
3249
+ vl2->setLabeling ( new QgsVectorLayerSimpleLabeling ( settings ) ); // TODO: this should not be necessary!
3250
+ vl2->setLabelsEnabled ( true );
3251
+
3252
+ // make a fake render context
3253
+ QSize size ( 640 , 480 );
3254
+ QgsMapSettings mapSettings;
3255
+ mapSettings.setLabelingEngineSettings ( createLabelEngineSettings () );
3256
+ mapSettings.setDestinationCrs ( vl2->crs () );
3257
+
3258
+ mapSettings.setOutputSize ( size );
3259
+ mapSettings.setExtent ( QgsRectangle ( -3328044.9 , -5963176 ., -1127782.7 , -4276844.3 ) );
3260
+ mapSettings.setLayers ( QList<QgsMapLayer *>() << vl2.get () );
3261
+ mapSettings.setOutputDpi ( 96 );
3262
+
3263
+ QgsLabelingEngineSettings engineSettings = mapSettings.labelingEngineSettings ();
3264
+ engineSettings.setFlag ( QgsLabelingEngineSettings::DrawLabelRectOnly, true );
3265
+ engineSettings.setFlag ( QgsLabelingEngineSettings::UseAllLabels, true );
3266
+ engineSettings.setFlag ( QgsLabelingEngineSettings::DrawUnplacedLabels, true );
3267
+ // engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, true );
3268
+ mapSettings.setLabelingEngineSettings ( engineSettings );
3269
+
3270
+ QgsMapRendererSequentialJob job ( mapSettings );
3271
+ job.start ();
3272
+ job.waitForFinished ();
3273
+
3274
+ QImage img = job.renderedImage ();
3275
+ QVERIFY ( imageCheck ( QStringLiteral ( " show_all_labels_when_no_candidates" ), img, 20 ) );
3276
+ }
3277
+
3215
3278
QGSTEST_MAIN ( TestQgsLabelingEngine )
3216
3279
#include " testqgslabelingengine.moc"
0 commit comments