1. 程式人生 > >關於QT5中QPrinter的問題(隨書程式碼5-9)

關於QT5中QPrinter的問題(隨書程式碼5-9)

<span style="color: rgb(68, 68, 68); font-size: 14px; line-height: 18px; background-color: rgb(245, 245, 245);">最近在看《Qt Creator快速入門》一書,我用的最新的Qt5.0,在移植隨書工程的時候,發現5-9工程中關於印表機的內容編譯出錯,出錯程式碼為:</span>
debug/mainwindow.o: In function `ZN10MainWindow7doPrintEv':
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:35: undefined reference to `_imp___ZN8QPrinterC1ENS_11PrinterModeE'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:37: undefined reference to `_imp___ZN12QPrintDialogC1EP8QPrinterP7QWidget'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:41: undefined reference to `_imp___ZN20QAbstractPrintDialog16addEnabledOptionENS_17PrintDialogOptionE'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:44: undefined reference to `_imp___ZN12QPrintDialog4execEv'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:46: undefined reference to `_imp___ZN12QPrintDialogD1Ev'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:46: undefined reference to `_imp___ZN8QPrinterD1Ev'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:46: undefined reference to `_imp___ZN12QPrintDialogD1Ev'
F:\HYQ_F\Projects\Qt\src\05\5-9\myPrint-build-Desktop_Qt_5_0_1_MinGW_32bit-Debug/../myPrint/mainwindow.cpp:46: undefined reference to `_imp___ZN8QPrinterD1Ev'

在.pro檔案中新增下面一行程式碼:

qtHaveModule(printsupport): QT += printsupport

就可以了,標頭檔案不需要改。這是因為Qt 5中將所有列印的相關類都放到了Qt Print Support模組造成的。

這裡有個建議就是,如果是初學者,沒有必要用Qt 5來學習Qt 4的內容,雖然官方一直說Qt 4程式可以很容易移植到Qt 5,不過對於初學者而言,學習知識是最重要的,而不是浪費時間來搞環境。因為大家對很多東西還不是很瞭解,Qt 4已經夠龐雜了,如果再和Qt 5摻和一起,我想,大家又會迷茫了,不知Qt如何學起了。

如果是學習《Qt Creator快速入門》,還是建議下載書中使用的Qt和Qt Creator版本來學習,這樣可以很快完成學習任務,儘量避免出錯。等自己已經有了一定的知識積累之後,並且是需要使用到高階版本的Qt的功能的時候,再去使用新版本的Qt。

很多公司現在還在用Qt 2,Qt 3呢,所以說不是最新的就是最好的。還是那句話,適合的東西才是最好的。這裡不是說Qt 5不好,只是,畢竟它跟Qt 4的框架都不同了,如果我們用不到它的新功能,而只是為了學習Qt程式設計,那麼現在沒必要用它。等我們掌握好Qt 4以後,瞭解了它的框架,然後再去學習Qt 5,這樣只需要花費很少的時間來檢視兩者的區別即可,大體程式還是一樣的。