简体   繁体   English

将类方法传递给const char *参数

[英]Pass class method to const char * parameter

In Qt QShortcut has such constructor 在Qt中QShortcut有这样的构造函数

QShortcut(const QKeySequence& key, QWidget *parent,
          const char *member = 0, const char *ambiguousMember = 0,
          Qt::ShortcutContext context = Qt::WindowShortcut);

As far as I understand from documentation I can pass a method to the third parameter instead of using connect to signal activated. 据我从文档中了解,我可以将方法传递给第三个参数,而不是使用connect来激活信号。 But how can I pass method there? 但是如何在那儿传递方法呢? Using 运用

QShortcut* sh = new QShortcut(QKeySequence(Qt::Key_Alt, Qt::Key_1), this, &OnShortcut);

or 要么

QShortcut* sh = new QShortcut(QKeySequence(Qt::Key_Alt, Qt::Key_1), this, &MyClass::OnShortcut);

gives errors 给出错误

error: no matching function for call to 'QShortcut::QShortcut(QKeySequence, KeyPressProcessing* const, void (*)())' 错误:没有匹配的函数来调用“ QShortcut :: QShortcut(QKeySequence,KeyPressProcessing * const,void(*)())”

You have to use SLOT(OnShortcut()) instead of &OnShortcut because the QShortcut class constructor requires const char * as the second and third arguments and not the pointer to a function. 您必须使用SLOT(OnShortcut())而不是&OnShortcut因为QShortcut类的构造函数需要const char *作为第二个和第三个参数,而不是函数的指针。 Thus, Qt has dedicated conversion macro SLOT that accepts the function signature as an argument and converts it in the form that suitable for establishing connection. 因此,Qt具有专用的转换宏SLOT ,该宏接受功能签名作为参数并将其转换为适合建立连接的形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM