Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将uint16_t和char*与QMetaObject::invokeMethod()一起使用_C++_Qt - Fatal编程技术网

C++ 将uint16_t和char*与QMetaObject::invokeMethod()一起使用

C++ 将uint16_t和char*与QMetaObject::invokeMethod()一起使用,c++,qt,C++,Qt,我想使用 包含插槽的类声明为: class Paintable : public QObject { Q_OBJECT [...] public slots: void drawString(uint16_t x, uint16_t y, uint16_t size, const char* str, color c); } 调用invokeMethod的方法定义为: void drawStringAsynchronously(uint16_t x, uint16_t y,

我想使用

包含插槽的类声明为:

class Paintable : public QObject {
  Q_OBJECT
  [...]
  public slots:
    void drawString(uint16_t x, uint16_t y, uint16_t size, const char* str, color c);
}
调用invokeMethod的方法定义为:

void drawStringAsynchronously(uint16_t x, uint16_t y, uint16_t size, const char* str, color c) {
  QMetaObject::invokeMethod(paintable,
               "drawString",
               Qt::QueuedConnection,
               Q_ARG(uint16_t, x), Q_ARG(uint16_t, y), Q_ARG(uint16_t, size),
               Q_ARG(const char*, str), Q_ARG(color, c));
}
(其中,
paintable
类型为
paintable*

但Qt似乎无法在invokeMethod中使用uint16_t或char*,因为在运行时我收到以下消息:

QMetaMethod::invoke:无法处理未注册的数据类型“const char*”

QMetaMethod::invoke:无法处理未注册的数据类型“uint16\u t”

分别

我能够使用
qRegisterMetaType()
成功注册我的自定义结构
color
,但是由于
uint16\u t
char*
都不是结构或类,所以这不起作用


如果有人能告诉我怎么做或者给我一个好的选择,我会非常高兴。

注册uint16的问题是:它是一个typedef,Qt已经注册了这个类型,但它的名称不同。由于QMetaType系统基于按名称确定类型,因此会产生问题

您可以通过以下方式解决此问题:

Q_DECLARE_METATYPE(uint16_t)
然后:

qRegisterMetaType(“uint16_t”);

这将创建一个别名,以便可以使用该名称创建元类型。

您是否应该使用
Q\u DECLARE\u元类型(…)
宏?@cmannett85:我正在使用
Q\u DECLARE\u元类型()
qRegisterMetaType()
来创建文档中所述的自定义
color
类型。但这对非结构或非类不起作用。您确实尝试过将这些类型注册为元类型吗?这会起作用的。@DanMilburn:我又试了一次,但使用
Q_DECLARE_元类型(char*)无效但似乎可以使用
Q_DECLARE_元类型(const char*)。不幸的是,对于
uint16\u t
const uint16\u t
qRegisterMetaType<uint16_t>("uint16_t");