0

我正在使用QCustomPlot我试图编写代码的地方,一旦用户按下鼠标并拖动,它将重新调整我的轴。我做了:

   connect(ui->plot, SIGNAL(mousePress(QMouseEvent *event)), this,  SLOT(mousedrag(QMouseEvent*))); 

我不断得到:

QObject::connect: 没有这样的信号 QCustomPlot::mousePress(QMouseEvent *event)

但是mouseWheel(QWheelEvent*)两者mouseWheel都在库mousePress中声明了信号。QCustomPlot

我哪里错了?此外,如果有人有更好的信号来触发我的函数mousedrag(QMouseEvent*),该函数根据 y1 轴重新调整 y2 轴,我愿意接受建议。

4

2 回答 2

0

传递给的信号签名connect无效。参数名称不是签名的一部分。您还应该删除任何空格,这样connect就不必规范化签名。规范化签名没有不必要的空格,const并且必须删除最外层和引用,SIGNAL(textChanged(QString))例如not SIGNAL(textChanged(const QString &))

                                                 remove
                                                 vvvvv
connect(ui->plot, SIGNAL(mousePress(QMouseEvent *event)), this,             
        SLOT(mousedrag(QMouseEvent*)));

请改为执行以下操作:

// Qt 5
connect(ui->plot, &QCustomPlot::mousePress, this, &MyClass::mousedrag);
// Qt 4
connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), SLOT(mousedrag(QMouseEvent*));

侧边栏

TL;DR:这种 API 设计本质上是一个错误。

事件和信号/槽机制是QCustomPlot's 的设计混合在一起的不同范例。连接到这些信号的插槽只能以非常特定和有限的方式使用。您必须像使用派生类中的重载一样使用它们。这表示:

  1. 每个信号必须有 0 或 1 个插槽连接到它。

  2. 与同一线程中的对象的连接必须是直接的或自动的。

    您不能使用排队连接:当控件返回事件循环时,事件已被销毁,并且插槽/仿函数将使用悬空指针。

于 2016-06-03T20:05:44.597 回答
0

当使用“旧的”信号/槽连接语法时,即在语句中使用SIGNALSLOT宏的语法,您不应提供参数的名称,只提供它们的类型。connect()

换句话说:

SIGNAL(mousePress(QMouseEvent *event)) // WRONG, parameter name in there!
SIGNAL(mousePress(QMouseEvent *)) // GOOD
SIGNAL(mousePress(QMouseEvent*)) // BETTER: already normalized

所以只需将您的陈述更改为

connect( ui->plot, SIGNAL(mousePress(QMouseEvent*)), 
         this,     SLOT(mousedrag(QMouseEvent*)) ); 
于 2016-06-03T20:01:36.877 回答