Qt – how to use ui file for making a simple widget

qtqt-designerqt4

i have a simple window with a quit button in qt.The working code is shown below

 #include <QApplication>
 #include <QDialog>
 #include <QPushButton>

class MyWidget : public QWidget
{
public:
    MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(200, 120);

    QPushButton *btquit = new QPushButton(tr("Quit"), this);
    btquit->setGeometry(62, 40, 75, 30);
    btquit->setFont(QFont("Times", 18, QFont::Bold));

    connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    return app.exec();
}

Now i want to code this program using qt designer.I created a widget named "mywindow" and a button inside that main widget named "btquit" in the ui file using qt designer.
How to rewrite the above code with the ui file.The name of ui file is mywindow.ui

Best Answer

#include <QApplication>
#include <QDialog>
#include <QPushButton>
#include "ui_mywindow1.h"

class MyWidget : public QWidget,private Ui::mywindow
{
public:
    MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    setupUi(this);


    connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    return app.exec();
}
Related Topic