Search code examples
c++qtqt5qt-creator

Why doesn't qDebug output anything after clicking the button?


I'm trying to implement one thing with signals and slots, and the problem arises that after pressing the button nothing happens, although the path to the file should be displayed. The button is made using the Qt Designer.

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "mainwindow.h"
#include "model.h"
#include <QMainWindow>
#include <QObject>
#include <QDebug>

class Controller : public QObject
{
    Q_OBJECT

public:

explicit Controller(QObject *parent = nullptr);
~Controller();

public slots:
    void onFileSelected(const QString& filePath);

private:
    // Model* model = nullptr;
    MainWindow* view = nullptr;
};

#endif // CONTROLLER_H

controller.cpp:

#include "controller.h"

Controller::Controller(QObject *parent)
    : QObject(parent)
{
    // model = new Model(this);
    view = new MainWindow();

    connect(view, &MainWindow::fileSelected, this, &Controller::onFileSelected);
}

Controller::~Controller()
{
    // delete model;
    delete view;
}

void Controller::onFileSelected(const QString& filePath)
{
     qDebug() << "File selected: " << filePath;
     // qDebug() << model->getHeaders();
}

mainwindow.h:

ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
   Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
   ~MainWindow();

signals:
    void fileSelected(const QString& filePath);

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QDebug>
#include <QMessageBox>
#include <QtCharts>

using namespace QtCharts;


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

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


void MainWindow::on_pushButton_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, "choose file", QDir::currentPath());

    emit fileSelected(filePath);
}

main.cpp:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setMinimumSize(1200, 900);
    w.show();
    return a.exec();
}

If you try to send the signal to the same file it was sent from, everything works fine!


Solution

  • Igor Tandetnik said:

    I suspect you are creating an instance of MainWindow outside of and independent from Controller. The instance where you click the button is not the same instance that Controller connected its slot to.

    You should do the opposite of what you're doing, instantiate a Controller in MainWindow, and connect signals to slots there.

    Example:

    MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Form)
    {
        ui->setupUi(this);
    
        Controller *controller = new Controller;
    
        connect(this, &
    MainWindow::fileSelected, controller, &Controller::onFileSelected);
    }
    

    And remove MainWindow object from Controller's class.