直接使用
#include "ui_hellodialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog w;
Ui::HelloDialog ui;
ui.setupUi(&w);
w.show();
return a.exec();
}
定义新类,在新类中定义一个Ui::HelloDialog的子对象,使用子对象加载该界面。也叫单继承方法
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget//单继成QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
private:
Ui::MyWidget *ui;
};
#endif // MYWIDGET_H
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QDialog>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
QDialog *dialog = new QDialog(this);
dialog->setModal(true);
dialog->show();
}
MyWidget::~MyWidget()
{
delete ui;
}
#include <QApplication>
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
派生新类,继承Ui::HelloDialog类,也叫多重继承方法
#ifndef HELLODIALOG_H
#define HELLODIALOG_H
#include <QDialog>
#include "ui_hellodialog.h"
class HelloDialog : public QDialog, public Ui::HelloDialog //继承了ui,多继承
{
Q_OBJECT
public:
explicit HelloDialog(QWidget *parent = 0);
signals:
public slots:
};
#endif // HELLODIALOG_H
#include "hellodialog.h"
#include "ui_hellodialog.h"
HelloDialog::HelloDialog(QWidget *parent) :
QDialog(parent),
Ui::HelloDialog()
{
setupUi(this);
}
#include <QApplication>
#include "hellodialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HelloDialog w;
w.show();
return a.exec();
}