Qt 动态加载ui

Qt 动态加载ui_第1张图片

 loadui.h

#ifndef LOADUI_H  
#define LOADUI_H  
  
#include   
#include "ui_loadui.h"  
  
class loadUi : public QDialog  
{  
    Q_OBJECT  
  
public:  
    loadUi(QWidget *parent = 0, Qt::WFlags flags = 0);  
    ~loadUi();  
  
private:  
    Ui::loadUiClass ui;  
    private slots:  
        void sl_creatChild();  
};  
  
#endif // LOADUI_H  

loadui.cpp

#include "loadui.h"  
#include   
  
loadUi::loadUi(QWidget *parent, Qt::WFlags flags)  
    : QDialog(parent, flags)  
{  
    ui.setupUi(this);  
    connect(ui.btnCreatChild,SIGNAL(clicked()),this,SLOT(sl_creatChild()));  
}  
  
loadUi::~loadUi()  
{  
  
}  
  
void loadUi::sl_creatChild()  
{  
    QUiLoader loader;  
    QFile file("forms/child.ui");  
    file.open(QFile::ReadOnly);  
    QWidget *widget_child = loader.load(&file);  
    file.close();  
    widget_child->show();  
}  

 main.cpp

#include "loadui.h"  
#include   
  
int main(int argc, char *argv[])  
{  
    QApplication a(argc, argv);  
    loadUi w;  
    w.show();  
    return a.exec();  
}  

因为本示例中用到了QUiLoader类,需要

,还有资源里应该加载child.ui的路径。(forms/child.ui

 

 

你可能感兴趣的:(Qt,ui,qt)