Results 1 to 7 of 7

Thread: dynamically unknown size of label array

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default dynamically unknown size of label array

    Hi,

    I'm a newbie for Qt C++. I want to create the QLabels Array programmatically.
    It's ok. code is as follow:

    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6.  
    7. class MyWidget : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MyWidget(QWidget *parent = 0);
    13. ~MyWidget();
    14.  
    15. private:
    16. QLabel *mylabel[5];
    17.  
    18. };
    19.  
    20. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. #include <QLabel>
    4. #include <QVBoxLayout>
    5.  
    6. MyWidget::MyWidget(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. QVBoxLayout *layout = new QVBoxLayout;
    10. int i;
    11.  
    12. for(i=0;i<5;i++)
    13. {
    14. mylabel[i] = new QLabel("Title: " + QString::number(i+1));
    15. layout->addWidget(mylabel[i]);
    16. }
    17. this->setLayout(layout);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    This code created 5 label
    But I want to create labels that numbers is assigned when I call class.
    My Question is how to assign number of labels to create dynamically?

    just example:
    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. MyWidget mytitle(3); // create 3 labels
    6. MyWidget mysubtitle(10); // create 10 labels
    7.  
    8.  
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Another Question is Array can resize and created programmatically.
    I created it in VB just like:
    Qt Code:
    1. dim A() as string
    2. .....
    3.  
    4. ReDim A(1)
    5.  
    6. Dim i as Integer
    7. i = UBound(A) + 1
    8. ReDim Preserve A(i)
    To copy to clipboard, switch view to plain text mode 
    If yes, how to do it?

    Thanks in advance.
    Last edited by anda_skoa; 8th March 2015 at 10:54. Reason: missing [code] tags

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: dynamically unknown size of label array

    (1) Make the number of labels a parameter of MyWidget ctor:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public :
    6. MyWidget( int howmany, QWidget *parent = 0 );
    7.  
    8. ...
    9. };
    To copy to clipboard, switch view to plain text mode 
    then you can use the syntax shown in your post.

    (2) Use QVector<MyWidget *> instead of a "plain" array. You'll be able to resize or remove existing vector items.

  3. #3
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: dynamically unknown size of label array

    thanks for your replay.

    eg. how to assign parameter (eg howmany) as array size?
    I change code as follow: It gives error.

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyWidget(int howmany, QWidget *parent = 0);
    7. ~MyWidget();
    8.  
    9. private:
    10. int n;
    11. QLabel *mylabel[n];
    12.  
    13. };
    14.  
    15. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. #include <QLabel>
    4. #include <QVBoxLayout>
    5.  
    6. MyWidget::MyWidget(int howmany, QWidget *parent)
    7. : QWidget(parent), h(howmany)
    8. {
    9. QVBoxLayout *layout = new QVBoxLayout;
    10. int i;
    11.  
    12. for(i=0;i<n;i++)
    13. {
    14. mylabel[i] = new QLabel("Title: " + QString::number(i+1));
    15. layout->addWidget(mylabel[i]);
    16. }
    17. this->setLayout(layout);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    compiler give error:
    mywidget.h:39: error: invalid use of non-static data member 'MyWidget::n'
    int n;
    ^
    Last edited by anda_skoa; 8th March 2015 at 10:55. Reason: changed [quote] to [code]

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamically unknown size of label array

    Don't use an array. Use QVector or QList as Radek said. Both of them don't need initial size.

  5. #5
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: dynamically unknown size of label array

    Thanks Radek and Lesiok.

    I changed from array to QList. I got it.

  6. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: dynamically unknown size of label array

    (1) The error: You cannot declare a "plain" array with a variable size
    Qt Code:
    1. private:
    2. int n;
    3. QLabel *mylabel[n]; // compile error
    To copy to clipboard, switch view to plain text mode 
    Tne n needed be an initialized const int.

    (2) IMO, QVector is better here. QVector is a dynamic array while QList is a linked list. Nevertheless both of them will serve you good.

  7. The following user says thank you to Radek for this useful post:

    binary001 (8th March 2015)

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamically unknown size of label array

    QList is not a linked list, but I agree that QVector is more appropriate here since the length is already known at creation time.

    Cheers,
    _

Similar Threads

  1. Replies: 7
    Last Post: 29th September 2014, 15:31
  2. Replies: 1
    Last Post: 11th July 2014, 15:01
  3. Replies: 2
    Last Post: 5th September 2013, 16:29
  4. two dimensional array, size determined dynamically
    By feraudyh in forum General Programming
    Replies: 9
    Last Post: 10th June 2010, 04:00
  5. Replies: 1
    Last Post: 24th October 2006, 17:40

Tags for this Thread

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.