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 graphic 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 <QApplication>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.show();
return app.exec();
}
If you try to send the signal to the same file it was sent from, everything works fine!
Aucun commentaire:
Enregistrer un commentaire