关于Q_PROPERTY的问题

zmcomputer 2011-11-20 11:30:38
在网上找了下关于Q_PROPERTY的使用,如下
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
public:
Test(QObject *parent = 0) : QObject(parent) {}
virtual ~Test(){}
void setEnabled(bool e) { enabled = e; }
bool isEnabled() const { return enabled; }

private:
bool enabled;
};

然后在主函数中
Test *test = new Test;
test->setProperty("enabled", true);
//test->setEnabled(true); //ok also work

但我编译的时候总是出现下面这个错误
no matching function for call to 'Test::setProperty(cosnt char[8], bool)'
candidates are: bool QObject::setProperty(const char*, const QVariant&)

求解救啊~~~

...全文
223 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dbzhang800 2011-11-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zmcomputer 的回复:]

引用 1 楼 dext 的回复:
我这里没有问题,你可以尝试 将方法 放在 Test.cpp 中再试试看

不行啊,你是怎么弄的?可以把整个工程发给我编译下吗?
[/Quote]

不需要特别构造,下面的这种例子即可

#include <QtCore>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
a.setProperty("enabled", true);
return a.exec();
}
念茜 2011-11-20
  • 打赏
  • 举报
回复
no matching function for call to 'Test::setProperty(cosnt char[8], bool)'
candidates are: bool QObject::setProperty(const char*, const QVariant&)
意思是说:
test->setProperty("enabled", true); 不符合 :
bool QObject::setProperty(const char*, const QVariant&)
zmcomputer 2011-11-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dext 的回复:]
我这里没有问题,你可以尝试 将方法 放在 Test.cpp 中再试试看
[/Quote]
不行啊,你是怎么弄的?可以把整个工程发给我编译下吗?
dext 2011-11-20
  • 打赏
  • 举报
回复
我这里没有问题,你可以尝试 将方法 放在 Test.cpp 中再试试看

16,212

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