Skip to content

Commit ad5aa30

Browse files
committedJul 23, 2014
Fix attribute dialog crash on close
1 parent 677abbf commit ad5aa30

File tree

5 files changed

+38
-97
lines changed

5 files changed

+38
-97
lines changed
 

‎src/app/qgisappinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ QDialog* QgisAppInterface::getFeatureForm( QgsVectorLayer *l, QgsFeature &featur
628628
myDa.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
629629

630630
QgsAttributeDialog *dialog = new QgsAttributeDialog( l, &feature, false, NULL, true );
631-
return dialog->dialog();
631+
return dialog;
632632
}
633633

634634
QgsVectorLayerTools* QgisAppInterface::vectorLayerTools()

‎src/app/qgsfeatureaction.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
6464

6565
if ( mLayer->actions()->size() > 0 )
6666
{
67-
dialog->dialog()->setContextMenuPolicy( Qt::ActionsContextMenu );
67+
dialog->setContextMenuPolicy( Qt::ActionsContextMenu );
6868

69-
QAction *a = new QAction( tr( "Run actions" ), dialog->dialog() );
69+
QAction *a = new QAction( tr( "Run actions" ), dialog );
7070
a->setEnabled( false );
71-
dialog->dialog()->addAction( a );
71+
dialog->addAction( a );
7272

7373
for ( int i = 0; i < mLayer->actions()->size(); i++ )
7474
{
@@ -77,11 +77,11 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
7777
if ( !action.runable() )
7878
continue;
7979

80-
QgsFeatureAction *a = new QgsFeatureAction( action.name(), *f, mLayer, i, -1, dialog->dialog() );
81-
dialog->dialog()->addAction( a );
80+
QgsFeatureAction *a = new QgsFeatureAction( action.name(), *f, mLayer, i, -1, dialog );
81+
dialog->addAction( a );
8282
connect( a, SIGNAL( triggered() ), a, SLOT( execute() ) );
8383

84-
QAbstractButton *pb = dialog->dialog()->findChild<QAbstractButton *>( action.name() );
84+
QAbstractButton *pb = dialog->findChild<QAbstractButton *>( action.name() );
8585
if ( pb )
8686
connect( pb, SIGNAL( clicked() ), a, SLOT( execute() ) );
8787
}

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ void QgsRelationReferenceWidget::referenceChanged( int index )
182182
// Backup old dialog and delete only after creating the new dialog, so we can "hot-swap" the contained QgsFeature
183183
QgsAttributeDialog* oldDialog = mAttributeDialog;
184184

185-
if ( mAttributeDialog && mAttributeDialog->dialog() )
185+
if ( mAttributeDialog )
186186
{
187-
mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );
187+
mAttributeEditorLayout->removeWidget( mAttributeDialog );
188188
}
189189

190190
// TODO: Get a proper QgsDistanceArea thingie
191191
mAttributeDialog = new QgsAttributeDialog( mReferencedLayer, new QgsFeature( feat ), true, mAttributeEditorFrame, false, mEditorContext );
192-
QWidget* attrDialog = mAttributeDialog->dialog();
192+
QWidget* attrDialog = mAttributeDialog;
193193
attrDialog->setWindowFlags( Qt::Widget ); // Embed instead of opening as window
194194
mAttributeEditorLayout->addWidget( attrDialog );
195195
attrDialog->show();

‎src/gui/qgsattributedialog.cpp

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer* vl, QgsFeature* thepFeature, bool featureOwner, QgsDistanceArea myDa, QWidget* parent, bool showDialogButtons )
28-
: QObject( parent )
28+
: QDialog( parent )
2929
, mHighlight( 0 )
3030
{
3131
QgsAttributeEditorContext context;
@@ -40,7 +40,7 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer* vl, QgsFeature* thepFeat
4040
}
4141

4242
QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer* vl, QgsFeature* thepFeature, bool featureOwner, QWidget* parent, bool showDialogButtons, QgsAttributeEditorContext context )
43-
: QObject( parent )
43+
: QDialog( parent )
4444
, mHighlight( 0 )
4545
{
4646
init( vl, thepFeature, context, parent );
@@ -60,25 +60,17 @@ QgsAttributeDialog::~QgsAttributeDialog()
6060
delete mHighlight;
6161
}
6262

63-
delete mDialog;
63+
saveGeometry();
6464
}
6565

6666
void QgsAttributeDialog::saveGeometry()
6767
{
68-
if ( mDialog )
69-
{
70-
QSettings settings;
71-
settings.setValue( mSettingsPath + "geometry", mDialog->saveGeometry() );
72-
}
68+
QSettings().setValue( mSettingsPath + "geometry", QDialog::saveGeometry() );
7369
}
7470

7571
void QgsAttributeDialog::restoreGeometry()
7672
{
77-
if ( mDialog )
78-
{
79-
QSettings settings;
80-
mDialog->restoreGeometry( settings.value( mSettingsPath + "geometry" ).toByteArray() );
81-
}
73+
QDialog::restoreGeometry( QSettings().value( mSettingsPath + "geometry" ).toByteArray() );
8274
}
8375

