简体   繁体   中英

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 .

Inside the reader class I declare the signal SetProgress :

reader.h

class reader :public QObject
    {
        Q_OBJECT    
         signals:
             void SetProgress(QString sOperation, int nPercentage);
}

reader.cpp

 void reader::UpdateProgress(double amount)
{
     int nPrecentage = (100 * amount / (max- min));
     emit SetProgress(m_sCurrentOperation, nPrecentage); 
}

mainwindow.h

    public:
    reader *MyReader

private slots:

    void SlotDisplayProgress(QString sActivity_i, int ProgressPercentage_i);

mainwindow.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

MyReader = reader::New();
connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );

I tried debugging and everything works correctly till the emit part. However, the slot is never executed.

Is MyReader pointer? Use &MyReader if not so.

Try setting 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:

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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