简体   繁体   English

未收到Qt发射信号

[英]Qt emitted signal is not received

I'm not able to receive my custom signal in the supposed SLOT . 我无法在假定的SLOT接收到自定义信号。 Here is my code: 这是我的代码:

mainwindow.h : 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 : mainwindow.cpp

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

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

And this is how I emit the signal: 这就是我发出信号的方式:

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);

However, qDebug() << "historyLoaded()..."; 但是, qDebug() << "historyLoaded()..."; is never executed. 永远不会执行。

Any ideas what the problem could be? 任何想法可能是什么问题?

It seems you're using threads. 看来您正在使用线程。 Using QList when signaling across threads (or using Qt::QueuedConnection in general) requires some extra work. 跨线程发信号时使用QList (或通常使用Qt::QueuedConnection )需要一些额外的工作。 Basically you need to define the QList<T> type using typedef and then register it using qRegisterMetaType : 基本上,您需要使用typedef定义QList<T>类型,然后使用qRegisterMetaType进行注册:

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

Then use this type in your signals and slots: 然后在信号和插槽中使用此类型:

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

Check return value of your connect , it should fail. 检查connect返回值,它应该失败。 There is an extra * in SIGNAL(historyLoaded(QList<HistoryItem*>*)) , should be SIGNAL(historyLoaded(QList<HistoryItem*>)) . SIGNAL(historyLoaded(QList<HistoryItem*>*))有一个额外的* ,应为SIGNAL(historyLoaded(QList<HistoryItem*>)) Fix your SLOT() too. 也修复您的SLOT()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM