Сделайте отдельный класс для окна. Т.е. как-то так:
main.cpp:
#include "mainwindow.h"
#include <QtWidgets/QApplication>
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 <QtWidgets/QMainWindow>
#include <QtWidgets/QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void buttonPressed();
private:
void setupUi();
QPushButton *pushButton;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi();
}
MainWindow::~MainWindow()
{
}
void MainWindow::buttonPressed()
{
QMessageBox::warning(this, "Hello", "Hello, World");
}
void MainWindow::setupUi()
{
if (objectName().isEmpty())
setObjectName(QStringLiteral("MainWindow"));
resize(400, 300);
setCentralWidget(new QWidget(this));
pushButton = new QPushButton(centralWidget());
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(90, 90, 92, 27));
connect(pushButton, SIGNAL(clicked(bool)), this, SLOT(buttonPressed()));
}
*.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = app
TARGET = untitled27
INCLUDEPATH += .
HEADERS += mainwindow.h
SOURCES += main.cpp mainwindow.cpp