Skip to content

Commit

Permalink
Fix attribute form labels show _ character if field name/alias has & …
Browse files Browse the repository at this point in the history
…characters

Qt treats these like shortcut key indicators in QLabel text if
they aren't escaped by a double &.

(cherry-picked from cca7141)
  • Loading branch information
nyalldawson committed Aug 14, 2018
1 parent ef9d4bf commit fc61803
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -1204,7 +1204,7 @@ void QgsAttributeForm::init()
tabWidget = nullptr;
WidgetInfo widgetInfo = createWidgetFromDef( widgDef, container, mLayer, mContext );
QLabel *label = new QLabel( widgetInfo.labelText );
label->setToolTip( QStringLiteral( "<b>%1</b><p>%2</p>" ).arg( widgetInfo.labelText, widgetInfo.hint ) );
label->setToolTip( widgetInfo.toolTip );
if ( columnCount > 1 && !widgetInfo.labelOnTop )
{
label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
Expand Down Expand Up @@ -1283,6 +1283,8 @@ void QgsAttributeForm::init()

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

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

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

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

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

break;
Expand Down Expand Up @@ -1719,7 +1723,7 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
else
{
QLabel *mypLabel = new QLabel( widgetInfo.labelText );
mypLabel->setToolTip( QStringLiteral( "<b>%1</b><p>%2</p>" ).arg( widgetInfo.labelText, widgetInfo.hint ) );
mypLabel->setToolTip( widgetInfo.toolTip );
if ( columnCount > 1 && !widgetInfo.labelOnTop )
{
mypLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsattributeform.h
Expand Up @@ -325,6 +325,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
{
QWidget *widget = nullptr;
QString labelText;
QString toolTip;
QString hint;
bool labelOnTop = false;
bool labelAlignRight = false;
Expand Down

0 comments on commit fc61803

Please sign in to comment.