Results 1 to 7 of 7

Thread: dynamically unknown size of label array

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 09:54. Reason: missing [code] tags

Similar Threads

  1. Replies: 7
    Last Post: 29th September 2014, 14:31
  2. Replies: 1
    Last Post: 11th July 2014, 14:01
  3. Replies: 2
    Last Post: 5th September 2013, 15:29
  4. two dimensional array, size determined dynamically
    By feraudyh in forum General Programming
    Replies: 9
    Last Post: 10th June 2010, 03:00
  5. Replies: 1
    Last Post: 24th October 2006, 16: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.