Qt原物件metaObject編譯錯誤的問題

2021-10-18 11:46:35 字數 1717 閱讀 3882

關於元物件編譯器的作用可參考文章:qt中moc的作用

編譯由元物件編譯器生成的 moc_filename.cpp 檔案時,報錯

classname': is not a class or namespace name
錯誤定位於**

void classname::qt_static_metacall(qobject *_o, qmetaobject::call _c, int _id, void **_a)
其中,使用qt的標頭檔案filename.h中包含由巨集定義的多層命名空間

#pragma once

#include #include "common_def.h"

test_namespace_begin

class macroexpand : public qwidget

;test_namespace_end

巨集定義於common_def.h

#define test_namespace_begin namespace ns1 }}
測試環境:

qt community 5.10.1 + vs2013

qt community 5.14.1 + vs 2019

filename.h完整路徑包含中文如moc_macro_expanding\巨集展開測試時,元編譯生成的moc_xx.cpp檔案片段為:

void macroexpand::qt_static_metacall(qobject *_o, qmetaobject::call _c, int _id, void **_a)

qt_init_metaobject const qmetaobject macroexpand::staticmetaobject = };

可以看出巨集定義test_namespace_begin, test_namespace_end並未展於macroexpand::staticmetaobject之前。

filename.h完整路徑純英文如moc_macro_expanding\moc_macro_expanding時,元編譯生成的moc_xx.cpp檔案片段為

void ns1::ns2::ns3::macroexpand::qt_static_metacall(qobject *_o, qmetaobject::call _c, int _id, void **_a)

qt_init_metaobject const qmetaobject ns1::ns2::ns3::macroexpand::staticmetaobject = };

巨集定義ns1::ns2::ns3::被正確展開,進而編譯moc_xx.cpp便不會出現前述問題。

尚缺todo

使用qt時注意不要包含非英文路徑。

###進一步閱讀

macro expansion in moc

c++ preprocessor and qt moc

force moc to moc a file

刪除物件屬性, 改變原物件或不改變原物件

一 用處 刪除物件的屬性 eg var person delete person.name console.log person 結果 二 注意事項 1 不可以刪除用var const定義的變數 注 是指物件裡的屬性,不是指該物件不能用var 或 const。eg var a 123 或 const...

Servlet的Responset物件原理和使用

2 servletcontext物件 格式 http 1.1 200 ok 設定狀態碼 setstatus int sc setheader string name,string value 獲取輸出流 使用輸出流,將資料輸出到客戶端瀏覽器 1.完成重定向 1.設定狀態碼為302 response....

QT 按鈕物件

建立乙個按鈕物件 使用預設建構函式 qpushbutton 物件名 new qpushbutton 如果建立的物件並不會在視窗顯示,因為還沒有指定該物件的父親,所以不會在widget的視窗中顯示 指定物件的基類 物件名 setparent 父類物件 給按鈕設定文字 物件名 settext 哈嘍 se...