PDA

View Full Version : C++ Qt5.12.1 - variable length array of QLabels



jimbo
10th March 2019, 13:33
Hello,

Qt Creator 4.8.2


int qty = 6;
QLabel *topLabel[qty], *numLabel[qty];

for( int i = 0; i < qty; i++)
{
topLabel[i] = newQLabel(topLabel[i]);
topLabel[i]->setGeometry(20+(i*108),20,102,67);
topLabel[i]->setFrameStyle(QFrame::StyledPanel|QFrame::Sunken);
topLabel[i]->setLineWidth(4);
numLabel[i] = newQLabel(topLabel[i]);
numLabel[i]->setGeometry(2,86,100,70);
numLabel[i]->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
numLabel[i]->setStyleSheet("QLabel{color:blue;}");
numLabel[i]->setFont(*font);
}
header
#ifndef TEST_H
#define TEST_H

#include <QMainWindow>

class test : public QMainWindow
{
Q_OBJECT

public:
test(QWidget *parent = nullptr);
~test();
private:
void tDelay(int milliSecs);
QList<uint> getList(int maxNum, int minNum, int qty);
};

#endif
pro file

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test
TEMPLATE = app

SOURCES += main.cpp\
test.cpp

HEADERS += test.h

CONFIG += c++11
warning: variable length arrays are a C99 feature

It compiled without warnings on an earlier version of Qt.
Now the program compiles and runs OK, but with various warnings, this being one of them.
If I turn off clang, all warnings are gone.
Suggestions please.

Regards

anda_skoa
10th March 2019, 16:00
Arrays in C (and C++) are defined to have a fixed length, requiring the argument passed to [] to be a build-time constant (numeric literal, numeric constant, enum value, C++ const-expr).
E.g. in your case making qty a "const int"

If the length is only available at run time, then the obvious way in C++ is a vector, e.g. QVector or std::vector

Cheers,
_

jimbo
12th March 2019, 12:13
Hello,


Arrays in C (and C++) are defined to have a fixed length, requiring the argument passed to [] to be a build-time constant (numeric literal, numeric constant, enum value, C++ const-expr).
E.g. in your case making qty a "const int"

If the length is only available at run time, then the obvious way in C++ is a vector, e.g. QVector or std::vector

Cheers,
_

Thanks

Regards