我想用Q_CLASSINFO宏存储一些类信息。但是,我想将其封装在我自己的宏中,例如:
#define DB_TABLE( TABLE ) \
Q_CLASSINFO( "db_table", #TABLE )
#define DB_FIELD( PROPERTY, COLUMN ) \
Q_CLASSINFO( "dbcol_" #PROPERTY, #COLUMN )
class Foo : public QObject
{
Q_OBJECT
DB_TABLE( some_table )
DB_FIELD( clientName, client_name )
}不幸的是,moc没有展开宏,所以没有添加Q_CLASSINFO。
我试着给moc提供已经预处理过的源代码,但它在一些包含的Qt类上失败了。
你知道解决这个问题的方法吗?
发布于 2010-11-09 02:42:31
除了使用你自己的pre-moc预处理器之外,没有。例如,MeeGo Touch就是这样做的。既然诺基亚自己都在这么做,我相信没有其他办法了。
在您的例子中,它只涉及将您自己的声明转换成Q_CLASSINFO,所以应该不会太难。如果使用qmake,也可以将其添加到构建序列中。
发布于 2013-02-09 10:39:29
实现这一点的简单方法是修改moc预处理器。
请转到Qt源代码,打开moc项目的新副本,例如,使用qtbase/src/tools/moc ( (C:\Qt\Qt5.0.1\5.0.1\Src\qtbase\src\tools\moc)
//阶段1:去掉反斜杠-换行符input = cleaned( input );// <-插入你的代码来修改输入变量// input是一个QByteArray对象,它包含了比moc正在处理的.h文件的源代码//我已经创建了replaceCustomMacros函数,见下一行replaceCustomMacros(input);...
#ifndef Q_MOC_RUN #定义DB_FIELD(属性,列) #endif //或者在我的例子中#ifndef Q_MOC_RUN #定义Q_SERVICE_INFO(方法,路径,类型) #endif
最后,我给你介绍我自己的函数replaceCustomMacros的源代码:
此函数用于将Q_SERVICE_INFO(方法,路径,类型)宏转换为Q_CLASSINFO(“srv://方法”,"type: path ")
void Preprocessor::replaceCustomMacros(QByteArray &source)
{
QString str(QLatin1String(source.data()));
QString param_exp(QLatin1String("([^,\n]+)"));
QByteArray expression("Q_SERVICE_INFO\\s*\\(");
expression
.append(param_exp.toLatin1())
.append(",")
.append(param_exp.toLatin1())
.append("(,")
.append(param_exp.toLatin1())
.append(")?\\)");
QRegExp *reg_ex = new QRegExp(QLatin1String(expression));
int pos = -1, offset = -1, len = str.length();
while ((offset = reg_ex->lastIndexIn(str, pos)) != -1)
{
reg_ex->cap(1);
pos = -(len - offset) - 1;
QString capturedString = reg_ex->capturedTexts().at(0);
QString pattern = capturedString;
pattern.remove(0, pattern.indexOf(QLatin1String("(")) + 1);
pattern.remove(pattern.length() - 1, 1);
QStringList params = pattern.split(QLatin1String(","));
QString method = params.at(0).trimmed();
method = method.mid(1, method.length() - 2);
QString type;
if (params.length() < 3)
{
type.append(QLatin1String("GET"));
}
else
{
type = params.at(2).trimmed();
type = type.mid(1, type.length() - 2);
}
QString path = params.at(1).trimmed();
path = path.mid(1, path.length() - 2);
source.replace(offset, capturedString.length(), QString(QLatin1String("Q_CLASSINFO(\"srv://%1\",\"%2:%3\")")).arg(method, type, path).toLatin1());
}
delete reg_ex;
}我还没有在互联网上找到任何具体的解决方案,然后我已经发布了这个解决方案。
祝你好运:)
https://stackoverflow.com/questions/4119688
复制相似问题