Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QStackedWidget in GUI?

I am new to Qt and am have to make a GUI having multiple windows for this I found QStackedWidget class using Qt designer tools.

I added QStackedWidget using add new->Qt designer form class->Qstackwidget

after that I created an object of this class in my main window

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<stackedwidget.h>

namespace Ui { class MainWindow; }

class MainWindow : public QMainWindow {
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    StackedWidget *stk; };

#endif // MAINWINDOW_H

then i tried to display StackedWidget by:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

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

void MainWindow::on_pushButton_clicked()
{
    stk = new StackedWidget(this);
    stk->show();
}

But stackwidget is not opening .

Can someone tell me what am I doing wrong and how to implement QStackedWidget GUI using designer tools?

like image 426
vasu gupta Avatar asked Aug 17 '17 05:08

vasu gupta


People also ask

What is QStackedWidget?

QStackedWidget can be used to create a user interface similar to the one provided by QTabWidget . It is a convenience layout widget built on top of the QStackedLayout class. Like QStackedLayout , QStackedWidget can be constructed and populated with a number of child widgets (“pages”):

How do I create a custom widget in Qt?

Adding the Custom Widget to Qt Designer. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. Click New Widget so that we are ready to add our new widget. Change the Class name from 'MyCustomWidget' to 'Vcr'.

What is a dock widget?

QDockWidget provides the concept of dock widgets, also know as tool palettes or utility windows. Dock windows are secondary windows placed in the dock widget area around the central widget in a QMainWindow.


1 Answers

The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.

You are new to Qt so I suggest you to using Qt Designer: Image Example

You can drag&drop StackedWidget to your form, customize it then use arrows to go to the next page and work on it too.

StackedWidget is like a vector you can access them via indexes.

ui->stackedWidget->setCurrentIndex(1);
like image 185
Farhad Avatar answered Sep 21 '22 00:09

Farhad