2

I'm trying to centralise some parameters in one class. I looked on several forums and tutorials without finding any real answer. Maybe I don't use Qt correctly.

I have a Qt class Application which handles theses parameters.

Application.h

class Application : public QApplication
{
     public:
        Application(int, char**);
        ~Application();
        bool setFilesPath(QString path);
        QString getFilesPath();

    private:
        QString filesPath;
}

main.cpp

int main(int argc, char *argv[])
{
    Application app(argc, argv);
    MainWindow mainWindow;

    mainWindow.show();

    return app.exec();
}

I'd like to access the setFilesPath() and getFilesPath() methods from everywhere (for exemple in another class. Therefore a made Application inherit from QApplication, hoping that qApp.setFilesPath() would work. But it doesnt.

It seems to be a quite usual thing to access some parameters from everywhere. So how could one do this ?

I've found this three ways :

  • Global variables
  • Static methods
  • Including an instance of Application in every class needing the method (but it seems wrong)

Which is more commonly used (and why) ? Why does my way doesn't work ?

Thank you.

EDIT: In this topic, they use singelton design pattern. So calling

Application *app = Application::getInstance();

everywhere I need it. Is it better ?

6
  • 2
    Look at the QSettings class.
    – sje397
    May 21, 2014 at 7:44
  • I know that. I've found it of course. But it's quite limited. And I may not want to remember this for the next session. I just want to use some parameters in every class. Not to remember it.
    – ochurlaud
    May 21, 2014 at 7:57
  • The singleton seems the way to go, if you need to access the Application instance from everywhere. You can use the build-in sinqleton from QApplication, and downcast it. However, keep in mind a singleton is (almost) a global variable, and thus should be avoided the rest of the time. A question: what is qApp, from qApp.setFilesPath()?
    – Armaghast
    May 21, 2014 at 8:04
  • 1
    qApp was the pointer on the QApplication object (see answer below : link)
    – ochurlaud
    May 21, 2014 at 8:14
  • Why combine it with the application class?
    – sje397
    May 21, 2014 at 13:40

2 Answers 2

2

You can access your class using qApp pointer. According to the Qt documentation:

A global pointer referring to the unique application object. It is equivalent to the pointer returned by the QCoreApplication::instance() function except that, in GUI applications, it is a pointer to a QApplication instance.

So, your code somewhere could look like:

Application *myApp = qobject_cast<Application *>(qApp);
QString path = myApp->getFilesPath();
7
  • Is casting something "pretty" and safe ?
    – ochurlaud
    May 21, 2014 at 8:06
  • I did it and your first line makes an error : erreur : invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>' enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, COUNTER) = sizeof(QStaticAssertFailure<!!(Condition)>)}
    – ochurlaud
    May 21, 2014 at 8:11
  • 2
    @ochurlaud, in this case yes. You can also check myApp against being null, if you want. The error message is because you did not use Q_OBJECT macro in your Application class - this is required to make signals/slots work.
    – vahancho
    May 21, 2014 at 8:21
  • 1
    You need Q_OBJECT for qobject_cast. May 21, 2014 at 8:21
  • @ochurlaud: Btw, if you had used 5.2.0 or later with C++11 support, you would have received a static assert about the missing macro. May 21, 2014 at 8:37
0

You can have your classes in the main independently and connect a signal from each class that needs to access the value in your class to the slot that returns the value :

int main(int argc, char *argv[])
{
    Application app(argc, argv);
    MainWindow mainWindow;

    MyClass myclass1;

    QObject::connect(&mainWindow,SIGNAL(askFilesPath()),myclass1,SLOT(getFilesPath()));

    mainWindow.show();

    return app.exec();
}

This way when you call the signal askFilesPath() in your MainWindow, the slot in your custom class gets called and the value is returned:

QString filePath = askFilesPath();

You should note that if the two classes are in different threads then the connection type should be Qt::BlockingQueuedConnection.

2
  • I think you miss QObject:: from the connect. May 21, 2014 at 8:20
  • I knew this solution but it's means connecting every class to Application. I prefer the other anwser. Thx for your idea, though
    – ochurlaud
    May 21, 2014 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.