8476
void QgsAttributeDialog::setHighlight( QgsHighlight* h )
@@ -91,73 +83,28 @@ void QgsAttributeDialog::setHighlight( QgsHighlight* h )
9183
void QgsAttributeDialog::accept()
9284
{
9385
mAttributeForm->save();
86+
QDialog::accept();
9487
}
9588

96-
int QgsAttributeDialog::exec()
89+
void QgsAttributeDialog::show( bool autoDelete )
9790
{
98-
if ( mDialog )
99-
{
100-
return mDialog->exec();
101-
}
102-
else
103-
{
104-
QgsDebugMsg( "No dialog" );
105-
return QDialog::Accepted;
106-
}
107-
}
91+
if ( autoDelete )
92+
setAttribute( Qt::WA_DeleteOnClose );
10893

109-
void QgsAttributeDialog::show()
110-
{
111-
if ( mDialog )
112-
{
113-
mDialog->setAttribute( Qt::WA_DeleteOnClose );
114-
mDialog->show();
115-
mDialog->raise();
116-
mDialog->activateWindow();
117-
mDialog->installEventFilter( this );
118-
setParent( mDialog );
119-
}
120-
}
121-
122-
bool QgsAttributeDialog::eventFilter( QObject* obj, QEvent* e )
123-
{
124-
if ( mHighlight && obj == mDialog )
125-
{
126-
switch ( e->type() )
127-
{
128-
case QEvent::WindowActivate:
129-
mHighlight->show();
130-
break;
131-
132-
case QEvent::WindowDeactivate:
133-
mHighlight->hide();
134-
break;
135-
136-
default:
137-
break;
138-
}
139-
}
140-
141-
return false;
142-
}
143-
144-
void QgsAttributeDialog::onDialogFinished( int result )
145-
{
146-
Q_UNUSED( result )
147-
saveGeometry();
94+
QDialog::show();
95+
raise();
96+
activateWindow();
14897
}
14998

15099
void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, QgsAttributeEditorContext& context, QWidget* parent )
151100
{
152-
mDialog = new QDialog( parent );
153-
mDialog->setWindowTitle( tr( "Feature Attributes" ) );
154-
mDialog->setLayout( new QGridLayout() );
155-
mDialog->layout()->setMargin( 0 );
101+
setWindowTitle( tr( "Feature Attributes" ) );
102+
setLayout( new QGridLayout() );
103+
layout()->setMargin( 0 );
156104
mAttributeForm = new QgsAttributeForm( layer, *feature, context, parent );
157-
mDialog->layout()->addWidget( mAttributeForm );
105+
layout()->addWidget( mAttributeForm );
158106
QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();
159-
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( reject() ) );
160-
connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( accept() ) );
161-
connect( mDialog, SIGNAL( finished( int ) ), this, SLOT( onDialogFinished( int ) ) );
107+
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
108+
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
162109
restoreGeometry();
163110
}

‎src/gui/qgsattributedialog.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "qgsattributeform.h"
2323

2424
#include <QDialog>
25-
#include <QPointer>
2625

2726
class QLayout;
2827

@@ -33,7 +32,7 @@ class QgsHighlight;
3332
class QgsVectorLayer;
3433
class QgsVectorLayerTools;
3534

36-
class GUI_EXPORT QgsAttributeDialog : public QObject
35+
class GUI_EXPORT QgsAttributeDialog : public QDialog
3736
{
3837
Q_OBJECT
3938

@@ -83,7 +82,14 @@ class GUI_EXPORT QgsAttributeDialog : public QObject
8382
*/
8483
void setHighlight( QgsHighlight *h );
8584

86-
QDialog *dialog() { return mDialog; }
85+
/**
86+
* @brief Returns reference to self. Only here for legacy compliance
87+
*
88+
* @return this
89+
*
90+
* @deprecated Do not use. Just use this object itself. Or QgsAttributeForm if you want to embed.
91+
*/
92+
Q_DECL_DEPRECATED QDialog *dialog() { return this; }
8793

8894
QgsAttributeForm* attributeForm() { return mAttributeForm; }
8995

@@ -115,25 +121,13 @@ class GUI_EXPORT QgsAttributeDialog : public QObject
115121
public slots:
116122
void accept();
117123

118-
//! Show the dialog and block the application until the dialog is closed. Ownership of this object is not changed.
119-
int exec();
120-
121124
//! Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form and is deleted when
122125
//! closed.
123-
void show();
124-
125-
protected:
126-
bool eventFilter( QObject *obj, QEvent *e );
127-
128-
private slots:
129-
void onDialogFinished( int result );
126+
void show( bool autoDelete = true );
130127

131128
private:
132129
void init( QgsVectorLayer* layer, QgsFeature* feature, QgsAttributeEditorContext& context, QWidget* parent );
133130

134-
// Using a guarded pointer we can savely delete the dialog in the destructor even
135-
// when the dialog is this object's parent
136-
QPointer<QDialog> mDialog;
137131
QString mSettingsPath;
138132
// Used to sync multiple widgets for the same field
139133
QgsHighlight *mHighlight;

0 commit comments

Comments
 (0)
Please sign in to comment.