2017-02-09 12 views
3

私はWindowsでQt 5を使用していて、複数のQDialogクラスを持つGUIアプリケーションを構築しています。 QMainWindowクラスのインスタンスが作成された後で、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))); 

と新しい構文のための古い構文の両方を試してみました

「『』トークン前に、プライマリ・表現予想」メインウィンドウは、I作成される(これは以下.cppファイルに示される)

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

main.cppと第2のダイアログはon_action_someAction_triggered()で作成されているので、参照しているインスタンスが存在することがわかります。 SIGNALと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'はタイプです(' new 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()が呼び出されるたびに作成されます。それは避けるべきです。

これが役に立ちます。

関連する問題