This question already has answers here:
C++ Qt signal and slot not firing
                                
                                    (3个答案)
                                
                        
                                5年前关闭。
            
                    
我有这样的一段代码:

przystanki.h

#ifndef PRZYSTANKI_H
#define PRZYSTANKI_H
#include "component.h"
class Przystanki : public Component
{
public:
    Przystanki(QWidget *parent = 0);

signals:
    void deletePosition(QString);
public slots:
    void deleteListItem();
    void addListItem(QString label);
    void createListItem(QString label, DodajPrzystanek* elem);

};

#endif // PRZYSTANKI_H


component.h

#ifndef COMPONENT_H
#define COMPONENT_H
#include <QListWidget>
#include <QGroupBox>
#include "dodajprzystanek.h"
class Component : public QGroupBox
{
    Q_OBJECT
public:
    explicit Component(QString name, QWidget *parent = 0);
    QListWidget* list;

};

#endif // COMPONENT_H


使用它的片段:

MyWindow::MyWindow(QWidget *parent) :
QMainWindow(parent)
{
webView = new MyWebView(this);
mainlayout = new QGridLayout();
mainlayout->addWidget(webView, 0,0);
Przystanki *stop = new Przystanki(this);
mainlayout->addWidget(stop, 0, 1);
QHBoxLayout* bottom = new QHBoxLayout();
//bottom->addWidget(new QLabel("Linie"));
bottom->addWidget(new Component("Autobusy"));
bottom->addWidget(new Component("Linie"));
QHBoxLayout* hrightCorner = new QHBoxLayout();
QVBoxLayout* rightCorner = new QVBoxLayout();
rightCorner->addStretch(1);
rightCorner->addWidget(new QPushButton("Start", this));
//QLabel* label = new QLabel("Labelka");

//rightCorner->addWidget(label);
rightCorner->addStretch(1);
hrightCorner->addLayout(rightCorner);
mainlayout->addLayout(bottom, 1, 0);
mainlayout->addLayout(hrightCorner, 1, 1);
hrightCorner->setAlignment(Qt::AlignCenter);
this->setCentralWidget(new QWidget);
this->centralWidget()->setLayout(mainlayout);
connect(webView, SIGNAL(getMapPosition(QString)), stop, SLOT(addListItem(QString)));
connect(stop, SIGNAL(deletePosition(QString)), webView, SLOT(deleteMarker(QString)));


}


我得到的是:

loaded the Generic plugin
Object::connect: No such slot Component::addListItem(QString)
Object::connect: No such signal Component::deletePosition(QString)


我将插槽的定义和信号从Component移到派生类Przystanki后发生了。我删除了整个构建目录,运行干净,运行qmake并再构建一次,但这没有帮助。谁能向我解释我做错了什么?

最佳答案

您的班级Przystanki缺少Q_OBJECT宏。添加它,将przystanki.h添加到.pro文件中的HEADERS(如果尚不存在)中,然后重新运行qmake。

09-16 15:44