(转载)Qt中MOC的一些限制
無意中發現在Qt的文檔里有一篇關于moc工具的limitation的文章,里頭的東西值得學習一下。
Qt一個鏈接的錯誤, 程序結構很簡單, 就是designer設計主界面,在代碼里用多重繼承方式使用, 奇怪的錯誤信息如下:
moc_mainwin.cpp:39: error: ‘staticMetaObject’ is not a member of ‘Ui::MainWindow’
moc_mainwin.cpp: In member function ‘virtual void* MainWin::qt_metacast(const char)’: moc_mainwin.cpp:56: error: ‘qt_metacast’ is not a member of ‘MainWin::qt_metacast(const char)::QMocSuperClass’
moc_mainwin.cpp: In member function ‘virtual int MainWin::qt_metacall(QMetaObject::Call, int, void)’: moc_mainwin.cpp:62: error: ‘qt_metacall’ is not a member of ‘MainWin::qt_metacall(QMetaObject::Call, int, void)::QMocSuperClass’
make: *** [moc_mainwin.o] Error 1
Qt自動生成的moc文件竟然也會編譯出錯?這可真有點匪夷所思。把工程拿過來從頭看到尾也沒有看出任何錯誤可能會導致這個鏈接錯誤。 N長時間后,一個編譯階段報出的warning引起了我的注意, 大意是說多重繼承不能從兩個QObject類繼承, 這就怪了,它怎么會把我的UI類也當成是從QObject派生的呢? 有了這個提示,嘗試著修改多重繼承的那行代碼:
class MainWin: public Ui::MainWindow, public QWidget改為
class MainWin: public QWidget, public Ui::MainWindow結果你猜怎么著?奇跡發生了……呵呵,編譯成功!原來竟然是繼承的順序造成的問題。 莫非這是Qt的bug?
給trolltech support發bug report得到了這樣的回答:
This is actually a known limitation which has been documented for some time, when using multiple inheritance you have to specify the QObject based class first and then the other class. Its mentioned in the documentation at:
http://doc.trolltech.com/4.5/moc.html
under the limitations section.
原來如彼! 看來咱的道行還不夠,這么重要的文檔竟然從來都不知道它的存在。
仔細閱讀一下居然發現還有不少Qt中和moc相關的編程限制需要我們注意, 各位看官也來受受再教育吧:
moc的功能數一數
不需要moc處理的代碼可以用預定義的宏括起來,如下:
#ifndef Q_MOC_RUN
…
#endif
moc的限制數一數(太多了,眼花繚亂)
轉載于:https://www.cnblogs.com/sunpan822/p/8677474.html
總結
以上是生活随笔為你收集整理的(转载)Qt中MOC的一些限制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino从DHT11读取温湿度数据
- 下一篇: 怎样创建两个菜单JAVA_java –