2015-11-11 68 views
1

我目前使用一個小程序來處理Qt窗體(.ui)文件,並自動生成具有公共基類並使用虛函數來訪問表單元素。 在windows上,我將此工具作爲ui表單文件上的自定義構建步驟運行。該工具唯一的參數是輸入文件名。如何創建運行自定義生成事件的Makefile或.pro文件

爲了闡明,在Windows上,Qt在.ui文件上運行uic,創建一個ui_filename.h文件。我需要運行我的工具文件。

我該怎麼做/應該在Linux上做這個?理想情況下,我會將它構建到.pro文件中,但我也很樂意編輯Makefile。

我並不擅長寫Makefiles,所以這可能很簡單。我很高興爲每個ui_或* .ui文件手動編寫命令,但理想情況下它會自動發生在所有的.ui文件中。

回答

1

不需要手動編寫Makefiles。調用自定義外部工具的Makefile可以通過項目文件.pro中的qmake生成。

需要使用QMAKE_EXTRA_TARGETS創建自定義目標。然後,主目標應該被設置爲在該自定義對象(自定義的目標名稱應該被添加到​​)denendent,例如How to modify the PATH variable in Qt Creator's project file (.pro)

該工具應代形式頭的後運行,因此自定義的目標應該取決於文件customtarget1.depends = ui_mainwindow.h

customtarget1.target = form_scanner 
customtarget1.commands = tool_win_bat_or_linux_shell.sh 
customtarget1.depends = ui_mainwindow.h 
QMAKE_EXTRA_TARGETS += customtarget1 
PRE_TARGETDEPS += form_scanner 

上面qmake命令創建以下Makefile規則:

# the form header depends on mainwindow.ui 
ui_mainwindow.h: ..\test\mainwindow.ui 
<tab>#build command... 

# form scanner depends on ui_mainwindow.h 
form_scanner: ui_mainwindow.h 
<tab>tool_win_bat_or_linux_shell.sh 

# the final target depends on form scanner 
$(DESTDIR_TARGET): form_scanner ui_mainwindowm.h $(OBJECTS) 

如果有多種形式,可以創建多個自定義目標或創建一個目標依賴於所有形式的文件:

for (form, FORMS) { 
    # autogenerated form headers are located in root of build directory 
    FILE_NAME = $$basename(form) 
    # prepend ui_ and replace ending .ui by .h 
    FORM_HEADERS += ui_$$replace(FILE_NAME, .ui$, .h) 
} 

customtarget1.target = form_scanner 
customtarget1.commands = tool_win_bat_or_linux_shell.sh 
customtarget1.depends = $$FORM_HEADERS 

QMAKE_EXTRA_TARGETS += customtarget1 
PRE_TARGETDEPS += form_scanner 

因此,這個命令tool_win_bat_or_linux_shell.sh產生的所有形式的標題時,才執行。

也可以從項目目錄$$PWD運行shell腳本,並通過命令行參數的形式頭文件名:

customtarget1.commands = $$PWD/tool_win_bat_or_linux_shell.sh $$FORM_HEADERS 

現在,shell腳本可以爲每個窗體標題tool_win_bat_or_linux_shell.sh運行一些命令:

# for each command line argument 
for file in "[email protected]" 
do 
    echo "$file" 
    ls -l $file 
done 
+0

謝謝 - 這看起來就像票。我還沒有檢查過它,但它肯定比手動編輯Makefile更有意義 – mike