Segfault when initializing pointer array of QPushButton
-
When I try to initialize an array of QPushButton, I get a segfault error.
Here, I declare the QPushButton variables in a header file.
QPushButton *num[12];
In a source file, I initialize the array and add it to a layout, which is added to a larger layout:
for (int i = 0; i < 12; i++) { //initializes and sets labels to numbers 1-12 num[i] = new QPushButton(QString::number(i+1)); //*QHBoxLayout selectLayout->addWidget(num[i]); } //*QGridLayout layout->addLayout(selectLayout, 2, 0, 2, 12, Qt::AlignCenter); setLayout(layout);
I have also tried initializing and then setting the text:
for (int i = 0; i < 12; i++) { //initializes and sets labels to numbers 1-12 num[i] = new QPushButton(); num[i]->setText(QString::number(i+1)); //*QHBoxLayout selectLayout->addWidget(num[i]); }
When executing code, I get
Segmentation Fault (Signal SIGSEGV)
no matter how I write the code. I've also tried the 2nd variation withthis
pointers and initializing outside of the loop, but none of these have worked.I'm fairly new to Qt and do not how to fix the error. Can anybody help?
-
Are you sure the crash is within the loop? Where are
selectLayout
andnum
declared?Why do you need num at all, i.e. are you accessing your buttons from some other place?
-
@aha_1980
selectLayout
andnum
are declared in this class:#ifndef UI_H #define UI_H #include <QWidget> class QLabel; class QPushButton; class QGridLayout; class QHBoxLayout; class QVBoxLayout; class ui : public QWidget { Q_OBJECT public: explicit ui(QWidget *parent = nullptr); private: //layouts QGridLayout *layout; QHBoxLayout *selectLayout; QGridLayout *titleWidget; //fonts QFont *fTitle; QFont *fTitleMini; QFont *fInfo; //labels QLabel *title; QLabel *titleMini; QLabel *info; //buttons QPushButton *num[12]; QPushButton *rollButton; signals: public slots: }; #endif // UI_H
The buttons are not being accessed anywhere. They are just being initialized and displayed.
-
@ryannasaurus said in Segfault when initializing pointer array of QPushButton:
QPushButton *num[12];
This is a pointer to an array (whyever) and the pointer is not initialized...
-
@Christian-Ehrlicher
I have it set to initialize in this for-loop here:for (int i = 0; i < 12; i++) { //initializes and sets labels to numbers 1-12 num[i] = new QPushButton(QString::number(i+1)); }
I tried to add a constructor to initialize in the header file itself but it kept returning errors. It is ok to initialize it in the respective source file like I have done here?
-
Sorry I misread the C declaration. I would use QVector <QPushButton*> num; instead. And there is no need to hold the font as pointer - QFont fTitle; etc is perefectl valid.
The crash must be somewhere else - I would suggest using a Debugger to take a look where it really crashes. -
hi @ryannasaurus said in Segfault when initializing pointer array of QPushButton:
is selectLayout defined? From what you pasted, I only see the pointer declaration and addWidget calls on it. Did you
new
anywhere ? -
This post is deleted!