How to Call .ui Design Form with The PushButton.clicked event in Qt C++ with Example?

1. Create a new Qt GUI Application project.
2. Right Click the Forms folder in the current project, then choose “Add New...”.
3. Choose the Qt Designer Form Class
4. Choose the window template
5. Name the form and locate the folder where it will be saved
6. Finish.
7. Now do the rest:

main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}



mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <aboutme.h> //add the include .h file of your form

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void openAbout(); //make a new function to call the window

protected:
    void changeEvent(QEvent *e);

private:
    Ui::MainWindow *ui;
    AboutMe     *winAbout; //make a new declaration for the new window


private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


mainwindow.cpp:
#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::openAbout()
{
    winAbout = new AboutMe(this);
    winAbout->show();
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::on_pushButton_clicked()
{
    openAbout();
}

void MainWindow::on_pushButton_2_clicked()
{
    exit(1);
}


aboutme.h:
#ifndef ABOUTME_H
#define ABOUTME_H

#include <QMainWindow>

namespace Ui {
    class AboutMe;
}

class AboutMe : public QMainWindow {
    Q_OBJECT
public:
    AboutMe(QWidget *parent = 0);
    ~AboutMe();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::AboutMe *ui;

private slots:
    void on_pushButton_clicked();
};

#endif // ABOUTME_H

aboutme.cpp:
#include "aboutme.h"
#include "ui_aboutme.h"

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

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

void AboutMe::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void AboutMe::on_pushButton_clicked()
{
    close();
}

DOWNLOAD THE PROJECT (.zip)

14 comments:

  1. It's working.... Thanks a lot bro....!!!

    ReplyDelete
  2. You are welcome, happy coding..
    :)

    ReplyDelete
  3. Thank you for the help!!! It's was really useful

    ReplyDelete
  4. Thank you so much for this post!

    This should be part of the documentation for QT, I've been banging my head against this problem for 2 weeks!

    ReplyDelete
  5. I get an error saying "mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall AdminMain::AdminMain(class QWidget *)" (??0AdminMain@@QAE@PAVQWidget@@@Z) referenced in function "public: void __thiscall MainWindow::openAdmin(void)" (?openAdmin@MainWindow@@QAEXXZ)" ... I tried to solve this for many weeks but didn't work... can you please help me... I'm new to QT

    ReplyDelete
  6. Have you tried the "ready-to-build" source code above?

    Don't forget to input the new include file "yournewwindow.h" to the "mainwindow.h", then add the new declaration to the private section in the "mainwindow.h".
    Just test my "ready-to-build" source code (http://www.mediafire.com/?fbim2vlo9rbpf1p) first, try to understand what it means then implement it to your code.

    ReplyDelete
  7. Thank you so much.. I'll let you know if it's working :)

    ReplyDelete
    Replies
    1. I'm working in Qt 5.0.2(32 bit) Qt creator 2.7.0 .. In the steps you have mentioned above it says "Right Click the Forms folder in the current project, then choose “Add New...”."... When I right click the Forms folder I didn't get anything :o ... so I did that method right clicking the main folder... So will that affect the project?

      Delete
  8. What are the benefits and the disadvantages if we use .ui files to create so many files. Is there any performance issue if we increase the number of ui files? Or is it not a good design principle to use?

    ReplyDelete
  9. In 16 channel DVR when we click camera 5 its should come into camera 1 by using QT 4.8.2 please help any one am trying from past one week...





    ReplyDelete
  10. Thanks a lot for this. I have not installed QtCreator. I was working with Eclipse CDT and QtDesigner. I had thought it was not possible to call one more window but just using QtDesigner. But with the help of your code now I know how to make it work. Many thanks again.

    ReplyDelete
  11. hi..Im college student, thanks for sharing :)

    ReplyDelete