C++ 为不带括号的函数输入参数
我正在使用Qt,目前我正在尝试将gui上的按钮按钮连接到静态函数。Qt中用于执行此操作的函数的语法为C++ 为不带括号的函数输入参数,c++,qt,C++,Qt,我正在使用Qt,目前我正在尝试将gui上的按钮按钮连接到静态函数。Qt中用于执行此操作的函数的语法为 connect(sender, &Sender::valueChanged, someFunction); 所以我的代码看起来像 QObject::connect(w.doneButton,&QPushButton::on_doneButton_pressed,getList); 问题是getList需要输入参数 QList<DeviceWidget*>* get
connect(sender, &Sender::valueChanged, someFunction);
所以我的代码看起来像
QObject::connect(w.doneButton,&QPushButton::on_doneButton_pressed,getList);
问题是getList需要输入参数
QList<DeviceWidget*>* getList(Window w)
{
return w.getList();
}
QList*getList(窗口w)
{
返回w.getList();
}
如果我用getList(w)替换getList,我会得到一个响应,表示它无法处理()运算符
'operator()' is not a member of 'QList<DeviceWidget*>*' return connect_functor(sender,signal,context,slot,&Func2::operator(),type);}
“operator()”不是“QList*”返回连接函数(发送方、信号、上下文、插槽和Func2::operator()、类型);)的成员
一定有办法解决这个问题。Qt的设计者不会设置这样的限制,但我已经在互联网上搜索了几天,似乎什么也找不到
谢谢你的帮助 将按下的“完成按钮”上的按钮连接到另一个插槽函数,然后从该插槽函数中调用getList。这样,就可以将窗口传递给getList
QObject::connect (w.doneButton, &QPushButton::on_doneButton_pressed, &someFunction);
void Window::someFunction()
{
getList(this);
}
将按下的按钮连接到另一个插槽函数,然后从该插槽函数中调用getList。这样,就可以将窗口传递给getList
QObject::connect (w.doneButton, &QPushButton::on_doneButton_pressed, &someFunction);
void Window::someFunction()
{
getList(this);
}
(我觉得)解决问题的惯用方法是使用@Nim在评论中建议的boost::bind()
或std::bind()
(如果您使用的是c++11)
例如:
#include <boost/bind.hpp>
QObject::connect(w.doneButton, &QPushButton::on_doneButton_pressed, boost::bind(getList, w));
#include <functional>
QObject::connect(w.doneButton, &QPushButton::on_doneButton_pressed, std::bind(getList, w));
如果既不能使用c++11,也不能使用Boost,则可以使用函子(这有点混乱,基本上是lambda在c++11中为您所做的):
类getListProxy
{
公众:
getListProxy(窗口w):m_w(w){}
QList*运算符()(){
返回getList(m_w);
}
私人:
窗口m_w;
};
QObject::connect(w.doneButton,&QPushButton::on_doneButton_按下,getListProxy(w));
还要注意,所有这些构造都将按值传递
w
,因为getList(窗口w)
按值获取w
。根据Window
的实现,这可能不是您想要的。(我觉得)解决问题的惯用方法是使用@Nim在评论中建议的boost::bind()
或std::bind()
(如果您使用的是c++11)
例如:
#include <boost/bind.hpp>
QObject::connect(w.doneButton, &QPushButton::on_doneButton_pressed, boost::bind(getList, w));
#include <functional>
QObject::connect(w.doneButton, &QPushButton::on_doneButton_pressed, std::bind(getList, w));
如果既不能使用c++11,也不能使用Boost,则可以使用函子(这有点混乱,基本上是lambda在c++11中为您所做的):
类getListProxy
{
公众:
getListProxy(窗口w):m_w(w){}
QList*运算符()(){
返回getList(m_w);
}
私人:
窗口m_w;
};
QObject::connect(w.doneButton,&QPushButton::on_doneButton_按下,getListProxy(w));
还要注意,所有这些构造都将按值传递
w
,因为getList(窗口w)
按值获取w
。根据Window
的实现,这可能不是您想要的。您需要查看std::bind
或boost::bind()
以帮助进行参数绑定。'operator()'不是'QList*'返回连接函数的成员(发送方、信号、上下文、插槽和Func2::operator(),类型);}(请忘记我的最后一条评论,我读错了您的代码。)您需要查看std::bind
或boost::bind()
以帮助进行参数绑定。'operator()'不是'QList*'返回连接函数的成员(发送方、信号、上下文、插槽和Func2::operator(),type);}(请忘记我最后的评论,我看错了你的代码。)谢谢。这是非常全面的!谢谢你。这是非常全面的!我很感激