簡體   English   中英

QT將來自不同類的信號和插槽連接到主窗口類?

[英]QT connect signal and slot in from different classes to mainwindow class?

我想在兩個類mainwindowreader之間實現信號和插槽。

reader類中,我聲明了信號SetProgress

閱讀器.h

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

閱讀器.cpp

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

主窗口.h

    public:
    reader *MyReader

private slots:

    void SlotDisplayProgress(QString sActivity_i, int ProgressPercentage_i);

主窗口.cpp

void mainwindow :: SlotDisplayProgress(QString sActivity_i, int nProgressPercentage_i)
{
     this->ui->QprogressBar->setValue(nProgressPercentage_i);
}

在 Mainwidow.cpp 中,我將聲明信號和插槽

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

我嘗試調試,一切正常,直到emit部分。 但是, slot永遠不會執行。

MyReader 是指針嗎? 如果不是這樣,請使用 &MyReader。

嘗試設置 Qt::DirectConnection:

connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress, ***Qt::DirectConnection***);

我遇到了這樣的問題,我連接了信號和插槽,它只在我定義了連接類型時才起作用。

我希望這有幫助。


附注。 我不知道這是否取決於 QT 的版本,但是當我連接信號和插槽時,我編寫的語法如下:

ImageURLLoadListener* downloader = new ImageURLLoadListener(&id, socket);
connect(downloader, SIGNAL(imageLoaded(QString*,QTcpSocket*)), this, SLOT(on_resourceImageDownload(QString*,QTcpSocket*)), Qt::DirectConnection);

不知道有沒有關系...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM