gpt4 book ai didi

c++ - QT5 中的信号和插槽

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:40 25 4
gpt4 key购买 nike

我遇到了以下问题:

我有一个 GUI 元素类和一个 Thread 类。我想在它们之间交换信号。然而,它只在一个方向上起作用。我可以成功地将信号从主 GUI 窗口发送到线程。但是,当我想从线程向调用线程的进程发送信号时,它不起作用。这是我的做法:

bool GfxMapItem::mapSwitch(int seqidx)
{
...
importMapThread_ = new ImportMapThread(sequence_->item(seqidx));
connect(GUI_UI->analExportButton, SIGNAL(clicked()), importMapThread_, SLOT(interrupt1()), Qt::QueuedConnection); //<-- this one works perfectly
connect(importMapThread_, SIGNAL(progress(int)), this, SLOT(loadMap(int)), , Qt::QueuedConnection); // <-- this one unfortunatelly doesn't
...
}

线程代码非常简单,它只发出信号(当我将线程与自身连接时,我检查过它是否有效)。

void ImportMapThread::run()
{
QLOGX("Running ImportMapThread...");
QLOGINC;
emit this->progress(100);

QLOGX("Stopping ImportMapThread...");
}

线程标题如下所示:

class ImportMapThread : public InterruptibleThread
{
Q_OBJECT

private:
...

protected:
...

public:
ImportMapThread(SeqItem *p);

signals:
void progress(int value);
void progressReset(int value);

public slots:
virtual void interrupt1();
void loadMap();

protected:
virtual void run();
};

调用该线程的类如下所示:

class GfxMapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
...

signals:
void mouseOutside();
void mouseMove(const QPointF &pos);

private slots:
void loadMap(int);

protected:
...
};

有什么想法吗?拜托,我已经没有想法了。

最佳答案

我相信,您应该在运行实现中调用 exec。 Exec 启动事件循环,应该允许发送信号。

看看:

int QThread::exec() [protected] Enters the event loop and waits until exit() is called, returning the value that was passed to exit(). The value returned is 0 if exit() is called via quit(). It is necessary to call this function to start event handling.

希望对您有所帮助。

关于c++ - QT5 中的信号和插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19747657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com