Skip to content

Commit e0838b0

Browse files
committedApr 25, 2013
Add workaround for loading forms with custom widgets from PyQt4
More information: qt-project.org/forums/viewthread/27098/
1 parent 9e57117 commit e0838b0

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
 

‎python/gui/qgisinterface.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ class QgisInterface : QObject
427427
virtual bool openFeatureForm( QgsVectorLayer *l, QgsFeature &f, bool updateFeatureOnly = false ) = 0;
428428

429429
virtual QDialog* getFeatureForm( QgsVectorLayer *l, QgsFeature &f ) = 0;
430+
431+
virtual void preloadForm(QString uifile) = 0;
430432
/** Return vector layers in edit mode
431433
* @param modified whether to return only layers that have been modified
432434
* @returns list of layers in legend order, or empty list

‎src/app/qgisappinterface.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <QMenu>
2222
#include <QDialog>
2323
#include <QAbstractButton>
24+
#include <QSignalMapper>
25+
#include <QTimer>
26+
#include <QUiLoader>
2427

2528
#include "qgisappinterface.h"
2629
#include "qgisappstylesheet.h"
@@ -541,6 +544,37 @@ bool QgisAppInterface::openFeatureForm( QgsVectorLayer *vlayer, QgsFeature &f, b
541544
}
542545
}
543546

547+
void QgisAppInterface::preloadForm( QString uifile )
548+
{
549+
QSignalMapper* signalMapper = new QSignalMapper ( this );
550+
mTimer = new QTimer(this);
551+
552+
connect( mTimer ,SIGNAL( timeout() ), signalMapper,SLOT( map() ) );
553+
connect( signalMapper, SIGNAL( mapped(QString) ), mTimer, SLOT( stop() ) );
554+
connect( signalMapper, SIGNAL( mapped(QString) ), this, SLOT( cacheloadForm( QString ) ) );
555+
556+
signalMapper->setMapping( mTimer, uifile );
557+
558+
mTimer->start(0);
559+
}
560+
561+
void QgisAppInterface::cacheloadForm( QString uifile )
562+
{
563+
QUiLoader loader;
564+
565+
QFile file( uifile );
566+
567+
if ( file.open( QFile::ReadOnly ) )
568+
{
569+
QUiLoader loader;
570+
571+
QFileInfo fi( uifile );
572+
loader.setWorkingDirectory( fi.dir() );
573+
QWidget *myWidget = loader.load( &file );
574+
file.close();
575+
}
576+
}
577+
544578
QDialog* QgisAppInterface::getFeatureForm( QgsVectorLayer *l, QgsFeature &f )
545579
{
546580
QgsDistanceArea myDa;

‎src/app/qgisappinterface.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ class QgisAppInterface : public QgisInterface
382382

383383
virtual QDialog* getFeatureForm( QgsVectorLayer *l, QgsFeature &f );
384384

385+
/** This method is only needed when using a UI form with a custom widget plugin and calling
386+
* openFeatureForm or getFeatureForm from Python (PyQt4) and you havn't used the info tool first.
387+
* Python will crash bringing QGIS wtih it
388+
* if the custom form is not loaded from a C++ method call.
389+
*
390+
* This method uses a QTimer to call QUiLoader in order to load the form via C++
391+
* you only need to call this once after that you can call openFeatureForm/getFeatureForm
392+
* like normal
393+
*
394+
* More information here: http://qt-project.org/forums/viewthread/27098/
395+
*/
396+
virtual void preloadForm(QString uifile);
397+
385398
/** Return vector layers in edit mode
386399
* @param modified whether to return only layers that have been modified
387400
* @returns list of layers in legend order, or empty list
@@ -395,6 +408,10 @@ class QgisAppInterface : public QgisInterface
395408
signals:
396409
void currentThemeChanged( QString );
397410

411+
private slots:
412+
413+
void cacheloadForm( QString uifile );
414+
398415
private:
399416

400417
/// QgisInterface aren't copied
@@ -406,6 +423,8 @@ class QgisAppInterface : public QgisInterface
406423
//! Pointer to the QgisApp object
407424
QgisApp *qgis;
408425

426+
QTimer *mTimer;
427+
409428
//! Pointer to the LegendInterface object
410429
QgsAppLegendInterface legendIface;
411430
};

‎src/gui/qgisinterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ class GUI_EXPORT QgisInterface : public QObject
477477

478478
virtual QDialog* getFeatureForm( QgsVectorLayer *l, QgsFeature &f ) = 0;
479479

480+
virtual void preloadForm( QString uifile ) = 0;
481+
480482
/** Return vector layers in edit mode
481483
* @param modified whether to return only layers that have been modified
482484
* @returns list of layers in legend order, or empty list

0 commit comments

Comments
 (0)
Please sign in to comment.