PDA

View Full Version : what happened to toolBars() in Qt4?



momesana
24th September 2007, 19:40
I need a function to return a List of all toolbars in the mainwindow. There used to be a toolBars() method in Qt3 that is gone now and I could'nt find an alternative. My current workaround is pretty ugly and I would like to know if there is a better way or a qt function that does this.

Thanx in advance

current workaround


QList<QToolBar*> MainWindow::toolBars()
{
QList<QToolBar*> tbs;
QToolBar *tb;
foreach(QObject* o, children()) {
tb = dynamic_cast<QToolBar*>(o);
if (tb)
tbs << tb;
}
return tbs;
}

spud
24th September 2007, 20:21
It seems that function is gone. You can however replace your call to toolBars() with


findChildren<QToolBar*>()

wysota
24th September 2007, 20:32
A simple solution is to keep the list of toolbars filled while adding them to the main window.

momesana
24th September 2007, 22:16
Thanks. The first solution seems more bulletproof since I might forget adding it to the list somehow and then I would unintentionally miss out on that Toolbar. :)

wysota
24th September 2007, 22:23
Depends what you value more - execution speed or one compilation less.

momesana
24th September 2007, 23:48
Depends what you value more - execution speed or one compilation less.
In this case, none of them. easier maintainability is the driving factor :). The purpose of the code is actually to dynamically query all toolbars for their state (visible/hidden) and save them using QSettings and their objectName and restore their state on startup again. So this is only called twice during the lifetime of the program. What bears the most value to me is painless maintainance. Of course manually adding the Toolbars to a list is the most straightforward and cleanest solution but if the number of Toolbars grows it may become a little tedious to do that. But I wouldn't be surprised to find out that this approach to saving the sates of the toolbars is flawed in the first place :-P .

Thanx

spud
24th September 2007, 23:53
Oh, but Qt 4 has a function for that: QMainWindow::saveState().

momesana
25th September 2007, 00:07
Oh, but Qt 4 has a function for that: QMainWindow::saveState().
Oh :-D. Thanks for the hint. :o