如果我理解这将满足您的需求:
mytarget.target = .buildfile
mytarget.commands = ./update.py
QMAKE_EXTRA_TARGETS += mytarget
PRE_TARGETDEPS += .buildfile
第一条语句定义了一个名为“buildfile”的新 Makefile 目标,第二条语句定义了 buildfile 目标的功能(它调用了生成代码的 update.py),第三条语句将 mytarget 定义为新的 qmake 目标,最后一个添加buildfile 目标到 qmake 的目标列表。
哦,我忘了。我在 qmake 的手册中找到了这个:http: //qt-project.org/doc/qt-4.8/qmake-environment-reference.html在本节。也许它有用;)
对于 moc 你可以定义这些:
new_moc.output = moc_${QMAKE_FILE_BASE}.cpp
new_moc.commands = moc ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
new_moc.depend_command = g++ -E -M ${QMAKE_FILE_NAME} | sed "s,^.*: ,,"
new_moc.input = NEW_HEADERS
QMAKE_EXTRA_COMPILERS += new_moc
我做了一些测试。这对我有用:
//.pro file:
QT += core gui
TARGET = test
TEMPLATE = app
SOURCES += main.cpp \
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
mytarget.target = .buildfile
mytarget.commands = ./update.py
QMAKE_EXTRA_TARGETS += mytarget
PRE_TARGETDEPS += .buildfile
new_moc.target = .mymoc
new_moc.output = moc_widget.cpp
new_moc.commands = moc widget.cpp -o moc_widget.o
new_moc.depend_command = g++ -E -M widget | sed "s,^.*: ,,"
new_moc.input = moc_widget.h
QMAKE_EXTRA_COMPILERS += new_moc
//update.py:
#!c:/Python/python.exe -u
fd=open("widget.h",'w')
fd.write("#ifndef WIDGET_H\n")
fd.write("#define WIDGET_H\n")
fd.write("#include <QWidget>\n")
fd.write("namespace Ui {\n")
fd.write("class Widget;\n")
fd.write("}\n")
fd.write("class Widget : public QWidget\n")
fd.write("{\n")
fd.write(" Q_OBJECT\n")
fd.write("public:\n")
fd.write(" explicit Widget(QWidget *parent = 0);\n")
fd.write(" ~Widget();\n")
fd.write("private:\n")
fd.write(" Ui::Widget *ui;\n")
fd.write("};\n")
fd.write("#endif // WIDGET_H\n")
fd.close()
fd=open("widget.cpp",'w')
fd.write("#include \"widget.h\"\n")
fd.write("#include \"ui_widget.h\"\n")
fd.write("Widget::Widget(QWidget *parent) :\n")
fd.write(" QWidget(parent),\n")
fd.write(" ui(new Ui::Widget)\n")
fd.write("{\n")
fd.write(" ui->setupUi(this);\n")
fd.write("}\n")
fd.write("\n")
fd.write("Widget::~Widget()\n")
fd.write("{\n")
fd.write(" delete ui;\n")
fd.write("}\n")
fd.close()