Skip to content

Commit fc61803

Browse files
committed
Fix attribute form labels show _ character if field name/alias has & characters
Qt treats these like shortcut key indicators in QLabel text if they aren't escaped by a double &. (cherry-picked from cca7141)
1 parent ef9d4bf commit fc61803

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/gui/qgsattributeform.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ void QgsAttributeForm::init()
12041204
tabWidget = nullptr;
12051205
WidgetInfo widgetInfo = createWidgetFromDef( widgDef, container, mLayer, mContext );
12061206
QLabel *label = new QLabel( widgetInfo.labelText );
1207-
label->setToolTip( QStringLiteral( "<b>%1</b><p>%2</p>" ).arg( widgetInfo.labelText, widgetInfo.hint ) );
1207+
label->setToolTip( widgetInfo.toolTip );
12081208
if ( columnCount > 1 && !widgetInfo.labelOnTop )
12091209
{
12101210
label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
@@ -1283,6 +1283,8 @@ void QgsAttributeForm::init()
12831283

12841284
//show attribute alias if available
12851285
QString fieldName = mLayer->attributeDisplayName( idx );
1286+
QString labelText = fieldName;
1287+
labelText.replace( '&', QStringLiteral( "&&" ) ); // need to escape '&' or they'll be replace by _ in the label text
12861288

12871289
const QgsEditorWidgetSetup widgetSetup = QgsGui::editorWidgetRegistry()->findBest( mLayer, field.name() );
12881290

@@ -1292,7 +1294,7 @@ void QgsAttributeForm::init()
12921294
bool labelOnTop = mLayer->editFormConfig().labelOnTop( idx );
12931295

12941296
// This will also create the widget
1295-
QLabel *l = new QLabel( fieldName );
1297+
QLabel *l = new QLabel( labelText );
12961298
l->setToolTip( QStringLiteral( "<b>%1</b><p>%2</p>" ).arg( fieldName, field.comment() ) );
12971299
QSvgWidget *i = new QSvgWidget();
12981300
i->setFixedSize( 18, 18 );
@@ -1625,6 +1627,8 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
16251627

16261628
newWidgetInfo.labelOnTop = mLayer->editFormConfig().labelOnTop( fieldDef->idx() );
16271629
newWidgetInfo.labelText = mLayer->attributeDisplayName( fieldDef->idx() );
1630+
newWidgetInfo.labelText.replace( '&', QStringLiteral( "&&" ) ); // need to escape '&' or they'll be replace by _ in the label text
1631+
newWidgetInfo.toolTip = QStringLiteral( "<b>%1</b><p>%2</p>" ).arg( mLayer->attributeDisplayName( fieldDef->idx() ), newWidgetInfo.hint );
16281632
newWidgetInfo.showLabel = widgetDef->showLabel();
16291633

16301634
break;
@@ -1719,7 +1723,7 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
17191723
else
17201724
{
17211725
QLabel *mypLabel = new QLabel( widgetInfo.labelText );
1722-
mypLabel->setToolTip( QStringLiteral( "<b>%1</b><p>%2</p>" ).arg( widgetInfo.labelText, widgetInfo.hint ) );
1726+
mypLabel->setToolTip( widgetInfo.toolTip );
17231727
if ( columnCount > 1 && !widgetInfo.labelOnTop )
17241728
{
17251729
mypLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter );

src/gui/qgsattributeform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
325325
{
326326
QWidget *widget = nullptr;
327327
QString labelText;
328+
QString toolTip;
328329
QString hint;
329330
bool labelOnTop = false;
330331
bool labelAlignRight = false;

0 commit comments

Comments
 (0)