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. QSound wont work, because "return a.exec();" will never be executed.
Forum Updated to NodeBB v4.3 + New Features

QSound wont work, because "return a.exec();" will never be executed.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 718 Views 2 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.
  • B Offline
    B Offline
    BD9a
    wrote on 12 Aug 2019, 01:10 last edited by
    #1

    Hello. I want to do Loop in main function what one time hide window, one time play sound and Looping random cursor position but if "return a.exec(); will never be executed" my QSound dont play sound.

    #include <QCoreApplication>
    #include <iostream>
    #include <windows.h>
    #include <QSound>
    #include <cstdlib>
    
    void noEscape(){
        INPUT input[2];
        ZeroMemory(input, sizeof(input));
        input[0].type = input[1].type = INPUT_KEYBOARD;
        input[0].ki.wVk = input[1].ki.wVk = VK_ESCAPE;
        input[1].ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(2, input, sizeof(INPUT));
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        int width = GetSystemMetrics(SM_CXSCREEN);
        int height = GetSystemMetrics(SM_CYSCREEN);
    
        HWND hWnd = GetConsoleWindow();
        ShowWindow( hWnd, SW_HIDE);
    
        QSound::play(":/Untitled.wav");
    
        while(true){
    
            SetCursorPos(rand () % (width + 1), rand () % (height +1));
    
            noEscape();
    
            if(GetAsyncKeyState(VK_HOME)){
                exit(0);
            }
    
        }
    
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      BD9a
      wrote on 12 Aug 2019, 17:00 last edited by
      #8

      Thanks, working. Used QTimer.

      P 1 Reply Last reply 12 Aug 2019, 18:17
      0
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 12 Aug 2019, 04:56 last edited by
        #2

        QSound can also do it's job when the eventloop is reached which will never be the case due to your while(true) loop.

        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
        3
        • B Offline
          B Offline
          BD9a
          wrote on 12 Aug 2019, 12:24 last edited by
          #3

          So, where I have to place this QSound if It's endless loop?

          P 1 Reply Last reply 12 Aug 2019, 12:38
          0
          • B BD9a
            12 Aug 2019, 12:24

            So, where I have to place this QSound if It's endless loop?

            P Offline
            P Offline
            Pl45m4
            wrote on 12 Aug 2019, 12:38 last edited by
            #4

            @bd9a

            Why do you need to call noEscape() so frequently?

            Check https://doc.qt.io/qt-5/qsound.html#isFinished and https://doc.qt.io/qt-5/qsound.html#setLoops


            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
            1
            • B Offline
              B Offline
              BD9a
              wrote on 12 Aug 2019, 13:39 last edited by
              #5

              I want to use QSound::play only once. I want this loop because it's "core" of this app. QSound dont work because "return a.exec(); will never be executed."...

              P 1 Reply Last reply 12 Aug 2019, 14:44
              0
              • B BD9a
                12 Aug 2019, 13:39

                I want to use QSound::play only once. I want this loop because it's "core" of this app. QSound dont work because "return a.exec(); will never be executed."...

                P Offline
                P Offline
                Pl45m4
                wrote on 12 Aug 2019, 14:44 last edited by
                #6

                @bd9a

                I would create a worker thread which will start doing its work after the QSoundwas played.
                You can connect / start the noEscape-Worker in your main


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

                ~E. W. Dijkstra

                B 1 Reply Last reply 12 Aug 2019, 16:12
                3
                • P Pl45m4
                  12 Aug 2019, 14:44

                  @bd9a

                  I would create a worker thread which will start doing its work after the QSoundwas played.
                  You can connect / start the noEscape-Worker in your main

                  B Offline
                  B Offline
                  BD9a
                  wrote on 12 Aug 2019, 16:12 last edited by
                  #7
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BD9a
                    wrote on 12 Aug 2019, 17:00 last edited by
                    #8

                    Thanks, working. Used QTimer.

                    P 1 Reply Last reply 12 Aug 2019, 18:17
                    0
                    • B BD9a
                      12 Aug 2019, 17:00

                      Thanks, working. Used QTimer.

                      P Offline
                      P Offline
                      Pablo J. Rogina
                      wrote on 12 Aug 2019, 18:17 last edited by
                      #9

                      @bd9a said in QSound wont work, because "return a.exec();" will never be executed.:

                      Thanks, working.

                      please don't forget to mark your post as solved! Thanks.

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0

                      5/9

                      12 Aug 2019, 13:39

                      • Login

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