VS2015无法自动生成ui_test.h

首先右键ui文件选择编译,然后VS就会自动在GeneratedFiles文件夹下生成ui_***.h

1.首先把ui文件放到项目的当前路径下.
2.在项目中加载该ui文件,然后先编译一下.
3.在你已经写好的h文件中引用它.比如命名为test.ui.则如下引用.#include "ui_test.h", 你也可以在项目文件中的GeneratedFiles中找到该文件.
4.在h文件的类中加入下面代码.把ui文件的命名空间拉过来.

 Ui::Form ui;

实在不懂的.可以在GeneratedFiles文件下的ui_test.h文件,使用文本编辑器去打开,我一般使用Notepad++.
可以清楚的看到,这个ui文件其实就是一个普通的类文件,只不过把所有的东西都放在了Ui_Form这个类中.最后在Ui这个命名空间内去定义了一个继承Ui_Form这个类的派生类.

 class Ui_Form
{
public:
    QPushButton *pushButton;
    QPushButton *pushButton_2;

    void setupUi(QWidget *Form)
    {
        if (Form->objectName().isEmpty())
            Form->setObjectName(QStringLiteral("Form"));
        Form->resize(400, 300);
        pushButton = new QPushButton(Form);
        pushButton->setObjectName(QStringLiteral("pushButton"));
        pushButton->setGeometry(QRect(130, 80, 75, 23));
        pushButton_2 = new QPushButton(Form);
        pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
        pushButton_2->setGeometry(QRect(130, 190, 75, 23));

        retranslateUi(Form);

        QMetaObject::connectSlotsByName(Form);
    } // setupUi

    void retranslateUi(QWidget *Form)
    {
        Form->setWindowTitle(QApplication::translate("Form", "Form", 0));
        pushButton->setText(QApplication::translate("Form", "PushButton", 0));
        pushButton_2->setText(QApplication::translate("Form", "PushButton", 0));
    } // retranslateUi

};

namespace Ui {
    class Form: public Ui_Form {};
} // namespace Ui

5.在.cpp文件中,ui.setupUi(this);调用类方法去通过ui初始化布局.
6.重新编译一下.可以发现在当前路径的Debug文件夹中生成了这么一个文件,叫做moc_c.cpp.这个是一个元文件.moc文件就是由C++代码去解释Qt的一些关键词后最终的C++代码片段.他有一个前提,类中必须包含Q_OBJECT这个宏.它把Qt中的信号与槽机制用C++中的回调函数来实现.可以使用Notepad++去看最终的代码.
7.之后就可以正常使用啦~如果要进行ui文件和类文件的移值.出现问题的话,可以直接把moc文件也顺便一起带过去.这样有时可以解决一些问题.
全程手打,望采纳,谢谢.

猜你喜欢

转载自blog.csdn.net/qq_28938403/article/details/81608599