Search code examples
qtargumentsqlist

Can't use element of a QList as argument. Save objects into list


What I have done

I created a List of QHBoxLayout's in my h-File and now I want to add my QVBoxLayout vLayout all elements of the List. I thought something like this would work:

At first I filled the list with some elements.

for(int i=0; i< COUNT; i++)
{
    // create box
    QHBoxLayout *hlayout_tmp = new QHBoxLayout;

    // label for the box
    QString name = "Channel ";
    name += i;
    name += ": ";
    QLabel* channel = new QLabel(name);

    // create slider
    QSlider* slider_tmp  = new QSlider(Qt::Vertical, this);
    sliders.append(*slider_tmp);

    // create scope
    ScopeWidget2* newScope = new ScopeWidget2(bufsize, maxValue, minValue);
    scopes.append(*newScope);

    // put widgets together
    hlayout_tmp->addWidget(channel);
    hlayout_tmp->addWidget(slider_tmp);
    hlayout_tmp->addWidget(newScope);
    hBoxes.append(*hlayout_tmp);
}

Then I wanted to add the hboxes to my vlayout.

// create column
QVBoxLayout* vlayout = new QVBoxLayout();

// put all layouts together  
vlayout->addLayout(hBoxes.at(0));
vlayout->addLayout(hBoxes.at(1));

And then I get the following error_ no matching function for call to 'QVBoxLayout::addLayout(const QHBoxLayout&)'vlayout->addLayout(hBoxes.at(0));

Now, as I write this question I understand why i get this message. But I have no clue, how to solve it.

Question:

Is it possible to save obejects into a list and not the const Type &value or cast the elemet into an object? So that I can use vlayout->addLayout(hBoxes.at(0));

Solution (So far it works):

Like

I try this:

For example:

// in the loop I had to change:
QSlider* slider_tmp  = new QSlider(Qt::Vertical, this);
sliders.append(slider_tmp); 

// And i can use the element with
vlayout->addLayout(hBoxes.at(0));

Correct me, if I did something wrong.

Attachment: H-File:

#include <scopewidget2.h>
#include <QWidget>
#include <QList>
#include <QTimer>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QSlider>
#include <QLabel>

class myWidgetNew:public QWidget
{
private:
    enum scope{scope1, scope2, scope3, COUNT}; // Falls weitere Scopes hinzugefügt werden sollen, können hier weitere eingefüt werden
    QList<ScopeWidget2> scopes; // Liste von Scopes
    QList<QVBoxLayout> vBoxes; // vertikale Boxen ( für das Raster notwendig)
    QList<QHBoxLayout> hBoxes; // hoizontale Boxen
    QList<QSlider> sliders;

    double maxValue;
    double minValue;
    QTimer* timer;

    QString outSring; // Ausgabe


public:
    myWidgetNew();
    void init_Layout();
};

Thanks for your help

ps. I think a Mod can close this thread.


Solution

  • first of all

    name += i;
    

    is wrong(it will compile, but with not expecting results)

    name += QString::number(i); //correct
    

    to your problem:

    QList<ScopeWidget2> scopes; // Liste von Scopes
    QList<QVBoxLayout> vBoxes; // vertikale Boxen ( für das Raster notwendig)
    QList<QHBoxLayout> hBoxes; // hoizontale Boxen
    QList<QSlider> sliders;
    

    you keep widgets by value, that will work if you move objects into container instead of copy by value, so:

    QList<ScopeWidget2*> scopes; // Liste von Scopes
    QList<QVBoxLayout*> vBoxes; // vertikale Boxen ( für das Raster notwendig)
    QList<QHBoxLayout*> hBoxes; // hoizontale Boxen
    QList<QSlider*> sliders;
    

    keep pointers to widgets - common practice, and operation copy for pointers is cheap.

    // create slider
    QSlider* slider_tmp  = new QSlider(Qt::Vertical, this);
    sliders.push_back(slider_tmp); //copy pointer
    
    // create scope
    ScopeWidget2* newScope = new ScopeWidget2(bufsize, maxValue, minValue);
    scopes.push_back(newScope); //copy pointer
    
    // put widgets together
    hlayout_tmp->addWidget(channel);
    hlayout_tmp->addWidget(slider_tmp);
    hlayout_tmp->addWidget(newScope);
    hBoxes.push_back(hlayout_tmp); //copy pointer
    

    AddWidget() - accept pointers, not objects, so addWidget(&realObj) or addWidget(pointerToObj). Append and push_back is same O(1) operation for QList(std::deque) but i prefer push_back.

    And more, if you only declare class as a pointer and dont use any of its method, better to move includes to .cpp file, and leave declaration of classes, something like this:

    #include <QTimer>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QLineEdit>
    #include <QSlider>
    #include <QLabel>
    

    move to .cpp and declare:

    class QTimer; //etc 
    class QLabel;
    

    in header.