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.
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.