2014-12-02 56 views
1

我在假定的SLOT中无法接收到我的自定义信号。这里是我的代码:未收到Qt发出的信号

mainwindow.h

class HistoryItem { 
    public: 
     QString channel; 
}; 


class dbThread : public QObject 
{ 
    Q_OBJECT 

public: 
    dbThread(); 

signals: 

void historyLoaded(QList<HistoryItem*> innerResult); 


class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

public slots: 
    void historyLoaded(const QList<HistoryItem*> innerResult); 

mainwindow.cpp

connect(dbtrad, SIGNAL(historyLoaded(QList<HistoryItem*>*)), this, SLOT(historyLoaded(QList<HistoryItem*>*))); 

void MainWindow::historyLoaded(QList<HistoryItem*> innerResult) { 
    qDebug() << "historyLoaded()..."; 
} 

而且这是我发出信号:

QList<HistoryItem*> innerResult; 
while (queryInner.next()) { 
    QString channelIDInner = queryInner.value(0).toString(); 

    HistoryItem* item = new HistoryItem(); 
    item->channel = channelIDInner; 

    innerResult.append(item); 
} 
qDebug() << "DONE LOADING....."; 
emit historyLoaded(innerResult); 

然而,从不执行qDebug() << "historyLoaded()...";

任何想法可能是什么问题?

+2

退房http://stackoverflow.com/:

typedef QList<HistoryItem*> HistoryList_t; ... qRegisterMetaType<HistoryList_t>("HistoryList_t"); 

然后在你的信号和槽使用这种类型question/26422154/my-slot-is-not-invoked-called-used-working-executed – Silicomancer 2014-12-02 12:20:55

+2

您确定信号/插槽机制可以处理模板吗? http://qt-project.org/doc/qt-4.8/templates.html – vahancho 2014-12-02 12:25:22

+0

如果你在Qt5或以上,你可以使用'connect(dbtrad,&dbThread :: historyLoaded,this,&MainWindow :: historyLoaded);' – 2014-12-02 12:44:57

回答

1

检查您的connect的返回值,它应该失败。 SIGNAL(historyLoaded(QList<HistoryItem*>*))中有一个额外的*,应该是SIGNAL(historyLoaded(QList<HistoryItem*>))。修复你的SLOT()

+0

更改为'connect(dbtrad,SIGNAL(historyLoaded(QList )),this,SLOT(historyLoaded(QList )));'但仍然相同 – Alosyius 2014-12-02 12:21:42

+0

您的插槽是'historyLoaded(const QList )'。从槽中删除'const'或将其添加到信号中。并检查你的'connect'返回的内容,直到它返回true,你的插槽将不会被触发。 – Paul 2014-12-02 12:37:36

2

看来你正在使用线程。跨线程信号传输时(或通常使用Qt::QueuedConnection)使用QList需要一些额外的工作。基本上你需要使用typedef定义QList<T>类型,然后使用qRegisterMetaType注册它:

public slots: 
    void historyLoaded(const HistoryList_t &list);