Results 1 to 3 of 3

Thread: C++ Qt5.12.1 - variable length array of QLabels

  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default C++ Qt5.12.1 - variable length array of QLabels

    Hello,

    Qt Creator 4.8.2

    Qt Code:
    1. int qty = 6;
    2. QLabel *topLabel[qty], *numLabel[qty];
    3.  
    4. for( int i = 0; i < qty; i++)
    5. {
    6. topLabel[i] = newQLabel(topLabel[i]);
    7. topLabel[i]->setGeometry(20+(i*108),20,102,67);
    8. topLabel[i]->setFrameStyle(QFrame::StyledPanel|QFrame::Sunken);
    9. topLabel[i]->setLineWidth(4);
    10. numLabel[i] = newQLabel(topLabel[i]);
    11. numLabel[i]->setGeometry(2,86,100,70);
    12. numLabel[i]->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    13. numLabel[i]->setStyleSheet("QLabel{color:blue;}");
    14. numLabel[i]->setFont(*font);
    15. }
    To copy to clipboard, switch view to plain text mode 
    header
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class test : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. test(QWidget *parent = nullptr);
    12. ~test();
    13. private:
    14. void tDelay(int milliSecs);
    15. QList<uint> getList(int maxNum, int minNum, int qty);
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 
    pro file
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = test
    6. TEMPLATE = app
    7.  
    8. SOURCES += main.cpp\
    9. test.cpp
    10.  
    11. HEADERS += test.h
    12.  
    13. CONFIG += c++11
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,190
    Thanks
    36
    Thanked 1,489 Times in 1,441 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Qt5.12.1 - variable length array of QLabels

    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,
    _

  3. #3
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Qt5.12.1 - variable length array of QLabels

    Hello,

    Quote Originally Posted by anda_skoa View Post
    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

Similar Threads

  1. TypeError: Cannot read property 'length' of undefined in QML array
    By TheIndependentAquarius in forum Qt Quick
    Replies: 6
    Last Post: 12th June 2014, 07:39
  2. Replies: 3
    Last Post: 25th February 2012, 23:25
  3. save variable size hex string into uchar array
    By amika in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2011, 23:35
  4. Replies: 1
    Last Post: 30th November 2007, 11:03
  5. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 15:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.