Search code examples
c++qtsignals-slotsqt-connection

Qt - connecting a signal/slot in a triggered action


I'm using Qt 5 on a Windows and building a GUI App with multiple QDialog classes. I am trying to connect a signal from a QDialog in a triggered action of the QMainWindow class after instances of both have been created. I have read the documentation on Qt here: http://doc.qt.io/qt-4.8/signalsandslots.html and here: https://wiki.qt.io/New_Signal_Slot_Syntax. I have also read through many questions on stackoverflow that have helped correct some of the initial errors I was getting, but haven't helped me solve this problem.

The error I keep getting is:

"expected primary-expression before ',' token"

I have tried both the old syntax for connect

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

and the new syntax (which is shown in the .cpp file below)

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

The MainWindow is created in the main.cpp and the 2nd dialog is created on_action_someAction_triggered(), so I know that the instances I am referencing exist. Is there a better way for me to connect the SIGNAL and the SLOT?

Here is the code I am working with (minus the extra unrelated code).

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

}

Solution

  • I think, the code should be

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

    and it should be placed in the constructor of the MainWindow, right after ui->setupUi(this); and the on_actionNew_Ship_triggered() function should look like this:

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

    In your original code, a new instance of shipdia will be created everytime the on_actionNew_Ship_triggered() is called. That should be avoided.

    Hope this helps.