There are two ways. You may subclass the Ui::MainWindow class or have it as a class member. I personally prefer the first way but it's mostly a matter of taste.
The first one (the multiple inheritance approach):
mainwindow.h
#include "ui_mainwindow.h"
class MainWindow
: public QMainWindow,
private Ui
::MainWindow{
Q_OBJECT
public:
protected slots:
void sayHello();
};
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
protected slots:
void sayHello();
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
setupUi(this);
connect(m_myButton, SIGNAL(clicked()),
this, SLOT(sayHello()));
}
void MainWindow::sayHello()
{
m_myLineEdit->setText(tr("Hello world"));
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
connect(m_myButton, SIGNAL(clicked()),
this, SLOT(sayHello()));
}
void MainWindow::sayHello()
{
m_myLineEdit->setText(tr("Hello world"));
}
To copy to clipboard, switch view to plain text mode
For the second way (the single inheritance approach):
mainwindow.h
#include "ui_mainwindow.h"
{
Q_OBJECT
public:
protected slots:
void sayHello();
private:
Ui::MainWindow ui;
};
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
protected slots:
void sayHello();
private:
Ui::MainWindow ui;
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
ui.setupUi(this);
connect(ui.m_myButton, SIGNAL(clicked()),
this, SLOT(sayHello()));
}
void MainWindow::sayHello()
{
ui.myLineEdit->setText(tr("Hello world"));
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.m_myButton, SIGNAL(clicked()),
this, SLOT(sayHello()));
}
void MainWindow::sayHello()
{
ui.myLineEdit->setText(tr("Hello world"));
}
To copy to clipboard, switch view to plain text mode
More details.
Bookmarks