Qt-interest Archive, March 2008
Serializing QTreeWidget to/from XML and clear QGridLayout
Message 1 in thread
Hey! I've got two questions.
1.) I just wondered what would be the best (cleanest) way to load/store
the content of a QTreeWidget to/from a XML file. Currently the
parent-hierarchy makes my approrach slow and ugly, because for each new
item I load, I have to find its parent.
2.) In my application I have a QGridLayout which displays widgets for
editing parameters. Sometimes I need to clear all widgets and
sub-layouts from the QGridLayout and add new items to it. I discovered,
that after removing all items, the QGridLayout::rowCount() function
doesn't return 0, but the old number of rows. Is this a bug? I think so,
but maybe my layout-clearing code is wrong. Ich just set up some code to
illustrate this.
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QGridLayout>
#include <QtGui/QPushButton>
#include <QtGui/QFrame>
void clearLayout(QLayout *layout)
{
QLayoutItem *item = 0;
while ((item = layout->itemAt(0)) != 0)
{
if (item->layout())
{
clearLayout(item->layout());
delete item->layout();
}
else if (item->widget())
{
delete item->widget();
}
layout->removeItem(item);
}
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow mainWnd;
mainWnd.resize(200, 200);
QFrame frame(&mainWnd);
frame.setGeometry(mainWnd.geometry());
frame.show();
QGridLayout vbl(&frame);
for (int i=0; i<10; i++)
{
QPushButton *btn = new QPushButton;
btn->setFixedHeight(25);
vbl.addWidget(btn, i/2, i%2);
}
clearLayout(&vbl);
Q_ASSERT(vbl.rowCount() == 0);
app.setActiveWindow(&mainWnd);
mainWnd.show();
return app.exec();
}
Best regards and thanks, David
<http://dict.leo.org/ende?lp=ende&p=eL4jZr&search=hierarchy>
Message 2 in thread
On Tuesday 11 March 2008 09:56:01 David Geier wrote:
> Hey! I've got two questions.
>
> 1.) I just wondered what would be the best (cleanest) way to load/store
> the content of a QTreeWidget to/from a XML file. Currently the
> parent-hierarchy makes my approrach slow and ugly, because for each new
> item I load, I have to find its parent.
QxmlStreamWriter would be the best for that task. it is recursive.
note that this will always be slow and should not be used for network.
in what way do you have to find the new parent? QxmlStreamReader is recuriosve
too, so you can just store the last parent on the stack. Of course you'd have
to store your data in a tree structure (thats the point of xml btw), like
<item>
<item>
<item>
</item>
</item>
</item>
> 2.) In my application I have a QGridLayout which displays widgets for
> editing parameters. Sometimes I need to clear all widgets and
> sub-layouts from the QGridLayout and add new items to it. I discovered,
> that after removing all items, the QGridLayout::rowCount() function
> doesn't return 0, but the old number of rows. Is this a bug? I think so,
> but maybe my layout-clearing code is wrong. Ich just set up some code to
> illustrate this.
for http-like gui navigation i suggest using QStackWidget instead of
deleting/rebuilding (or QAssistant if you want tà do it properly).
Layouts are nasty and not really the right choice for that task.
--
[ signature omitted ]
Message 3 in thread
On Tuesday 11 March 2008 09:56:01 David Geier wrote:
> Hey! I've got two questions.
>
> 1.) I just wondered what would be the best (cleanest) way to load/store
> the content of a QTreeWidget to/from a XML file. Currently the
> parent-hierarchy makes my approrach slow and ugly, because for each new
> item I load, I have to find its parent.
There is a widget loader: see QUILoader. Maybe that's a nice start.
<SNIP />
Happy coding,
Eric
--
[ signature omitted ]