PDA

View Full Version : Error when QVBoxLayout is overflowed



emil0076
13th February 2011, 21:24
Hi, I have problem with QVBoxLayout (and other Q...Layout objects).
1. I have QTabWidget, I add tab with set widget QScrollArea.
2. QScrollArea has set widget QWidget.
3. QWidget has set layout QVBoxLayout.
4. I make array of pointers to QLabel, next in "for" loop I make objects of QLabel, add pointers to array and add objects to QVBoxLayout.

(...)
QTabWidget *tabWidget;
QScrollArea *scrollArea1;
QWidget *widget1;
QLayout *layoutTab1;
QLabel **labels;
(...)
this->tabWidget = new QTabWidget();
this->scrollArea1 = new QScrollArea(this->tabWidget);
this->widget1 = new QWidget(this->scrollArea1);
this->layoutTab1 = new QVBoxLayout(widget1);

this->widget1->setLayout(this->layoutTab1);
this->scrollArea1->setWidgetResizable(true);
this->scrollArea1->setWidget(this->widget1);
this->tabWidget->insertTab(0, this->scrollArea1, tr(TAB1_));
*(this->labels) = new QLabel[10];

for(int i=0; i<=9; i++)
{
labels[i] = new QLabel(tr("TEST"));
labels[i]->setFrameStyle(1);
labels[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
this->layoutTab1->addWidget(this->labels[i]);
}

My problem is:
When number of labels is over 5 they are outside app window (scrolling in QScrollArea works perfectly) and (in debug mode) I get error after close app - "Application has stopped working". Released app doesn't run, this same error is before start. If number of QLabels is less (all can be in window) everything works good.
I think this is problem with QVBoxLayout because I comment everything except making object of QVBoxLayout, making array of objects QLabel and adding them to QVBoxLayout and problem is this same.
What's wrong? Please help.

high_flyer
13th February 2011, 21:50
this code:


QLabel **labels;
...
*(this->labels) = new QLabel[10];

for(int i=0; i<=9; i++)
{
labels[i] = new QLabel(tr("TEST"));
labels[i]->setFrameStyle(1);
labels[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
this->layoutTab1->addWidget(this->labels[i]);
}


is very problematic.

It seems you are probably coming from a C background, but don't understand what an object is.

Put all your labels in a container, a QList or QVector.


//in the class header make a member
QList<QLabel*> m_labels;
...
//in the implementation then you do:
for(int i=0; i<=9; i++)
{
QLabel *pLabel = new QLabel(this,tr("TEST")); //if you don't give the label a parent, you have to make sure to delete it your self!
pLabel ->setFrameStyle(1);
pLabel ->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
this->layoutTab1->addWidget(pLabel );
m_lables.push_back(pLabel);
}
...
//To access label you do:
m_labels.at(index)->setText("some text");


Also, you have made your pointer local in the method - so you wont have access to them once the method is out of scope.
So in my example the list should be a member.

emil0076
14th February 2011, 17:00
It works! Thank you.