Basic question about PyQt4

as a part of my learning PyQt4 I started making a gui where I implement all the examples I learn from video tuts or book I am referring, :nod:

first thing I want to know while searching the web questions about PyQt4 I usually come across code written in C++ but since I don’t know c++ I want to know the syntactical translation like if -> is .(period) in Python what is “::” ? what other things to translate C++ code to PyQt4 ?

Secondly I built my GUI by inheriting from QWidget() now I want to add my own customized QFrame

so I made a new class object inheriting from QFrame

class MyFrame(QtGui.QFrame):

    def __init__(self, *args):
        super(MyFrame, self).__init__(*args)
        self.setStyleSheet("background-color: rgb(105,78,56); margin:5px; border:1px solid rgb(0, 255, 0); ")
        w = MyFrame()
        w.show()

how do I use it in my GUI based on QWidget and add further QPushButton or other widgets to MyFrame ?

You leave out the last two lines there. The first one creates an instance of the class the second one shows it. Instead you use your QWidget’s Layout’s .addWidget() method to add your custom widget where you want.

Regards,
Thorsten

you mean
w.addWidget(someBtn)## will add some QPushButton
w.addLayout(someLayout) # will add some layout

well this makes me think that that because I used QFrame as base class I have access to all the methods …OMG why i didnt think this way…

Kinda yes. You’d have your QWidget. This has some layout. And you would add your QFrame widget to the layout. To add widgets to your frame you would add a layout and add the widget to that. So far so good. There is no addLayout method tho. Typically you’d use self.addLayout() to add the layout to the widget. Then use self.layout().addWidget() to add subwidgets to it. And you’d create the instance of your QFrame in your parent QWidget, rather than in the Frames’ init.

Regards,
Thorsten

I just realised that stuff like this is highly confusing when written as text only. So here’s a working example with comments.


import sys
from PyQt4 import QtGui


class MyWindow(QtGui.QMainWindow):
    """This is the main window. Could also be a dialog, a Widget or whatever
    """
    def __init__(self):
        super(MyWindow, self).__init__() #Run init of the base class

        self.widget = MyWidget() #Create an instance of our custom widget
        self.setCentralWidget(self.widget) #Set the instance as the main widget of the window


class MyFrame(QtGui.QFrame):
    """This is your custom QFrame. You can add whatever functionality and
    properties you need.
    """
    def __init__(self, name):
        super(MyFrame, self).__init__() #Run init of the base class

        self.setLayout(QtGui.QVBoxLayout()) #Set the layout to be Vertical
        self.layout().addWidget(QtGui.QLabel(name)) #Add a label using our init parameter "name"
        self.layout().addWidget(QtGui.QPushButton("Button!")) #Add a button
        self.layout().addWidget(QtGui.QPushButton("Another Button!")) # Add another button for the fun of it


class MyWidget(QtGui.QWidget):
    """This is a containing Widget. You don't need to have that. I just added
    it, as you mentioned you want your Frame in a Widget
    """
    def __init__(self):
        super(MyWidget, self).__init__() #Run init of the base class

        self.setLayout((QtGui.QVBoxLayout())) #Set the layout to be Vertical
        self.layout().addWidget(MyFrame("Custom Frame 1")) #Add an instance of our Custom Frame
        self.layout().addWidget(MyFrame("Custom Frame 2")) #Add another instance of our Custom Frame with a different name

# Standard PyQt Application logic stuff. Would be different inside a host app obviously
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())


I just realised that stuff like this is highly confusing when written as text only.
you just read my mind