Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Declaring an array from const int

Declaring an array from const int

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 477 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saa_
    wrote on last edited by
    #1

    Hello

    I am trying to make a grid of buttons, using a widget application. In the Widget class I have added two private const int for how many rows and columns:

    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
    
    private:
        QSize sizeHint() const;
        const int windowWidth = 600;
        const int windowHeight = 600;
        const int columns = 8;
        const int rows = 8;
    
    };
    

    In the constructor for widget class I then initialize an array of pointer to QPushButton using the constants columns and rows:

    QPushButton * buttons[columns][rows];
    

    At compile time this leads to an error:
    "expression does not evaluate to a constant,
    failure was caused by a read of a variable outside its lifetime,
    see usage of this"

    I know that you cannot initialize an array using variables, but it should be possible using constants. I tried to do the same outside QT (with an array of pointers to integers) and it worked fine.

    So is this problem something specific to QT, or is there something fundamental that I am missing?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @saa_ said in Declaring an array from const int:

      I tried to do the same outside QT (with an array of pointers to integers) and it worked fine.

      But only with a gcc compiler and enabled gcc extensions.
      Use constexpr instead const to make it a real compile-time constant.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        You mean someting like this?

        std::vector<QPushbutton*> myButtons(columns * rows, nullptr);
        y = 4; // [0..7]
        x = 1; // [0..7]
        myButtons[ columns * y + x ] = new QPushbutton();
        

        or any of the dynamic containers that could serve you well.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved