插槽中的多个参数
Multiple arguments in slot
想要将两个参数传递给插槽。我有 ui->spinBox
,它有两个信号 valueChanged(int)
和 valueChanged(QString)
。我就是这样做的:
connect(ui->spinBox,&QSpinBox::valueChanged, [&](){ this->ChangeParamCurve(ui->spinBox_11->value(),0);});
出现错误:
error: no matching member function for call to 'connect'
qobject.h:463:41: note: candidate function not viable: no overload of
'valueChanged' matching 'const char *' for 2nd argument
qobject.h:260:13: note: candidate template ignored: couldn't infer
template argument 'Func1' qobject.h:300:13: note: candidate template
ignored: couldn't infer template argument 'Func1' qobject.h:208:36:
note: candidate function not viable: requires at least 4 arguments,
but 3 were provided qobject.h:211:36: note: candidate function not
viable: requires at least 4 arguments, but 3 were provided
qobject.h:228:43: note: candidate function template not viable:
requires at least 4 arguments, but 3 were provided qobject.h:269:13:
note: candidate function template not viable: requires at least 4
arguments, but 3 were provided qobject.h:308:13: note: candidate
function template not viable: requires at least 4 arguments, but 3
were provided
我认为 qt 不能接受过载信号或类似的东西。
有什么想法吗?
您可以自己从 spinbox
class 派生对象。您可以根据需要向其中添加信号和插槽。
我曾经做过这样的事情,因为 qlineedit
没有“焦点”事件。让我谈谈我为 qlineedit
.
所写内容的主要思想
如果你想有一个集中的信号,你将不得不导出 QLineEdit
class。这是如何实现这一目标的示例。
在 myLineEdit.h
文件中你有:
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget *parent = 0);
~MyLineEdit();
signals:
void focussed(bool hasFocus);
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
};
在 myLineEdit.cpp
文件中你有:
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent)
{}
MyLineEdit::~MyLineEdit()
{}
void MyLineEdit::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
emit(focussed(true));
}
void MyLineEdit::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
emit(focussed(false));
}
您现在可以将 MyLineEdit::focussed()
信号连接到您的 focus()
方法(插槽)。
以相同的方式,两个事件可以使用自定义参数触发您的自定义事件。同样,如果您愿意,可以通过 right-clicking 并使用 Promote to
方法从 Designer 将这些功能添加到现有的 spinbox
。
如@chehrlic指出的docs所示,连接到此信号时需要使用QOverload。
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
[=](int i){ ChangeParamCurve(i, 0); });
另外,ChangeParamCurve
有多少个参数并不重要。 lambda 函数是这里连接的槽。它应该只接受一个参数,因为这就是信号发送的内容。但在 lambda 中,您可以使用任意数量的参数调用函数。
想要将两个参数传递给插槽。我有 ui->spinBox
,它有两个信号 valueChanged(int)
和 valueChanged(QString)
。我就是这样做的:
connect(ui->spinBox,&QSpinBox::valueChanged, [&](){ this->ChangeParamCurve(ui->spinBox_11->value(),0);});
出现错误:
error: no matching member function for call to 'connect' qobject.h:463:41: note: candidate function not viable: no overload of 'valueChanged' matching 'const char *' for 2nd argument qobject.h:260:13: note: candidate template ignored: couldn't infer template argument 'Func1' qobject.h:300:13: note: candidate template ignored: couldn't infer template argument 'Func1' qobject.h:208:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided qobject.h:211:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided qobject.h:228:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:269:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:308:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
我认为 qt 不能接受过载信号或类似的东西。 有什么想法吗?
您可以自己从 spinbox
class 派生对象。您可以根据需要向其中添加信号和插槽。
我曾经做过这样的事情,因为 qlineedit
没有“焦点”事件。让我谈谈我为 qlineedit
.
如果你想有一个集中的信号,你将不得不导出 QLineEdit
class。这是如何实现这一目标的示例。
在 myLineEdit.h
文件中你有:
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget *parent = 0);
~MyLineEdit();
signals:
void focussed(bool hasFocus);
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
};
在 myLineEdit.cpp
文件中你有:
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent)
{}
MyLineEdit::~MyLineEdit()
{}
void MyLineEdit::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
emit(focussed(true));
}
void MyLineEdit::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
emit(focussed(false));
}
您现在可以将 MyLineEdit::focussed()
信号连接到您的 focus()
方法(插槽)。
以相同的方式,两个事件可以使用自定义参数触发您的自定义事件。同样,如果您愿意,可以通过 right-clicking 并使用 Promote to
方法从 Designer 将这些功能添加到现有的 spinbox
。
如@chehrlic指出的docs所示,连接到此信号时需要使用QOverload。
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
[=](int i){ ChangeParamCurve(i, 0); });
另外,ChangeParamCurve
有多少个参数并不重要。 lambda 函数是这里连接的槽。它应该只接受一个参数,因为这就是信号发送的内容。但在 lambda 中,您可以使用任意数量的参数调用函数。