Qt:QObject :: connect:没有这样的插槽

时间:2019-06-15 16:08:34

标签: c++11 qt5 ros signals-slots qpushbutton

我正在构建一个小型接口,在其中我将RViz细分为ROS的可视化工具。根据{{​​3}},可以重新使用和重新实现此工具中提供的某些功能。我正在尝试创建两个不同的QPushButton,它们将改变实现的渲染器的视图。 我的两个按钮的SIGNALSLOT存在一些问题,实际上,当我单击它们时,视图没有改变。 现在RViz具有称为getNumViews()的特定功能,该功能使用户可以设置视图数。就我而言,我有两个仅与我要实现的两个QPushButton相关的视图。

sig-slot

当我运行应用程序时,收到以下错误QObject::connect: No such slot MyViz::switchToView(),并认为正确设置SIGNALSSLOT的所有段落均正确地符合official documentation 。另外,出于完整性考虑,我正在使用C++11,通过更多的研究,我发现SIGNALSLOT中的旧official documentation应该是仍然有效。

下面与我正在运行的SIGNALSLOT相关的代码如下:

myviz.h

public Q_SLOTS:
    void switchToView(QString view);

private:
    rviz::ViewManager *view_man;

myviz.cpp

MyViz::MyViz(QWidget *parent) : QWidget(parent)
{

// operation in the constructor

    QPushButton *topViewBtn = new QPushButton("Top View");
    QPushButton *sideViewBtn = new QPushButton("Side View");


    connect(topViewBtn, SIGNAL(clicked()), this, SLOT(switchToView(QString("Top View"))));
    connect(sideViewBtn, SIGNAL(clicked()), this, SLOT(switchToView(QString("Side View"))));

}

在这里我设置与两个QPushButtons

相关的2种视图可能性
void MyViz::switchToView(QString view)
{
    view_man = manager->getViewManager();
    for(int i = 0; i<view_man->getNumViews(); i++)
    {
        if(view_man->getViewAt(i)->getName() == view)
            view_man->setCurrentFrom(view_man->getViewAt(i));
                    return;
        std::cout<<"Did not find view named %s"<<std::endl;
    }
}

感谢您为解决此问题指明了正确的方向。

1 个答案:

答案 0 :(得分:1)

您不能使用旧语法在connect函数中传递参数。另外,参数的数量和类型需要匹配,因此您只能将# Tuple unpacking with variable annotation syntax header: str kind: int body: Optional[List[str]] header, kind, body = message 连接到没有参数的函数。如果要使用旧语法,则需要定义2个插槽

clicked

您可以通过以下方式进行连接:

public Q_SLOTS:
    void switchToTopView();
    void switchToSideView();

编辑:

新的connect方法的正确语法为:

connect(topViewBtn, SIGNAL(clicked()), this, SLOT(switchToTopView()));
connect(sideViewBtn, SIGNAL(clicked()), this, SLOT(switchToSideView()));

此方法的优点是您还可以绑定到lambda,这可以在连接期间间接设置参数,因此您可以编写

connect( topViewBtn, &QPushButton::clicked, this, &MyViz::switchToTopView);
相关问题