PDA

View Full Version : Function returning a QHBoxLayout



aomegax
10th August 2010, 18:04
Hi, I want to do the following dialog:

http://img825.imageshack.us/img825/9126/schermata3v.png

now I have done a function which create an entry item, but I don't know how return "widget" or "layout" to allow to add them to a QGridLayut...



??? GestioneClienteDialog::creaItemCliente(Domain::Per sona c)
{
??? widget = new ???; // widget or layout? what type? and how?

QHBoxLayout *boxEsterno = new QHBoxLayout();
QVBoxLayout *boxContenitore = new QVBoxLayout();
QHBoxLayout *boxDati = new QHBoxLayout();

QRadioButton *radio = new QRadioButton();
QLabel *piva = new QLabel();
QLabel *ragionesociale = new QLabel();
QLabel *cognome = new QLabel();
QLabel *nome = new QLabel();
QLabel *indirizzo = new QLabel();
QLabel *citta = new QLabel();

piva->setVisible(false);

piva->setText(c.piva());
ragionesociale->setText(c.ragionesociale());
cognome->setText(c.cognome());
nome->setText(c.nome());
indirizzo->setText(c.indirizzo());
citta->setText(c.citta());

boxDati->addWidget(cognome);
boxDati->addWidget(nome);
boxDati->addWidget(indirizzo);
boxDati->addWidget(citta);

boxContenitore->addWidget(ragionesociale);
boxContenitore->addLayout(boxDati);

boxEsterno->addWidget(radio);
boxEsterno->addLayout(boxContenitore);

widget->addWHAT(boxEsterno);

return widget;
}



Thank you...

Lykurg
10th August 2010, 18:18
It depends on how you want use it, but normally a widget is a good choice:

QWidget* GestioneClienteDialog::creaItemCliente(Domain::Per sona c)
{
QWidget* widget = new QWidget();
//your code like
widget->setLayout(boxEsterno)
return widget;

Make sure that the receiver sets a valid parent for the widget.

Netheril
10th August 2010, 19:24
Hi aomegax, I think all depends of what you wish to do with the returned type. If you are going to used as a regular QWidget and add it to another Layout (for example, QHBoxLayout or QVBoxLayout, etc), then of course you should return a QWidget. But, if you wish to use the returned type as a container of QWidgets, or you are going to add more elements after call creaItemCliente; would be better to use a Layout as the returned type.

It's also important to note, the Parent-Child relationship between the components. Remember that if you call setLayout on a QWidget, all the components that you added to the QWidget via the QLayout, will be childs of the QWidget, not the QLayout.
So, it would be thoughtful if you have into account both of the recommendation I gave you.

I Hope this answers your question.

aomegax
12th August 2010, 10:57
Hi, thanks everybody to help me...
I have used both your informations...
At the end I have used layout because I must add them to a gridLayout...
now only one question...If I do:



while (grid->layout()->count() > 0)
{
QLayoutItem *l = grid->layout()->takeAt(0);
if (l->layout())
{
delete l->layout();
}
delete l;
}

why the items added before isn't deleted? how can I do to clear gridLayout from all the layouts which I add in a first moment?


however thank you!

aomegax
16th August 2010, 11:39
can anybody help me?

Urthas
16th August 2010, 18:19
http://www.qtcentre.org/threads/32951-How-delete-contents-in-QGridLayout?highlight=delete+layout