Add workaround for loading forms with custom widgets from PyQt4

More information: qt-project.org/forums/viewthread/27098/
This commit is contained in:
Nathan Woodrow 2013-04-26 00:27:54 +10:00
parent 9e57117e79
commit e0838b0bbf
4 changed files with 57 additions and 0 deletions

View File

@ -427,6 +427,8 @@ class QgisInterface : QObject
virtual bool openFeatureForm( QgsVectorLayer *l, QgsFeature &f, bool updateFeatureOnly = false ) = 0;
virtual QDialog* getFeatureForm( QgsVectorLayer *l, QgsFeature &f ) = 0;
virtual void preloadForm(QString uifile) = 0;
/** Return vector layers in edit mode
* @param modified whether to return only layers that have been modified
* @returns list of layers in legend order, or empty list

View File

@ -21,6 +21,9 @@
#include <QMenu>
#include <QDialog>
#include <QAbstractButton>
#include <QSignalMapper>
#include <QTimer>
#include <QUiLoader>
#include "qgisappinterface.h"
#include "qgisappstylesheet.h"
@ -541,6 +544,37 @@ bool QgisAppInterface::openFeatureForm( QgsVectorLayer *vlayer, QgsFeature &f, b
}
}
void QgisAppInterface::preloadForm( QString uifile )
{
QSignalMapper* signalMapper = new QSignalMapper ( this );
mTimer = new QTimer(this);
connect( mTimer ,SIGNAL( timeout() ), signalMapper,SLOT( map() ) );
connect( signalMapper, SIGNAL( mapped(QString) ), mTimer, SLOT( stop() ) );
connect( signalMapper, SIGNAL( mapped(QString) ), this, SLOT( cacheloadForm( QString ) ) );
signalMapper->setMapping( mTimer, uifile );
mTimer->start(0);
}
void QgisAppInterface::cacheloadForm( QString uifile )
{
QUiLoader loader;
QFile file( uifile );
if ( file.open( QFile::ReadOnly ) )
{
QUiLoader loader;
QFileInfo fi( uifile );
loader.setWorkingDirectory( fi.dir() );
QWidget *myWidget = loader.load( &file );
file.close();
}
}
QDialog* QgisAppInterface::getFeatureForm( QgsVectorLayer *l, QgsFeature &f )
{
QgsDistanceArea myDa;

View File

@ -382,6 +382,19 @@ class QgisAppInterface : public QgisInterface
virtual QDialog* getFeatureForm( QgsVectorLayer *l, QgsFeature &f );
/** This method is only needed when using a UI form with a custom widget plugin and calling
* openFeatureForm or getFeatureForm from Python (PyQt4) and you havn't used the info tool first.
* Python will crash bringing QGIS wtih it
* if the custom form is not loaded from a C++ method call.
*
* This method uses a QTimer to call QUiLoader in order to load the form via C++
* you only need to call this once after that you can call openFeatureForm/getFeatureForm
* like normal
*
* More information here: http://qt-project.org/forums/viewthread/27098/
*/
virtual void preloadForm(QString uifile);
/** Return vector layers in edit mode
* @param modified whether to return only layers that have been modified
* @returns list of layers in legend order, or empty list
@ -395,6 +408,10 @@ class QgisAppInterface : public QgisInterface
signals:
void currentThemeChanged( QString );
private slots:
void cacheloadForm( QString uifile );
private:
/// QgisInterface aren't copied
@ -406,6 +423,8 @@ class QgisAppInterface : public QgisInterface
//! Pointer to the QgisApp object
QgisApp *qgis;
QTimer *mTimer;
//! Pointer to the LegendInterface object
QgsAppLegendInterface legendIface;
};

View File

@ -477,6 +477,8 @@ class GUI_EXPORT QgisInterface : public QObject
virtual QDialog* getFeatureForm( QgsVectorLayer *l, QgsFeature &f ) = 0;
virtual void preloadForm( QString uifile ) = 0;
/** Return vector layers in edit mode
* @param modified whether to return only layers that have been modified
* @returns list of layers in legend order, or empty list