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>
{
Q_OBJECT
public:
~MyWidget();
private:
};
#endif // MYWIDGET_H
#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
To copy to clipboard, switch view to plain text mode
#include "mywidget.h"
#include <QLabel>
#include <QVBoxLayout>
MyWidget
::MyWidget(QWidget *parent
){
int i;
for(i=0;i<5;i++)
{
layout->addWidget(mylabel[i]);
}
this->setLayout(layout);
}
#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);
}
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:
#include "mywidget.h"
int main(int argc, char *argv[])
{
MyWidget mytitle(3); // create 3 labels
MyWidget mysubtitle(10); // create 10 labels
return 0;
}
#include "mywidget.h"
int main(int argc, char *argv[])
{
MyWidget mytitle(3); // create 3 labels
MyWidget mysubtitle(10); // create 10 labels
return 0;
}
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:
dim A() as string
.....
ReDim A(1)
Dim i as Integer
i = UBound(A) + 1
ReDim Preserve A(i)
dim A() as string
.....
ReDim A(1)
Dim i as Integer
i = UBound(A) + 1
ReDim Preserve A(i)
To copy to clipboard, switch view to plain text mode
If yes, how to do it?
Thanks in advance.
Bookmarks