PDA

View Full Version : dynamically unknown size of label array



binary001
7th March 2015, 10:33
Hi,

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



#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QLabel>

class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget(QWidget *parent = 0);
~MyWidget();

private:
QLabel *mylabel[5];

};

#endif // MYWIDGET_H




#include "mywidget.h"

#include <QLabel>
#include <QVBoxLayout>

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
int i;

for(i=0;i<5;i++)
{
mylabel[i] = new QLabel("Title: " + QString::number(i+1));
layout->addWidget(mylabel[i]);
}
this->setLayout(layout);

}



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:


#include "mywidget.h"

int main(int argc, char *argv[])
{
MyWidget mytitle(3); // create 3 labels
MyWidget mysubtitle(10); // create 10 labels


return 0;
}


Another Question is Array can resize and created programmatically.
I created it in VB just like:


dim A() as string
.....

ReDim A(1)

Dim i as Integer
i = UBound(A) + 1
ReDim Preserve A(i)

If yes, how to do it?

Thanks in advance.

Radek
7th March 2015, 11:05
(1) Make the number of labels a parameter of MyWidget ctor:


class MyWidget : public QWidget
{
Q_OBJECT

public :
MyWidget( int howmany, QWidget *parent = 0 );

...
};

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.

binary001
7th March 2015, 11:27
thanks for your replay.

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



class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget(int howmany, QWidget *parent = 0);
~MyWidget();

private:
int n;
QLabel *mylabel[n];

};

#endif // MYWIDGET_H




#include "mywidget.h"

#include <QLabel>
#include <QVBoxLayout>

MyWidget::MyWidget(int howmany, QWidget *parent)
: QWidget(parent), h(howmany)
{
QVBoxLayout *layout = new QVBoxLayout;
int i;

for(i=0;i<n;i++)
{
mylabel[i] = new QLabel("Title: " + QString::number(i+1));
layout->addWidget(mylabel[i]);
}
this->setLayout(layout);

}



compiler give error:
mywidget.h:39: error: invalid use of non-static data member 'MyWidget::n'
int n;
^

Lesiok
7th March 2015, 12:08
Don't use an array. Use QVector or QList as Radek said. Both of them don't need initial size.

binary001
7th March 2015, 14:03
Thanks Radek and Lesiok.

I changed from array to QList. I got it.

Radek
7th March 2015, 18:17
(1) The error: You cannot declare a "plain" array with a variable size


private:
int n;
QLabel *mylabel[n]; // compile error

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.

anda_skoa
8th March 2015, 09:56
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,
_