[PyQt] Drop to QTDesigner using PyQt4 question

Mathias.MAZEL at akka.eu Mathias.MAZEL at akka.eu
Mon Nov 9 17:07:38 GMT 2015


Using PyQt4 + QT4.8.4, I'd like to drop (external) textual content to a widget defined as a PyQt4 plugin in QtDesigner

I use two python classes :
- widgetlabelplugin.py inherited from QPyDesignerCustomWidgetPlugin
- widgetlabel.py inherited from QLabel

Overriding the dropEvent in (widgetlabel.py), I'm able to retrieve "external textual content" and to set _model property.

I do the following steps :
1 - Launch designer bu previously setting PYQTDESIGNERPATH to the .py path
2 - Create a dialog without button
3 - Drop a PyGMT/ WiddgetLabel on the dialog
4 - Drop a "textual content" (from notepad) to the widgetlabel label
-> at this step, the label is updated on the dialog but not on the properties browser on the right
5 - Save Dialog from Qt designer tool bar 
-> ui file doesn't contain any "textual content" neither for QLabel/text, nor WidgetLabel/model
6 - In Qt designer, If I select dialog background and reselect WidgetLabel, properties are updated in the browser, they are still not saved if I save the ui !

Thanks for your help !

----------------------------------------------------------------------------------
python class : widgetlabelplugin.py
----------------------------------------------------------------------------------
# A demonstration custom widget plugin for PROJECT Qt Designer.
from PyQt4 import QtGui, QtDesigner

from widgetlabel import WidgetLabel

# This class implements the interface expected by Qt Designer to access the
# custom widget. See the description of the QDesignerCustomWidgetInterface
# class for full details.
class WidgetLabelPlugin(QtDesigner.QPyDesignerCustomWidgetPlugin):

# Initialise the instance.
def __init__(self, parent=None):
super(WidgetLabelPlugin, self).__init__(parent)

self._initialized = False

# Initialise the custom widget for use with the specified formEditor
# interface.
def initialize(self, formEditor):
if self._initialized:
return

self._initialized = True

# Return True if the custom widget has been intialised.
def isInitialized(self):
return self._initialized

# Return a new instance of the custom widget with the given parent.
def createWidget(self, parent):
return WidgetLabel(parent)

# Return the name of the class that implements the custom widget.
def name(self):
return "WidgetLabel"

# Return the name of the group to which the custom widget belongs. A new
# group will be created if it doesn't already exist.
def group(self):
return "PyGMT"

# Return the icon used to represent the custom widget in Designer's widget
# box.
def icon(self):
return QtGui.QIcon(":/designer/frame.png")

# Return a short description of the custom widget used by Designer in a
# tool tip.
def toolTip(self):
return "Satis demonstration widget"

# Return a full description of the custom widget used by Designer in
# "What's This?" help for the widget.
def whatsThis(self):
return "WidgetLabel is a demonstration custom widget written in Python " \
"using PyQt."

# Return True if the custom widget acts as a container for other widgets.
def isContainer(self):
return False

# Return the name of the module containing the class that implements the
# custom widget. It may include a module path.
def includeFile(self):
return "WidgetLabel"

----------------------------------------------------------------------------------
python class : widgetlabel.py
----------------------------------------------------------------------------------
#############################################################################
##
## This file is part of the examples of PROJECT Qt Designer.
##
#############################################################################

from PyQt4 import QtCore, QtGui

# This is the class that implements the custom widget for PROJECT.
class WidgetLabel(QtGui.QLabel):

#model changed signal
modelChanged = QtCore.pyqtSignal(QtCore.QString)

# Initialise the instance.
def __init__(self, parent=None):
super(WidgetLabel, self).__init__(parent)
self.setAcceptDrops(True)
self.setText("Label")

# Initialise the model property. 
self._model = None

###########################################################################
# DRAG & DROP PART #

###########################################################################
def dragEnterEvent(self, e):
if e.mimeData().hasFormat("text/plain"):
e.setDropAction(QtCore.Qt.CopyAction)
e.accept()
else:
e.ignore()

def dragMoveEvent(self, event):
if event.mimeData().hasFormat("text/plain"):
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore

def dropEvent(self, e):
e.acceptProposedAction()
bStream = e.mimeData().retrieveData("text/plain",
QtCore.QVariant.ByteArray)

self.setModel(QtCore.QString(str(bStream.toByteArray())))

# The getter for the zoom property.
def getModel(self):
return self._model

# The setter for the model property. We also make define this as a Qt slot
# which can be connected to Qt signals in Qt Designer.
@QtCore.pyqtSlot(QtCore.QString)
def setModel(self, model):
# print "new model", model
# Set QLabel text
self.setText(model)

# Don't do anything if nothing has changed.
if self._model == model:
return

# Remember the new model level.
self._model = model

# Emit the Qt signal to say that the model level has changed.
self.modelChanged.emit(model)

# The resetter for the model property.
def resetModel(self):
self.setModel(None)

# Define the model property. Changing the value of this in Qt Designer's
# property editor causes the model to change dynamically.
model = QtCore.pyqtProperty(QtCore.QString, getModel, setModel, resetModel)

# Display the custom widget if the script is being run directly from the
# command line.
if __name__ == "__main__":

import sys

app = QtGui.QApplication(sys.argv)

demo = WidgetLabel()
demo.show()

sys.exit(app.exec_())

_______________________________________


L'intégrité de ce message n'étant pas assurée sur internet, AKKA TECHNOLOGIES et ses filiales ne peuvent être tenues responsables de son contenu. Ce message et les éventuels fichiers attachés contiennent des informations confidentielles. Au cas où il ne vous serait pas destiné, nous vous remercions de bien vouloir le supprimer et en aviser l'expéditeur. Toute utilisation de ce message non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. 



This message and the files that may be attached to it contain confidential information. AKKA TECHNOLOGIES or its subsidiaries may not be held responsible for their contents, whose accuracy and completeness cannot be guaranteed over the internet. If the message is not addressed to you, kindly delete it and notify the sender. Any use of this message not in accordance with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.


________________________________________



More information about the PyQt mailing list