Exception when reading widget store on QVector
-
Why when i write like the first example i get an exception in the
layout->addWidgetline
and when i write like the second example i get no exception?//1 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl; QLabel* lbl2; QVector<QLabel*> lbl_vec { lbl, lbl2 }; //... } for (int i = 0; i < 2; i++) { lbl_vec[i] = new QLabel(this); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception//2 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl = new QLabel(this); QLabel* lbl2 = new QLabel(this); QVector<QLabel*> lbl_vec { lbl, lbl2}; //... } for (int i = 0; i < 2; i++) { lbl_vec[i]->setText("Loading"); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exceptionAlso, is it wrong to initialize a label in a subclass header like this?
public: QLabel* lbl = new QLabel(this); -
Why when i write like the first example i get an exception in the
layout->addWidgetline
and when i write like the second example i get no exception?//1 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl; QLabel* lbl2; QVector<QLabel*> lbl_vec { lbl, lbl2 }; //... } for (int i = 0; i < 2; i++) { lbl_vec[i] = new QLabel(this); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception//2 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl = new QLabel(this); QLabel* lbl2 = new QLabel(this); QVector<QLabel*> lbl_vec { lbl, lbl2}; //... } for (int i = 0; i < 2; i++) { lbl_vec[i]->setText("Loading"); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exceptionAlso, is it wrong to initialize a label in a subclass header like this?
public: QLabel* lbl = new QLabel(this);@n34rt said in Exception when reading widget store on QVector:
QVector<QLabel*> lbl_vec { lbl, lbl2 };
At that time lb1 and lb2 are invalid pointers! So, if you use them your app will crash.
-
Why when i write like the first example i get an exception in the
layout->addWidgetline
and when i write like the second example i get no exception?//1 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl; QLabel* lbl2; QVector<QLabel*> lbl_vec { lbl, lbl2 }; //... } for (int i = 0; i < 2; i++) { lbl_vec[i] = new QLabel(this); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception//2 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl = new QLabel(this); QLabel* lbl2 = new QLabel(this); QVector<QLabel*> lbl_vec { lbl, lbl2}; //... } for (int i = 0; i < 2; i++) { lbl_vec[i]->setText("Loading"); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exceptionAlso, is it wrong to initialize a label in a subclass header like this?
public: QLabel* lbl = new QLabel(this);@n34rt said in Exception when reading widget store on QVector:
layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exceptionlayout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exceptionDid you run your code in a debugger to answer this? In the first case (
QLabel* lbl;)lblis an uninitialized variable so it "crashes", in the second case (QLabel* lbl = new QLabel(this);)lblis set to aQLabelso it does not. Don't know what else you would expect. There is no relevance to theQVectoryou mention in your title or yourlbl_vec.QLabel* lbl = new QLabel(this);You can put that in a class declaration/header if you wish. Personally I would not: I would do the
lbl = new QLabel(this);in the class's constructor code. -
Why when i write like the first example i get an exception in the
layout->addWidgetline
and when i write like the second example i get no exception?//1 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl; QLabel* lbl2; QVector<QLabel*> lbl_vec { lbl, lbl2 }; //... } for (int i = 0; i < 2; i++) { lbl_vec[i] = new QLabel(this); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception//2 class SpinnerButton : public QPushButton { Q_OBJECT public: QLabel* lbl = new QLabel(this); QLabel* lbl2 = new QLabel(this); QVector<QLabel*> lbl_vec { lbl, lbl2}; //... } for (int i = 0; i < 2; i++) { lbl_vec[i]->setText("Loading"); //... } QGridLayout* layout = new QGridLayout(); this->setLayout(layout); layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exceptionAlso, is it wrong to initialize a label in a subclass header like this?
public: QLabel* lbl = new QLabel(this);@n34rt said in Exception when reading widget store on QVector:
QVector<QLabel*> lbl_vec { lbl, lbl2 };
At that time lb1 and lb2 are invalid pointers! So, if you use them your app will crash.