When I must use an "qApp" pointer and when "this" pointer?



  • Hellow, I get started with "http://doc.qt.nokia.com/4.7/gettingstartedqt.html":http://doc.qt.nokia.com/4.7/gettingstartedqt.html Qt course, and I dont understan one thing: When should I use an "qApp" pointer, and when "this" pointer?
    In this example @QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));@ we use na qApp, but in this example:
    @Notepad::Notepad()
    {
    textEdit = new QTextEdit;
    quitButton = new QPushButton(tr("Quit"));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));@
    now we use an this pointer. Why? What is a difference beetween thouse pointers?

    Please help :)



  • Hi,

    you should use qApp when you want to call function/slot from QCoreApplication.

    this is used when you call slot that belongs to the class where you are at that moment.

    May be you are confused because Notepad class has slot quit() and this slot is called on line 5.



  • You use qApp macro (this one is a pointer to the QApplication object) when you want your application to... in your case 'quit' - might be some different functionality specific to the QApplication instance

    And in the case with this pointer - the class Notepad is defined with a slot quit (just a name coincidence - it could be called for example saveTextToFileAndQuit and obviously be coded to save or prompt the use to save)
    //i didn't saw the definition of the slot quit in that tutorial, but i assume it prompt (message box the user) about unsaved text and only then quits the application.



  • qApp is a globally accessible pointer to the QApplication object, this is a pointer to the object for which the member function is called.

    So in your case qApp points to the QApplcation object which was created in <code>main()</code>, this points to the <code>Notepad</code> object whose member function - or constructor in your case - is called.

    So if you want to connect to a slot of the QApplication object use qApp, if you want to connect to a slot of the "current" object use this, or if you want to connect to any other object just use any other pointer that points to a valid QObject subclass.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.