Unsolved All widets list on a widget
-
Hi All,
A quick quesiton.
Is there any way to get all widgets on a specific widget, which is the lowest base widget, in pyQt5?
I tried findChildren but it requres a search widget name string as a parameter such as QPushButton. Why it doesn't work with just empty string???Many thanks for your help.
Sat -
Hi @ShinSat,
Are all of your child widgets in a layout? If so, you can call
QLayout::itemAt()
followed byQLayoutItem::widget()
.I'm not sure if these methods are available in PyQt or not.
-
@JKSH Thanks for an update. Some children are NOT in layout. :<
Sat
-
@ShinSat said in All widets list on a widget:
@JKSH Thanks for an update. Some children are NOT in layout. :<
I can't think of a way to get a non-layouted child without specifying its type, sorry.
May I ask why you want to get all the child widgets?
-
@ShinSat I could think of a very, time-consuming way to list all widgets.
If you need to create the list once, than this could be fine. And its in c++ as I don't use pyqtQList<QWidget *> listOfWidgets; int width(targetwidget->width()); int height(targetwidget->height()); for(int x(0); x < width; x++){ for(int y(0); y < height; y++){ QWidget *w = targetwidget->childAt(x,y); if(w && !listOfWidgets.contains(w)) listOfWidgets.append(w); } }
-
@J.Hilk said in All widets list on a widget:
@ShinSat I could think of a very, time-consuming way to list all widgets.
If you need to create the list once, than this could be fine. And its in c++ as I don't use pyqtQList<QWidget *> listOfWidgets; int width(targetwidget->width()); int height(targetwidget->height()); for(int x(0); x < width; x++){ for(int y(0); y < height; y++){ QWidget *w = targetwidget->childAt(x,y); if(w && !listOfWidgets.contains(w)) listOfWidgets.append(w); } }
Haha!
This assumes that all child widgets are already visible.
-
Thank you very much for sharing various ideas!
Well, let me share what I'm trying.
I want to save the app window(widgets) structure, which includes both horizontal and vertical hierarchy.
The app consists of multiple widgets on one MAIN widget.
By saving the structure when the app shut down, the app can restore every widget during the startup(the same widget layout).
I think the most challenging part is to get and list all vertical structure, which can be dynamically changed by the user in runtime.Sat
-
@ShinSat Well the easiest way would probably be to create your own base class. That includes or inherits from QSettings.
in the Destructor you save all relevent data via QSettings, visible, x, y, width, height etc and in the constructor you load them via QSettings.
-
@ShinSat said in All widets list on a widget:
Well, let me share what I'm trying.
I want to save the app window(widgets) structure, which includes both horizontal and vertical hierarchy.
The app consists of multiple widgets on one MAIN widget.
By saving the structure when the app shut down, the app can restore every widget during the startup(the same widget layout).
I think the most challenging part is to get and list all vertical structure, which can be dynamically changed by the user in runtime.Have a look at:
QWidget::saveGeometry()
/QWidget::restoreGeometry()
QMainWindow::saveState()
/QMainWindow::restoreState()
Are these enough to fully save/restore your widget?
-
Hi
Just as a note. findChildren does work without using a name.QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();
also no name is not like
parentWidget.findChildren<QPushButton *>(""); // its an empty name
That would be empty name.
No name is
parentWidget.findChildren<QPushButton *>(QString()); // this is truely "no name"
which is default param. -
Have a look at:
QWidget::saveGeometry()/QWidget::restoreGeometry()
QMainWindow::saveState()/QMainWindow::restoreState()Are these enough to fully save/restore your widget?
@JKSH
Actually, I also need to save/restore the app's horizontal and vertical hierarchy.
Since the hierarchy is something like widgets on widget on widget on widget ,,,, I would like to find out how I can achieve it in Qt(in my env, it's pyqt5.9) :)
findChildren() is probably the only way to achieve that?, I'm still testing, though...
I can't find a right direction so far. :<Sat
-
@mrjj said in All widets list on a widget:
parentWidget.findChildren<QPushButton *>(QString()); // this is truely "no name"
AFAIK, QString() is not available in pyqt5, but I'll double check that. :)
Thanks,
Sat -
@ShinSat
ok?
Its default value for name paramter so maybe just leave it out completely as inno "" for name.