0
votes

I want to display in button click a stackwidget with some information.

However, having trouble to do this. I have 2 files : mainwindow.cpp and ppualert.cpp and i want to open the file ppualert while the user click the button in mainwindow. (something like div in html)

my main problem : the stackwidget is shown allways and his buttons does not response.

what am i do wrong? BTW i can't open it in another different window cause i am workin with qt linux embedded and eglfs plugin, it's write the widgets straight to frame buffer and limited to one window.

here is the code:

MainWindow.cpp:

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


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

 ppu = new ppuAlert(this);
 connect(ppu,&ppuAlert::ppuDialogClosed,this,&MainWindow::onPPUDialogClosed);
 ui->swPPU->addWidget(new ppuAlert);
 ui->swPPU->hide();
}


void MainWindow::on_btnShowPPU_clicked()
{
   ui->swPPU->setCurrentIndex(0);
   ui->swPPU->show();
}
void MainWindow::onPPUDialogClosed()
{
   ui->swPPU->hide();
}

ppualert.cpp:

#include "ppualert.h"
#include "ui_ppualert.h"

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

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

void ppuAlert::on_pushButton_5_clicked()
{
   emit ppuDialogClosed();
}

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ppualert.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
  ppuAlert *ppu;
public slots:
  void on_btnShowPPU_clicked();
  void onPPUDialogClosed();
private:
  Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

ppualert.h:

#ifndef PPUALERT_H
#define PPUALERT_H

#include <QWidget>

namespace Ui {
class ppuAlert;
}

class ppuAlert : public QWidget
{
 Q_OBJECT

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

private slots:
    void on_pushButton_5_clicked();

private:
   Ui::ppuAlert *ui;
signals:
   void ppuDialogClosed();
};

#endif // PPUALERT_H
1
How do you know the slot handling the button press is called ? You haven't connected the button clicked() signal to anything, so it won't do anything when you click it. - Vincent Fourmond
@VincentFourmond isn't it already done automagically in QtDesigner? - Miki
@Miki - you right this is done automaticlly for me - Natile
Possibly, but your code doesn't show it, so I'm left to guess. Especially when you say that the slot never gets executed... - Vincent Fourmond

1 Answers

1
votes
  1. ppu = new ppuAlert(this); Passing the MainWindow parent of ppuAlert will embed the ppuAlert ui on the MainWindow ui.

  2. Change it to ppu = new ppuAlert(); Now you can add ppuAlert to the stacked widget and hide or show it.

  3. To hide the stackWidget , Add this in MainWindow connect(ppu,SIGNAL(ppuDialogClosed()),this,SLOT(ppuDialogClosed()));

  4. Change

    void MainWindow::onPPUDialogClosed()
    {
    ui->swPPU->setHidden(true);
    }
    
  5. Change this ui->swPPU->addWidget(new ppuAlert); to ui->swPPU->addWidget(ppu);