You could emit a signal, that a new path was selected and connect it to another component's slot for loading this file. With this, you can get a loose coupling between classes.
Signals are like:"Hey, something has happended. But I don't matter about who cares about that".
Slots are like: "I do that work, but I don't care about who wants me to do that".
fb_test.h
class FB_test : public QMainWindow
{
Q_OBJECT
public:
FB_test(QWidget *parent = 0, Qt::WFlags flags = 0);
~FB_test();
signals:
void newPathSelected(const QString& path);
private:
Ui::FB_testClass ui;
public slots:
void on_pushButton_clicked();
};
fb_test.cpp
// ...
void FB_test::on_pushButton_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null);
if (!path.isEmpty())
emit newPathSelected(path);
}
An an excel class could look like this:
class MyExcelClass : public QObject
{
Q_OBJECT
public:
MyExcelClass(QObject *parent = 0);
~MyExcelClass();
public slots:
void readExcelSheet(const QString& path);
};
If you have instances of both classes in your main.cpp, this file would look like this:
int main(int argc, char** argv)
{
QApplication app(argc, argv);
FB_test fbTest;
MyExcelClass *excelClass = new MyExcelClass(&fbTest);
connect(&fbTest, SIGNAL(newPathSelected(QString)),
excelClass, SLOT(readExcelSheet(QString));
return app.exec();
}
Note that fbTest in main isn't dynamically allocated with new because this will result in a memory leak when main finishes. The excelClass
object will get a pointer to the fbTest object in its constructor, this will ensure, that excelClass
will get deleted as child object of fbTest
.
If both the signal and the slot would be in the same class, you have to connect these in the constructor like this:
connect(this, SIGNAL(newPathSelected(QString)),
this, SLOT(readExcelSheet(QString));