[PyQt] Layout and widget interaction
David Boddie
david at boddie.org.uk
Sat May 15 00:51:10 BST 2010
On Fri, 14 May 2010 22:37:52 +0530, Anshul Jain wrote:
> I have made a very small trial application in which there are 2 widgets
> inside a horizontal layout.(Please see attached image). One contains images
> and the other contains a text editor. The code snippet that adds these
> widgets to this layout is:
>
> mainWidget = QtGui.QWidget()
> horizontalLayout = QtGui.QHBoxLayout()
> horizontalLayout.addWidget(ImageWidget())
> horizontalLayout.addWidget(editorWidget())
>
> My aim is that when i click on an image in the ImageWidget() it must reads
> a file corresponding to that image and then display the text in
> editorWidget(). I have been able to write code to find which image has been
> l have clicked but then I don't know to transfer that filename to the
> editorWidget() in the horizontalLayout. How should I do it. Please help me
> out. I'm really stuck at it. :(
I suggest making the ImageWidget() emit a signal that you receive in a slot
in the editorWidget(). You will need to connect the signal somewhere, and
it's probably best to do that before you lay out those widgets.
imageWidget = ImageWidget()
editor = editorWidget()
imageWidget.clickedImage.connect(editor.openFile)
mainWidget = QtGui.QWidget()
horizontalLayout = QtGui.QHBoxLayout()
horizontalLayout.addWidget(imageWidget)
horizontalLayout.addWidget(editor)
In the image widget you will need to define a custom signal called
clickedImage that takes a string as an argument.
clickedImage = QtCore.pyqtSignal(QtCore.QString)
You need to emit this when handling the click.
In the editor widget, you simply need to define a method called openFile,
like this:
def openFile(self, path):
# Open the file with the given path.
Hope this helps,
David
More information about the PyQt
mailing list