所谓的Qt工程在Linux下编译时出现.ui文件编译不通过的问题,实质上就是
如何在
QT中直接使用ui文件生成h文件的方法
QT的很多教程上都写
uic -o hello.h hello.ui
uic -o hello.cpp -impl hello.h hello.ui
来生成cpp和h文件,但是第二步其实是出不来的,系统认不出-impl的参数。
但是第一步生成的h文件其实已经内容很全了,我们未必需要cpp文件,我们只要在稍作修改就能让这个h顺利运行。
假设:生成的h文件的文件名为:“test.h”,test.h的内容如下(注意我在代码里用****注释标出的部分)
#ifndef FORM1_H
#define FORM1_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>
class Ui_Form//****
{
public:
QPushButton *pushButton;
void setupUi(QWidget *Form)//****
{
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(QSize(400, 300).expandedTo(Form->minimumSizeHint()));
pushButton = new QPushButton(Form);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(150, 190, 87, 30));
retranslateUi(Form);
QObject::connect(pushButton, SIGNAL(clicked()), Form, SLOT(close()));
QMetaObject::connectSlotsByName(Form);
} // setupUi
void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
Q_UNUSED(Form);
} // retranslateUi
};
namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui
#endif // FORM1_H
那么,我们只要在主文件(就是有main函数的那个cpp)里,添加一个类(这个类的名字可以自行修改)
class Win : public QWidget, public Ui::Form//****
{
public:
Win(QWidget *parent = 0):QWidget(parent)//****
{setupUi(this);}
};
段代码中红色的内容要与之前h文件中****标注的部分相对应。
然后#include "test.h"//就是用ui生成的那个h文件,不需要修改,直接include
在main函数中,用以下方法调用就行了:
Win w;
w.show();
比如:我的main.cpp的内容就是
#include <QApplication>
#include <QWidget>
#include "button.h"
class Win : public QWidget, public Ui::Form
{
public:
Win(QWidget *parent = 0):QWidget(parent)
{setupUi(this);}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Win w;
w.show();
return app.exec();
}
参考自:http://www.qtcn.org/bbs/read.php?tid=6979并整理
原文地址:http://blog.sina.com.cn/s/blog_60ffc4670100mral.html
其他参考资料:http://blog.csdn.net/hsl17/article/details/6737713
http://zhidao.baidu.com/question/212527133.html