Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. When I must use an "qApp" pointer and when "this" pointer?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 14.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yuki
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      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.

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • ZlatomirZ Offline
        ZlatomirZ Offline
        Zlatomir
        wrote on last edited by
        #3

        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.

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved