2

I have declared a QLabel array (which i will be using as picture boxes) and the function call which is going to fill the array in the header file of the project as:

void fillArray();
QLabel *label_array[7];

Then i called the declared function in the main(). That function is going to fill the array and add the labels to the QTabWidget tab's body. In the source file i wrote the following:

void MainWindow::fillArray()
{
    for (int i = 0; i < sizeof(MainWindow::label_array) - 1; i++)
    {
        MainWindow::label_array[i] = new QLabel("Label " + i + 1);
        ui->tabWgt->widget(0)->layout()->addWidget(MainWindow::label_array[i])
    }
}

But the problem is it returns an error at the moment i build the program. I tried creating a layout and adding it to the layout and setting the tab's layout to that layout, it works, but it gets really messy and it is not understandable.

void MainWindow::fillArray()
{
    QHBoxLayout *layout = new QHBoxLayout;
    for (int i = 0; i < sizeof(MainWindow::label_array) - 1; i++)
    {
        MainWindow::label_array[i] = new QLabel("Label " + i + 1);
        MainWindow::label_array[i]->move(10, 5*i); // this doesn't work...
        layout->addWidget(MainWindow::label_array[i]);
    }
    ui->tabWgt->widget(0)->setLayout(layout);
}

Is it possible to add the labels directly to the tab's body without having to declare a layout (since i'd need to do it to every created array i declare along my program, and i have no idea how they are being located)?

How to position the label correctly?

Devenis
  • 23
  • 5

2 Answers2

0

Is it possible to add the labels directly to the tab's body without having to declare a layout (...) ?

No, because by default widget don't have a layout and you have to create it. But you can do it more clear:

void MainWindow::fillArray()
{
    /* set layout for tab */
    ui->tabWgt->widget(0)->setLayout(new QVBoxLayout); 

    /* calculate size of array */
    const uint array_size = sizeof(MainWindow::label_array)/sizeof(MainWindow::label_array[0]);

    /* fill tab widget */
    for (uint i = 0; i < array_size; i++)
    {
        MainWindow::label_array[i] = new QLabel(QString("Label %1").arg(i+1));
        ui->tabWgt->widget(0)->layout()->addWidget(MainWindow::label_array[i]);
    }
}

How to position the label correctly?

Here is an article which describe how to use different layouts for Qt 4.8 or here for Qt5.3.

Blueman
  • 781
  • 10
  • 13
0

In no case will you need to allocate the labels explicitly on the heap. You should store them directly as a member array.

With C++11, you should use range-based for to enumerate the safe, zero-overhead std::array.

Otherwise, use the C-style array and the sizeof(array)/sizeof(array[0]) idiom to determine its size from within the setup method.

Having written a whole bunch of QWidget based code, I now generally consider child widgets explicitly allocated on the heap in the widget's constructor to be premature pessimizations. There's rarely any reason to write the code that way. The widgets already allocate their implementation data on the heap (the pimpl idiom). When you allocate this skinny, pointer-sized QWidget subclass on the heap, you're simply doubling the number of heap allocations for no good reason at all. Don't.

#include <QApplication>
#include <QLabel>
#include <QVBoxLayout>
#include <array>

class WidgetCpp11 : public QWidget {
    QVBoxLayout m_layout;
    std::array<QLabel, 3> m_labels; // No overhead compared to `QLabel m_labels[3]`
    void setupLabels() {
        int n = 1;
        for (QLabel & label: m_labels) {
            label.setText(QString("Label %1").arg(n++));
            m_layout.addWidget(&label);
        }
    }
public:
    WidgetCpp11(QWidget * parent = 0) : QWidget(parent), m_layout(this) {
        setupLabels();
    }
};

class WidgetCpp98 : public QWidget {
    QVBoxLayout m_layout;
    QLabel m_labels[3];
    void setupLabels() {
        for (uint i = 0; i < sizeof(m_labels)/sizeof(m_labels[0]); ++i) {
            m_labels[i].setText(QString("Label %1").arg(i+1));
            m_layout.addWidget(m_labels+i);
        }
    }
public:
    WidgetCpp98(QWidget * parent = 0) : QWidget(parent), m_layout(this) {
        setupLabels();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    WidgetCpp98 w;
    w.show();
    return a.exec();
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313