[Python] A class object inside the class



  • Hello, how could i create, for example, Qt button in Qt widget class. I imagine that would like something like this:
    @class Window(QtGui.QWidget):
    def init(self):
    #...
    self.button = ButtonClass()
    button.show()

    class ButtonClass(...)
    def init(self):
    #...
    def show(self):
    self.show()@


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    are you looking for something like "this":http://qt-project.org/wiki/PySideTutorials_Clickable_button ?



  • Hey, thanks.
    That's right but i want to create neutral button and window classes so i could create many similiar buttons in a window (widget) class with different variables for each. In C# it is pretty simple (it's enough to do public class and then create object of it) but i have no idea how to do that in Python.


  • Lifetime Qt Champion

    What do you mean by neutral button ?

    Could you show an simple example of what you would like to achieve (even in pseudo code)



  • By saying neutral button i mean a class which can be used for creating more than two buttons just by defining other parameters and basicly is not for repeating the similiar code twice.

    This is an example how it must look (im not good at python, etc. in C# it is possible to do with pointers):
    @#Global object popUpWindow?

    class ButtonClass(...)
    def init(self): #and parameters after 'self' for title and text
    #...
    self.title = "Press this button" #first parameter
    self.text = "My text" #pop-up text, used in new window
    # These must be defined by creating object or defined later. Etc. firstButton = ButtonClass("Press this button","My text")
    button = QPushButton(self.title) # Creating exact button but it must be in Window object
    button.clicked.connect(setTextInWindow())

    def setTextInWindow(self):
    # Use global object here? and show popUpWindow with self.text label

    def show(self):
    self.show()

    class Window(QtGui.QWidget):
    def init(self):
    #...
    self.button = ButtonClass("""Parameters here""")
    button.show() # then show the button in main window

    class popUpWindow(QtGui.QWidget):

    ... Blank window only with text label from ButtonClass

    #Program main first calls Window then ButtonClass must be called by placing a button in Window widget. By pressing the button the pop-up appears (popUpWindow)@

    This must be pretty clear. A thing is that i don't know how to manage classes.
    EDIT: I have managed to find out a solution. Thank you for you help.


  • Lifetime Qt Champion

    Greate you found out !

    Don't use show on each button/sub-widget, put everything in a layout, that will manage position etc… for you.

    Since you have it working, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.