(转载)Qt中MOC的一些限制
無意中發(fā)現(xiàn)在Qt的文檔里有一篇關(guān)于moc工具的limitation的文章,里頭的東西值得學(xué)習(xí)一下。
Qt一個鏈接的錯誤, 程序結(jié)構(gòu)很簡單, 就是designer設(shè)計主界面,在代碼里用多重繼承方式使用, 奇怪的錯誤信息如下:
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文件竟然也會編譯出錯?這可真有點匪夷所思。把工程拿過來從頭看到尾也沒有看出任何錯誤可能會導(dǎo)致這個鏈接錯誤。 N長時間后,一個編譯階段報出的warning引起了我的注意, 大意是說多重繼承不能從兩個QObject類繼承, 這就怪了,它怎么會把我的UI類也當(dāng)成是從QObject派生的呢? 有了這個提示,嘗試著修改多重繼承的那行代碼:
class MainWin: public Ui::MainWindow, public QWidget改為
class MainWin: public QWidget, public Ui::MainWindow結(jié)果你猜怎么著?奇跡發(fā)生了……呵呵,編譯成功!原來竟然是繼承的順序造成的問題。 莫非這是Qt的bug?
給trolltech support發(fā)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.
原來如彼! 看來咱的道行還不夠,這么重要的文檔竟然從來都不知道它的存在。
仔細閱讀一下居然發(fā)現(xiàn)還有不少Q(mào)t中和moc相關(guān)的編程限制需要我們注意, 各位看官也來受受再教育吧:
moc的功能數(shù)一數(shù)
不需要moc處理的代碼可以用預(yù)定義的宏括起來,如下:
#ifndef Q_MOC_RUN
…
#endif
moc的限制數(shù)一數(shù)(太多了,眼花繚亂)
轉(zhuǎn)載于:https://www.cnblogs.com/sunpan822/p/8677474.html
總結(jié)
以上是生活随笔為你收集整理的(转载)Qt中MOC的一些限制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino从DHT11读取温湿度数据
- 下一篇: 怎样创建两个菜单JAVA_java –