Greenfish
Greenfish

Reputation: 378

how do i add QWidgets properly on a QStack in c++?

I have the following problem. I want to waste as less memory as possible and therefore remove a underlying QWidget whenever i load another QWidget atop of the it. E.g. i have several contents and i want to switch between them by hitting a button.

So i tried to create a QStack and add QWidgets to it whenever there is the proper button clicked. At the same time i tried to remove the previous widget which is no more shown.

/*header file*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStack>

namespace Ui
{
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0); //Konstruktor
    ~MainWindow(); //Destruktor

    QStack<QWidget> *widgetStack;

private:
    Ui::MainWindow *ui;

private slots:
    void on_clickToProceedButton_clicked();
    void on_Rand_Taster_Anmeldung_clicked();
    void on_Rand_Taster_Anael_clicked();
    void on_Rand_Taster_Power_clicked();
};

#endif

/*cpp file*/
#include "headerFiles/mainwindow.h"
#include "ui_mainwindow.h"
#include "headerFiles/authentication.h"


MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    widgetStack = new QStack<QWidget>();
    widgetStack->push(ui->clickToProceedButton);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_clickToProceedButton_clicked()
{
    Authentication *auth = new Authentication(this);
    ui->verticalLayout->addWidget(auth);
    ui->verticalLayout->removeWidget(widgetStack->pop());
    widgetStack->push(auth);

}

/*header file, rest of the code is not important, just that it is a QWidget*/
class Authentication : public QWidget

When i run this program it says that "there is no matching function for call to 'QStack::push(QPushButton*);"

QPushButton inherits from QWidget so i can only guess the problem that i should have used something what is similar to in java as a generic type parameter.

I googled on this topic and found this Is there equivalent of <? extends T> <? super T> in c++? saying that there is a way to implement a function for generic type parameters like this but i don't see how I can apply this to my code.

I also found the class QStackedWidget but that is what I don't want to use as with this approach i can only hide widgets but not delete them. I really want save memory and don't load everything on a qstackedwidget. I am new to c++ and Qt. Please help me. Thank's in advance

Upvotes: 0

Views: 270

Answers (2)

Benjamin Debott&#233;
Benjamin Debott&#233;

Reputation: 348

You're trying to add a QWidget* to a QStack stack.

You may refactor your

QStack<QWidget> *widgetStack;

into

QStack<QWidget*> *widgetStack;

This should work better

Upvotes: 0

dhaumann
dhaumann

Reputation: 1688

QStack is not what you want. Instead, have a look at QStackedWidget and QStackedLayout. You can find details in the "Detailed Description" sections of the referred links.

Upvotes: 1

Related Questions