QVector< QVBoxLayout * > crashing on adding layout
-
I'm trying to make a QStackedWidget with 5 labels and text inputs on each of the 10 pages. Like this:

So far I've got:int ss=0, s=0; ui->stck->setCurrentIndex(0); QVector< QWidget * > wdgVector(10); QVector< QVBoxLayout * > strVector(10); for (int i=0; i<50; i++){ if (5<=s) { wdgVector[ss]->setLayout(strVector[ss]); ui->stck->insertWidget(ss, wdgVector[ss]); ss++; s=0; } QLabel *lbl = new QLabel(this); QHBoxLayout *hlay = new QHBoxLayout(); QLineEdit *txt = new QLineEdit(this); hlay->addWidget(lbl); hlay->addWidget(txt); s++; strVector[ss]->addLayout(hlay);//<----CRASH }It's crashing on the last bit of code and I'm unsure how to fix it.
-
I'm trying to make a QStackedWidget with 5 labels and text inputs on each of the 10 pages. Like this:

So far I've got:int ss=0, s=0; ui->stck->setCurrentIndex(0); QVector< QWidget * > wdgVector(10); QVector< QVBoxLayout * > strVector(10); for (int i=0; i<50; i++){ if (5<=s) { wdgVector[ss]->setLayout(strVector[ss]); ui->stck->insertWidget(ss, wdgVector[ss]); ss++; s=0; } QLabel *lbl = new QLabel(this); QHBoxLayout *hlay = new QHBoxLayout(); QLineEdit *txt = new QLineEdit(this); hlay->addWidget(lbl); hlay->addWidget(txt); s++; strVector[ss]->addLayout(hlay);//<----CRASH }It's crashing on the last bit of code and I'm unsure how to fix it.
@Inzinejkr said in QVector< QVBoxLayout * > crashing on adding layout:
strVector[ss]->addLayout(hlay);//<----CRASH
You never insert any value into
strVector. The vector is empty so it will always crash when you try to access an element of it. -
@sierdzio said in [QVector< QVBoxLayout * > crashing on adding layout]
You never insert any value into
strVector.So how would I do that to achieve the image shown?
wdgVector[ss] = new QWidget(); wdgVector[ss]->setLayout(strVector[ss]);But you also have to initialise strVector.
QVector< QVBoxLayout * > strVector(10); // This only declares a vector containing pointers - you have to create actual objects and put their pointers there. C++ basics. -
QStackedWidget *parent = ... something; for (int i = 0; i < 10; ++i) { QVBoxLayout *vertical = new QVBoxLayout; for (int y = 0; y < 5; ++y) { QHBoxLayout *row = new QHBoxLayout; row->addWidget(new QLabel(tr("TextLabel"))); row->addWidget(new QLineEdit); vertical->addLayout(row); } QWidget *page = new QWidget; page->setLayout(vertical); parent->addWidget(page); }(untested pseudocode ;-))
-
@sierdzio said in QVector< QVBoxLayout * > crashing on adding layout:
(untested pseudocode ;-))
Alright, it does what I wanted, so good job! :)
Just for the beauty I thought about inserting the label and text input in a group box, but I can't get that to work neither.int z=0; for (int i = 0; i < 3; i++) { QVBoxLayout *vertical = new QVBoxLayout; for (int y = 0; y < 5; y++) { QGroupBox *grp=new QGroupBox(); QVBoxLayout *vlay=new QVBoxLayout; grp->setTitle("Something number: "+QString::number(z)); vlay->addWidget(new QLabel("Label number: ("+QString::number(z))); vlay->addWidget(new QLineEdit); vertical->addLayout(vlay); grp->setLayout(vertical); z++; } QWidget *page = new QWidget; page->setLayout(vertical); ui->stck->insertWidget(i, page); ui->stck->setCurrentIndex(0); }Sorry for bothering you..
-
@sierdzio said in QVector< QVBoxLayout * > crashing on adding layout:
(untested pseudocode ;-))
Alright, it does what I wanted, so good job! :)
Just for the beauty I thought about inserting the label and text input in a group box, but I can't get that to work neither.int z=0; for (int i = 0; i < 3; i++) { QVBoxLayout *vertical = new QVBoxLayout; for (int y = 0; y < 5; y++) { QGroupBox *grp=new QGroupBox(); QVBoxLayout *vlay=new QVBoxLayout; grp->setTitle("Something number: "+QString::number(z)); vlay->addWidget(new QLabel("Label number: ("+QString::number(z))); vlay->addWidget(new QLineEdit); vertical->addLayout(vlay); grp->setLayout(vertical); z++; } QWidget *page = new QWidget; page->setLayout(vertical); ui->stck->insertWidget(i, page); ui->stck->setCurrentIndex(0); }Sorry for bothering you..
@Inzinejkr said in QVector< QVBoxLayout * > crashing on adding layout:
grp->setLayout(vertical);
You are creating multiple
QGroupBoxes in your loop, yet you set each one to have the sameQVBoxLayout *vertical = new QVBoxLayout;instance. And you later dopage->setLayout(vertical);. Something wrong here! -
@Inzinejkr said in QVector< QVBoxLayout * > crashing on adding layout:
grp->setLayout(vertical);
You are creating multiple
QGroupBoxes in your loop, yet you set each one to have the sameQVBoxLayout *vertical = new QVBoxLayout;instance. And you later dopage->setLayout(vertical);. Something wrong here!@JonB said in QVector< QVBoxLayout * > crashing on adding layout:
You are creating multiple
QGroupBoxes in your loop, yet you set each one to have the sameQVBoxLayout *vertical = new QVBoxLayout;instance. And you later dopage->setLayout(vertical);. Something wrong here!Well, I guess? I'm trying to group up one label and one textbox..

So this, just via code -
@JonB said in QVector< QVBoxLayout * > crashing on adding layout:
You are creating multiple
QGroupBoxes in your loop, yet you set each one to have the sameQVBoxLayout *vertical = new QVBoxLayout;instance. And you later dopage->setLayout(vertical);. Something wrong here!Well, I guess? I'm trying to group up one label and one textbox..

So this, just via code@Inzinejkr
I can only say: start by creating separate instances of all yourQVBoxLayouts: a given instance must only be used as a layout for a widget once, not re-used for multiple widgets as you presently show in your code.