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.

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 705 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.
  • B Offline
    B Offline
    BD9a
    wrote on 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 last edited by
      #8

      Thanks, working. Used QTimer.

      Pablo J. RoginaP 1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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 last edited by
          #3

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

          Pl45m4P 1 Reply Last reply
          0
          • B BD9a

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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on 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 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."...

              Pl45m4P 1 Reply Last reply
              0
              • B BD9a

                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."...

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on 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
                3
                • Pl45m4P Pl45m4

                  @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 last edited by
                  #7
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BD9a
                    wrote on last edited by
                    #8

                    Thanks, working. Used QTimer.

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • B BD9a

                      Thanks, working. Used QTimer.

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on 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

                      • Login

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