Question

As you can find (if you look at few of mine previous questions) I am pretty new with Qt. I am making "read-from-excel-file-push-to-some-DB" module.

I want to get path wich push button returns in main.cpp

So, long story short here's my code:

fb_test.h

#ifndef FB_TEST_H
#define FB_TEST_H

#include <QtGui/QMainWindow>
#include "ui_fb_test.h"

class FB_test : public QMainWindow
{
    Q_OBJECT

public:
    FB_test(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~FB_test();

private:
    Ui::FB_testClass ui;

public slots:
    QString on_pushButton_clicked();

};

#endif // FB_TEST_H

fb_test.cpp

#include <QtGui>
#include "fb_test.h"

FB_test::FB_test(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);

}

FB_test::~FB_test()
{

}

QString FB_test::on_pushButton_clicked()
{
    QString path;

    path = QFileDialog::getOpenFileName(
        this,
        "Choose a file to open",
        QString::null,
        QString::null);

    return path;
}

main.cpp

    #include <QApplication>
    #include <QtGui>
    #include <QtSql>
    #include <QAxObject>
    #include <QAxWidget>
    #include "fb_test.h"

    bool initExcel(QAxObject* &excelApp);
    void getTableHeaders(QAxObject* _worksheet, QAxObject*  _excel);
    bool readExcelFile(QAxObject* excel, QString& file_path, QString& selected_list);
    void getTableHeaders(QAxObject* _worksheet, QAxObject*  _excel);

    //....
    //here's methods above implementation 
    //....
    void excelTest(){
        QAxObject* excel;

        QString path = QString("C:\\databases\\oilnstuff.xls");//gonna use GUI choose
        QString list = QString("list1");

        if(initExcel(excel)){
            if (readExcelFile(excel, path, list)){
                //
            }else{
                //error output
            }
        }

        excel->dynamicCall("Quit");
        delete excel;
    }

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    QComboBox myCombo;

    FB_test *test = new FB_test;
    test->show();


    excelTest();

    return app.exec();
}

so here, instead of such right part QString path = QString("C:\\databases\\oilnstuff.xls");

I want to get what QString FB_test::on_pushButton_clicked() returns.

How to make such thing?

UPD: well, such way not works

QString mypath = test->ui.pushButton();

P.S.

Oh and btw, I am not sure about the way I degin this module. Maybe I should move all working stuff from main.cpp into FB_test.cpp and get what button returns there and only call show() in main.cpp?

Était-ce utile?

La solution

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));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top