Add a QWidget inside a QFrame

5.8k views Asked by At

I'm developing a desktop software using Python3 and QtDesigner for the Graphic User Interface.

My problem is the seguent: i'm trying to automate the creation of many QRadioButtons over a QFrame (The RadioButtons must stay inside the frame [as...children?]).

Now, i see that i can only create new widgets inside a Layout (e.g. "MyLayout.addWidget(QRadioButton")) and it's not possible to do something like "MyFrame.addWidget(QRadioButton)". I need these widgets inside the frame cause then i can place them in the correct position with "MyRB.move(X,Y)".

With QtDesigner is possible to place many Widgets (like RadioButtons) in a frame that has a 'broken layout' so i can choose X,Y coordinates but i need to create and place a variable number of those.

Is it possible to create Qwidgets inside a QFrame?

2

There are 2 answers

0
Michele V On

[EDIT] according to musicamante's comment, i got that's a parent problem. I tried to insert a Label and a RadioButton in the main window:

def __init__(self):
     super().__init__()
     uic.loadUi('DSS_GUI2.ui',self) # i load the GUI with QtDesigner
     LB1 = QLabel('MyLabel',self)
     RB1 = QRadioButton('MyRadioButton',self)
     ...

This very simple example works fine but when i try to add a Label through a function

def myFunction(self):
    LB1 = QLabel('MyLabel')
    LB1.setObjectName('LABEL_1')
    LB1.setParent(self.myFrame)

the Widget is inserted but it is not visible, in fact adding this lines to check his presence

    WidgetList = self.myFrame.findChildren(QLabel)
    
    for item in WidgetList:
        print(item.objectName())

i see in the console that the Label is there. Do you know why it's not visible?

0
BadmanSkinny On

Try

def myFunction(self):

    LB1 = self.sender()
    LB1.QLabel('MyLabel')
    LB1.setObjectName('LABEL_1')
    LB1.setParent(self.myFrame)

You can call self.myFunction() in parent.

If you wanted to pass label, you could:

def myFunction(self, label):

    LB1 = self.sender()
    LB1.QLabel(label)
    LB1.setObjectName(label)
    LB1.setParent(self.myFrame)