插槽中的多个参数

发布于 2025-02-03 16:37:57 字数 770 浏览 2 评论 0原文

想要将两个参数传递给插槽。我有uii-> spinbox,它有两个信号valuechanged(int)value echanged(qString)。我这样做:

connect(ui->spinBox,&QSpinBox::valueChanged, [&](){ this->ChangeParamCurve(ui->spinBox_11->value(),0);});

出现错误:

错误:呼叫“连接”的匹配成员函数 Qobject.h:463:41:注意:候选功能不可行:没有超负荷 第二个论点的“估值”匹配'const char *' Qobject.h:260:13:注意:候选模板被忽略:无法推断 模板参数“ func1” qobject.h:300:13:注意:候选模板 忽略:无法推断模板参数'func1'qobject.h:208:36: 注意:候选功能不可行:至少需要4个参数, 但是提供了3个Qobject.h:211:36:注意:候选功能不 可行:至少需要4个论点,但提供了3个论点 Qobject.h:228:43:注意:候选功能模板不可行: 需要至少4个论点,但提供了3个论点。H:269:13: 注意:候选功能模板不可行:至少需要4个 争论,但提供了3个Qobject.h:308:13:注意:候选人 功能模板不可行:需要至少4个参数,但3个参数 提供了

我认为QT不能像这样接受超载信号或SMTH。 有什么想法吗?

Want to pass two arguments to the slot. I have ui->spinBox, it has two signals valueChanged(int) and valueChanged(QString). I'm doing like that:

connect(ui->spinBox,&QSpinBox::valueChanged, [&](){ this->ChangeParamCurve(ui->spinBox_11->value(),0);});

Appears error:

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

I think that qt can't accept overloaded signals or smth like that.
Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2025-02-10 16:37:57

docs 连接到此信号时使用QOVERLOAD。

connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
    [=](int i){ ChangeParamCurve(i, 0); });

同样,有多少参数thickparamcurve接受了多少参数。 lambda函数是在此处连接的插槽。它应该完全采用一个参数,因为那是信号发送的内容。但是在lambda中,您可以使用任意数量的参数调用函数。

As shown in the docs that @chehrlic pointed out, you need to use QOverload when connecting to this signal.

connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
    [=](int i){ ChangeParamCurve(i, 0); });

Also, it doesn't matter how many parameters ChangeParamCurve takes. The lambda function is the slot that is being connected here. And it should take exactly one parameter, since that is what the signal is sending. But within the lambda, you can call functions with any number of parameters.

雨轻弹 2025-02-10 16:37:57

您可以自己从spinbox类得出对象。您将能够根据需要在其中添加信号和插槽。

我曾经做过这样的事情,因为qlineedit没有“焦点”事件。让我对我为qlineedit>> Qline>的主要思想提出。

如果您想有一个集中的信号,则必须得出qlineedit类。这是如何实现这一目标的示例。

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()方法(插槽)发出信号。

以同样的方式,两个事件可以使用自定义参数触发您的自定义事件。同样,如果愿意,您可以通过右键单击并使用propers propers propers方法将这些功能添加到现有的spinbox,从设计师那里添加到。

You can derive an object from the spinbox class yourself. You would be able to add signals and slots to it as you want.

I once did something like this because there was no "focus" event for qlineedit. Let me throw in the main idea of what I wrote for qlineedit.

If you want to have a focused signal, you will have to derive the QLineEdit class. Here is a sample of how this can be achieved.

In the myLineEdit.h file you have:

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);
};

In the myLineEdit.cpp file you have:

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));
}

You can now connect the MyLineEdit::focussed() signal to your focus() method (slot).

In the same way two events can trigger your custom event with custom parameters. Again, if you wish, you can add these features to an existing spinbox, from the Designer, by right-clicking and using the Promote to method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文