【问题标题】:Qt emitted signal is not received未收到 Qt 发出的信号
【发布时间】:2023-03-30 23:02:01
【问题描述】:

我无法在假定的SLOT 中接收我的自定义信号。这是我的代码:

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() &lt;&lt; "historyLoaded()..."; 永远不会被执行。

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

【问题讨论】:

标签: c++ qt


【解决方案1】:

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

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

然后在你的信号和槽中使用这个类型:

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

【讨论】:

    【解决方案2】:

    检查您的connect 的返回值,它应该会失败。 SIGNAL(historyLoaded(QList&lt;HistoryItem*&gt;*))中多了一个*,应该是SIGNAL(historyLoaded(QList&lt;HistoryItem*&gt;))。也修复你的SLOT()

    【讨论】:

    • 更改为 connect(dbtrad, SIGNAL(historyLoaded(QList&lt;HistoryItem*&gt;)), this, SLOT(historyLoaded(QList&lt;HistoryItem*&gt;))); 但还是一样
    • 您的广告位是historyLoaded(const QList&lt;HistoryItem*&gt;)。从插槽中删除 const 或将其添加到信号中。并检查您的 connect 返回什么,直到它返回 true 您的插槽不会被触发。
    猜你喜欢
    • 2020-04-12
    • 2016-04-20
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多