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. QScopedPointer object, signal/slot usage, I am confused.
Forum Update on Monday, May 27th 2025

QScopedPointer object, signal/slot usage, I am confused.

Scheduled Pinned Locked Moved Solved General and Desktop
qcoreappqscopedpointersignal & slot
4 Posts 2 Posters 354 Views
  • 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.
  • P Online
    P Online
    Pl45m4
    wrote on 7 Jun 2024, 03:30 last edited by Pl45m4 6 Jul 2024, 04:01
    #1

    Re: Hybrid Qt GUI/Console cmake application on Windows

    Referring to the above topic of mine.
    Have extended the code to this:

    int main(int argc, char *argv[])
    {
        int exitStatus = EXIT_SUCCESS;
        QScopedPointer<QCoreApplication> app( createApplication(argc, argv) );
    
        // if cast to QApp is successful => GUI mode was enabled
        if (qobject_cast<QApplication *>(app.data())) {
    
            //ShowWindow(GetConsoleWindow(), SW_HIDE);
    
            // start GUI version...
            MainWindow w;
            w.show();
    
            exitStatus = app->exec();
    
        } else {
    
            // start non-GUI version...
    
            // QStringList args = QCoreApplication::arguments();
            // parseArgs(args);
    
            BackendWorker worker;
    
            // Quit application when work is finished
            QObject::connect(&worker, &BackendWorker::finished, app.data(), &QCoreApplication::exit);  // [ 1 ]
            // QObject::connect(&worker, &BackendWorker::finished, app, &QCoreApplication::exit); // [ 2 ]
    
            QTimer::singleShot(0, &worker, &BackendWorker::start);
    
            exitStatus = app->exec(); // [ 3 ]
            // exitStatus = app.data()->exec(); // [ 4 ]
    
        }
        return exitStatus;
    }
    

    I'm confused about the connection part where [ 1 ] works and [ 2 ] does not.
    Whereas both of [ 3 ] and [ 4 ] work and do the same.
    And this is why it confuses me a little.

    Uncommenting [ 2 ] throws:

    error: no matching function for call to 'QObject::connect(BackendWorker *, void (BackendWorker::*)(int),
    QScopedPointer<QCoreApplication>&, void (*)(int))'

    69 | QObject::connect(&worker, &BackendWorker::finished, app, &QCoreApplication::exit);
    ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Typing app->... even auto-completes/suggests functions like exec(), quit() or exit() (all part of QCoreApplication API)
    I know these suggestions mean nothing :)

    But if you are able to call QCoreApplication::exec on QScopedPointer<QCoreApplication> app as in [ 3 ], why you can't connect a signal like that (as in [ 2 ]) ?


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 7 Jun 2024, 04:15 last edited by
      #2

      3 and 4 are the same because of this operator overload: https://doc.qt.io/qt-6/qscopedpointer.html#operator--gt

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply 7 Jun 2024, 04:23
      1
      • C Christian Ehrlicher
        7 Jun 2024, 04:15

        3 and 4 are the same because of this operator overload: https://doc.qt.io/qt-6/qscopedpointer.html#operator--gt

        P Online
        P Online
        Pl45m4
        wrote on 7 Jun 2024, 04:23 last edited by
        #3

        @Christian-Ehrlicher said in QScopedPointer object, signal/slot usage, I am confused.:

        3 and 4 are the same because of this operator overload: https://doc.qt.io/qt-6/qscopedpointer.html#operator--gt

        lol, thank you, totally missed that.
        Didn't even know that -> is an operator which you can overload :o
        Same with . - operator (as I'm seeing it now)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • P Pl45m4 has marked this topic as solved on 7 Jun 2024, 04:23
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 7 Jun 2024, 04:40 last edited by Christian Ehrlicher 6 Jul 2024, 04:40
          #4

          You can do nasty things with this. Overload operator + for integers to surprise your colleagues for example 😁

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          2/4

          7 Jun 2024, 04:15

          • Login

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