Hello!
Sorry for being a little offtopic. I am a Python junior developer. I am using PyQt5.QtWidgets Python module. My problem is with laying out widgets on parent object, so I guess they are not Python-specific.
Namely, I need a widget to be positioned using GridLayout, and then I need to delete it a little later.
If I create a widget specifying a parent widget among constructor arguments, ia gets automatically positioned on a parent widget, which is what I don't want. If I do not specify parent explicitly, but instead create a QGridLaoyut object in a parent widget and add my child widget using AddWidget method of a QGridLaoyut object, widget is positioned OK, but I cannot reach it from a parent (because this is not its parent, as mentioned earlier). So the questions are:
1. How can I maintain some control ovet my child widgets without getting them auto-positioned by the parent?
2. How exactly are child widgets positioned on a parent (if parent is specified)? I checked parent layout() method, and it does not return any valid laoyut object.
Thanks in advance!
30 August 2022
Всеволод Никоноров2. maybe my question is not clear. Widgets in fact are positioned on a parent even if I did not call any layout methods explicitly. If a widget is positioned, there should be a layout object controlling it, shouldn't it?
if you just specify a parent, they are not automatically added to a layout
Всеволод НиконоровIs there a way to iterate over a layout's children?
there is.
a layout children is *not* a widget, but a LayoutItem, that contains a widget.
please look at the documentation for that.
tomaz canabravaif you just specify a parent, they are not automatically added to a layout
But I sure see them right after creation. Overlapping one another.
FreyaI don't understand - if you don't want your widgets to be positioned automatically, why use a layout? That's kind of its job?
I want them not just partitioned in any way, but in one specific way - using GridLayout
Всеволод НиконоровBut I sure see them right after creation. Overlapping one another.
then create then and imediately add them to the grid layout.
tomaz canabravathere is.
a layout children is *not* a widget, but a LayoutItem, that contains a widget.
please look at the documentation for that.
Thanks for the hint. This way I can call their methods!
tomaz canabravathen create then and imediately add them to the grid layout.
If I add widgets they appear on a window immediately, but are in fact not laid out. Interesting thing: QPushButton.clicked.connect() does not work before laying out a button, even though button visually respond when I try to click it.
Всеволод НиконоровIf I add widgets they appear on a window immediately, but are in fact not laid out. Interesting thing: QPushButton.clicked.connect() does not work before laying out a button, even though button visually respond when I try to click it.
No, that's incorrect.
The dialogs are visible *if* the eventloop has time to paint them, when you are on a method (or on the widget constructor), there's no way that a paint can run.
tomaz canabravaplease show us your code, there something quite wrong on it.
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout
import sys
class View(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.btn1 = QPushButton(self, text="very long button name")
self.btn2 = QPushButton(self, text="short name")
self.btn1.clicked.connect(self.pressbutton1)
self.btn2.clicked.connect(self.pressbutton2)
#self.grid = QGridLayout()
#self.setLayout(self.grid)
def pressbutton1(self):
print(1, flush=True)
def pressbutton2(self):
print(2, flush=True)
def main():
app = QApplication([])
view = View()
view.show()
sys.exit(app.exec())
if name == "__main__":
main()
Всеволод НиконоровIf I add widgets they appear on a window immediately, but are in fact not laid out. Interesting thing: QPushButton.clicked.connect() does not work before laying out a button, even though button visually respond when I try to click it.
Here I mentioned the fact that click handler is not executed if button is not laid out.