2017-02-09 69 views
3

我在Windows上使用Qt 5並構建具有多個QDialog類的GUI應用程序。我試圖在QMainWindow類的一個觸發動作中創建一個QDialog的實例後,連接一個信號。我在這裏閱讀了關於Qt的文檔:http://doc.qt.io/qt-4.8/signalsandslots.html和這裏:https://wiki.qt.io/New_Signal_Slot_Syntax。我也閱讀了很多關於stackoverflow的問題,這些問題幫助我糾正了一些最初的錯誤,但並沒有幫助我解決這個問題。Qt - 在觸發的動作中連接信號/插槽

我不斷收到的錯誤是:

「之前的預期‘’令牌主表達式」

我曾經嘗試都舊語法連接

connect(sender, SIGNAL (valueChanged(QString,QString)), 
receiver, SLOT (updateValue(QString))); 

和新語法(顯示在下面的.cpp文件中)

connect(sender, &Sender::valueChanged, 
receiver, &Receiver::updateValue); 

MainWindow創建成i在main.cpp和第二個對話框中創建on_action_someAction_triggered(),所以我知道我所引用的實例存在。有沒有更好的方式來連接信號和SLOT?

這是我正在使用的代碼(減去額外的無關代碼)。

主窗口的.h:

#include <QMainWindow> 
#include "shipdia.h" 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

public slots: 

    void loadSelectedShip(QString shipName); 

private slots: 

    void on_actionNew_Ship_triggered(); 

private: 
    Ui::MainWindow *ui; 
    shipdia *sDialog; 

}; 

#endif // MAINWINDOW_H 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include <QTextStream> 
#include <QObject> 


MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::on_actionNew_Ship_triggered() 
{ 
    sDialog = new shipdia(this); 
    QObject::connect(&shipdia,     //this is were I attempt to 
     &shipdia::sendShip,     //connect the signal/slot 
     this,&MainWindow::loadSelectedShip); //but always get an error 
    sDialog ->show(); 

} 

void MainWindow::loadSelectedShip(QString shipName) 
{ 
    ... do something ... //this code works, but the signal is never received 
} 

qdialog.h

#ifndef SHIPDIA_H 
#define SHIPDIA_H 

#include "functions.h" 
#include <QDialog> 

namespace Ui { 
class shipdia; 
} 

class shipdia : public QDialog 
{ 
    Q_OBJECT 

public: 
    explicit shipdia(QWidget *parent = 0); 
    ~shipdia(); 

private slots: 

    void on_pushButton_2_clicked(); 

signals: 
    void sendShip(QString shipName); 

private: 
    Ui::shipdia *ui; 
}; 

#endif // SHIPDIA_H 

qdialog.cpp

#include "shipdia.h" 
#include "ui_shipdia.h" 
#include <QObject> 
#include <QMessageBox> 
#include <QTextStream> 
#include <QDir> 

shipdia::shipdia(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::shipdia) 
{ 
    ui->setupUi(this); 
} 

shipdia::~shipdia() 
{ 
    delete ui; 
} 

void shipdia::sendSelectedShip(QString shipName) 
{ 
    emit sendShip(shipName); //I previously just emitted sendSelectedShip, 
          //but separating the function did not fix it. 
} 

void shipdia::on_pushButton_2_clicked() 
{ 
    //Code below functions up to next comment 
    QString shipName = ui->line_23->text(); 
    shipName = QDir::currentPath() + "/shipFolder/" + shipName + ".txt"; 
    QFile shipFile(shipName); 
    QStringList stringList; 

    if (shipFile.open(QIODevice::ReadOnly)) 
    { 
     QTextStream in(&shipFile); 
     while(!in.atEnd()) 
     { 
      QString line = in.readLine(); 
      if(line.isNull()) 
       break; 
      else 
       stringList.append(line); 
     } 
     shipFile.close(); 
    } 

    //Code above functions^

    sendSelectedShip(shipName); //this line does not produce an error 

} 
+1

'shipdia'是一種類型(如果'新船'...是相信的)。 'connect(&shipdia,...)'因此沒有任何意義。你可能意思是'connect(sDialog,...)' –

回答

1

我認爲,將碼應該是

sDialog = new shipdia(this); 
QObject::connect(sDialog,    
     &shipdia::sendShip,this,&MainWindow::loadSelectedShip); 

並且應當被放置在主窗口的構造,後右UI-> setupUi(本);和on_actionNew_Ship_triggered()函數應該是這樣的:

void MainWindow::on_actionNew_Ship_triggered() 
{ 
    sDialog ->show(); 
} 

在你的原代碼,shipdia的新實例將每次創建on_actionNew_Ship_triggered()被調用。這應該避免。

希望這會有所幫助。