title: Changing widget in QStackedWidget
body: <p>I have 2 custom widgets in my stacked widget and I want to be able to change from one to the other I press a button, however when I currently click it the program just terminates.</p>
  
  <p>Here is the first custom widget .cpp:</p>
  
  <pre><code> #include "loginpage.h"
   #include "ui_loginpage.h"
  
   #include "mainwindow.h"
   #include "mainpage.h"
  
   #include "QtGui"
  
  
   LoginPage::LoginPage(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::LoginPage)
   {
       ui-&gt;setupUi(this);
  
  
       connect(ui-&gt;loginPushButton, SIGNAL(clicked()), this, SLOT(doLogin()));
   }
  
   LoginPage::~LoginPage()
   {
       delete ui;
   }
  
   QString LoginPage::getUsername()
   {
       QString userName = ui-&gt;usernameLineEdit-&gt;text();
       return userName;
   }
  
   QString LoginPage::getPassword()
   {
       QString passWord = ui-&gt;passwordLineEdit-&gt;text();
       return passWord;
   }
  
   void LoginPage::doLogin()
   {
       QString username = getUsername();
       QString password = getPassword();
       bool check = checkCredentials(password, username);
  
       if(check == true)
       {
           MainWindow *main;
           main = new MainWindow;
           main-&gt;test();
  
       }else
       {
           QMessageBox::information(this, tr("incorrect"), tr("incorrect"));
       }
  
  
   }
  
  ...
  </code></pre>
  
  <p>Here is the main window .cpp:</p>
  
  <pre><code> #include "mainwindow.h"
   #include "ui_mainwindow.h"
   #include "loginpage.h"
   #include "ui_loginpage.h"
  
   #include "mainpage.h"
  
   #include &lt;QtGui&gt;
  
   MainWindow::MainWindow(QWidget *parent) :
       QMainWindow(parent),
       ui(new Ui::MainWindow)
   {
  
       ui-&gt;setupUi(this);
  
   }
  
   MainWindow::~MainWindow()
   {
       delete ui;
   }
  
   void MainWindow::test()
   {
       ui-&gt;stackedWidget-&gt;setCurrentIndex(1);
  
   }
  </code></pre>
  
  <p>Any help appreciated</p>