Qt connect “no such slot” when slot exists [duplicate]

不羁的心 提交于 2019-12-24 16:27:17

问题


I am trying to connect a signal to a slot. The project compiles fine, but at runtime I get this error:

QObject::connect: No such slot QHeaderView::onFilterAdded(int)

here is my code:

class MySortFilterProxyModel: public QSortFilterProxyModel
{
Q_OBJECT
public:
    explicit MySortFilterProxyModel(QObject *parent = 0);
    ~MySortFilterProxyModel();
    void addFilter(int col, SteFilter *pFilter);
    void removeFilter(int col);
signals:
    void filterAdded(int);
    void filterRemoved(int);
}

class MyHeaderView: public QHeaderView
{
public:
    MyHeaderView();
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;

public slots:
    void onFilterAdded(int);
    void onFilterRemoved(int);

private:
    QList<int> m_listFilters;
};

I use this line of code to connect the signal to the slot:

QObject::connect(&m_proxyModel, SIGNAL(filterAdded(int)), &m_headerView, SLOT(onFilterAdded(int)));

m_proxyModel is of type MySortFilterProxyModel and m_headerView is of type MyHeaderView. They are not pointers.

I don't get why this happens. I have connected other signals and slots using the same technique and had no problems. Any help would be appreciated, thanks.


回答1:


The class MyHeaderView doesn't have Q_OBJECT macro, don't forget to run qmake after you add it and only after that build your project.



来源:https://stackoverflow.com/questions/20749757/qt-connect-no-such-slot-when-slot-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!