简体   繁体   English

无效使用非静态成员函数

[英]invalid use of non-static member function

In my Qt application I wish to be able to add a tab to a non-static QTabWidget when the user presses a shortcut. 在我的Qt应用程序中,我希望能够在用户按下快捷方式时向非静态QTabWidget添加选项卡。 Normally I would implement it like this: 通常我会这样实现:

File.addAction("Settings", openSettings, Qt::ALT + Qt::Key_S);

where File is a QTabWidget and openSettings is a static method. 其中FileQTabWidgetopenSettings是静态方法。 The only issue with that is that it only works for static methods, and the issue with that is that I can't access a non-static variable in a static method. 唯一的问题是它仅适用于静态方法,而那个问题是我无法在静态方法中访问非静态变量。 As such I figured that since Qt asks for the function to be a static function I can instantiate a static std::function<> object as such: 因此,我发现由于Qt要求该函数为静态函数,因此可以实例化一个静态std::function<>对象,如下所示:

static std::function<void(void)> openSettings_ = []() { openSettings; };

and call it as such 并这样称呼它

File.addAction("Settings", openSettings_, Qt::ALT + Qt::Key_S);

The issue with this is that it generates the error: 问题在于它会生成错误:

Error: invalid use of non-static member function 'void window::openSettings()'

My reasoning for this is that I am familiar with C and assumed that what Qt calls a functor is almost the same as a function pointer that pretty much is an object. 我这样做的原因是,我熟悉C,并假设Qt称为函子与几乎是对象的函数指针几乎相同。 As such, I assumed that if I were to instantiate a static object of type std::function that pointed to / executed a non-static function I would get around this issue, which is clearly not the case. 因此,我假设如果要实例化一个指向/执行非静态函数的std::function类型的静态对象,那么我将解决这个问题,显然不是这种情况。 How would I go about doing this, seeing as my current thought process is wrong? 鉴于我当前的思维过程是错误的,我将如何去做?

First, the immediate error is raised because you're not actually calling the function. 首先,引发立即错误,因为您实际上并未调用该函数。 You must call it: openSettings(); 您必须调用它: openSettings(); .

However, this won't work. 但是,这不起作用。 openSettings is non-static member function. openSettings是非静态成员函数。 All such normal member functions take an implicit this pointer to the object on which they're being invoked. 所有这些普通成员函数都将一个隐式this指针指向在其上调用它们的对象。 This means that one cannot directly invoke the openSettings function without an object on which to invoke it. 这意味着,如果没有在其上调用它的对象,则无法直接调用openSettings函数。 But this is not captured by the lambda you've written, meaning there's no such object. 但是, this是不是你写的拉姆达捕获,这意味着没有这样的对象。

This can be fixed by capturing this in the lambda, such as auto openSettings_ = [this]() { this->openSettings(); } 可以通过在lambda中捕获this来解决this问题,例如auto openSettings_ = [this]() { this->openSettings(); } auto openSettings_ = [this]() { this->openSettings(); } ; auto openSettings_ = [this]() { this->openSettings(); } ;

But on the other hand, this function is acting like a slot. 但另一方面,此功能就像插槽一样。 You should attach the signal you're interested in directly to the signal using the standard signal/slot syntax, rather than writing the separate functor. 您应该使用标准的信号/插槽语法将感兴趣的信号直接附加到信号上,而不是编写单独的函子。 That would be something like this. 那将是这样的。

File.addAction("Settings", this, &(decltype(*this))::openSettings, Qt::ALT + Qt::Key_S);

(Note that I'm using decltype because I'm not sure what type *this is. You can substitute with the name of the class.) (请注意,我正在使用decltype因为我不确定*this是什么类型。您可以用类的名称代替。)

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

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