2016-02-16 133 views
-1

我是使用Qt編程的新程序,我需要將信號(例如菜單觸發的信號)與另一個類中的插槽連接起來。這是我的代碼: connect(ui-> actionAbrir,SIGNAL(triggered()),this,SLOT(QImageProc :: open())) 應用程序運行時沒有錯誤,但連接似乎沒有工作,當我啓動應用。將信號連接到不同類中的插槽

+0

歡迎上計算器。發佈前請閱讀本文(http://stackoverflow.com/help/how-to-ask)。我已閱讀下面的線索評論,請爲您的問題提供一個最簡單的示例(http://stackoverflow.com/help/mcve)。此外花2分鐘來學習如何正確編輯你的文章(http://stackoverflow.com/editing-help)。你將有更大的機會得到答案。 – bibi

回答

0

使用Qt 4連接語法時,不需要在SLOT宏中指定類名(QImageProc)。此外,您可以使用返回值檢查連接的正確性。

更新:您的QImageProc類的對象必須通過使用指針或一個部件中,例如加入到MainWindow類:

//MainWindow.h: 
QImageProc* m_pImageProc; 

//MainWindow.cpp:  
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    //somthing like this: 
    m_pImageProc = new QImageProc(/* parameters */); 

    //add m_pImageProc as a third parameter to connect: 
    bool ok = connect(ui->actionAbrir, SIGNAL(triggered()), m_pImageProc, SLOT(open())); 
    //... 

參見:New Signal Slot Syntax in Qt 5

+0

我在MainWindow類中創建了這個連接,但它不起作用 –

+0

@ A.Duarte,您需要展示一個最小工作示例:如何以及在哪裏創建'actionAbrir',您在哪裏創建'connect',你在哪裏定義'open()' –

+0

我在類QImageProc中定義了open(),這是代碼: void QImageProc :: open() { QString fileName = QFileDialog :: getOpenFileName(this,tr(「 Abrir archivo「),QDir :: currentPath()); if(!fileName.isEmpty()) { this-> image = QImage(fileName); } } –