[英]QT connect signal and slot in from different classes to mainwindow class?
I want to implement signal and slot between two classes mainwindow
and reader
.我想在两个类mainwindow
和reader
之间实现信号和插槽。
Inside the reader
class I declare the signal SetProgress
:在reader
类中,我声明了信号SetProgress
:
reader.h阅读器.h
class reader :public QObject
{
Q_OBJECT
signals:
void SetProgress(QString sOperation, int nPercentage);
}
reader.cpp阅读器.cpp
void reader::UpdateProgress(double amount)
{
int nPrecentage = (100 * amount / (max- min));
emit SetProgress(m_sCurrentOperation, nPrecentage);
}
mainwindow.h主窗口.h
public:
reader *MyReader
private slots:
void SlotDisplayProgress(QString sActivity_i, int ProgressPercentage_i);
mainwindow.cpp主窗口.cpp
void mainwindow :: SlotDisplayProgress(QString sActivity_i, int nProgressPercentage_i)
{
this->ui->QprogressBar->setValue(nProgressPercentage_i);
}
inside Mainwidow.cpp I will declare signal and slot在 Mainwidow.cpp 中,我将声明信号和插槽
MyReader = reader::New();
connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );
I tried debugging and everything works correctly till the emit
part.我尝试调试,一切正常,直到emit
部分。 However, the slot
is never executed.但是, slot
永远不会执行。
Is MyReader pointer? MyReader 是指针吗? Use &MyReader if not so.如果不是这样,请使用 &MyReader。
Try setting Qt::DirectConnection:尝试设置 Qt::DirectConnection:
connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress, ***Qt::DirectConnection***);
I had a problem like this, where I connected the signal and slot, and it only worked when I defined the type of connection.我遇到了这样的问题,我连接了信号和插槽,它只在我定义了连接类型时才起作用。
I hope this helps.我希望这有帮助。
PS.附注。 I don't know if this depends on the version of QT but when I connect signals and slots the syntax I write is the following:我不知道这是否取决于 QT 的版本,但是当我连接信号和插槽时,我编写的语法如下:
ImageURLLoadListener* downloader = new ImageURLLoadListener(&id, socket);
connect(downloader, SIGNAL(imageLoaded(QString*,QTcpSocket*)), this, SLOT(on_resourceImageDownload(QString*,QTcpSocket*)), Qt::DirectConnection);
I don't know if it's related or not...不知道有没有关系...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.