August 8, 2011

ogopa ogopa
Lab Rat
87 posts

QMainWIndow not popping up when I run

Page  
1

I have created a program which builds and runs without errors but no window pops up when I run. Can someone please tell me why.
My main function is below:

  1. int main(int argc, char *argv[])
  2.  {
  3.          struct option long_option[] =
  4.          {
  5.                  {"help", 0, NULL, 'h'},
  6.                  {"device", 1, NULL, 'D'},
  7.                  {"rate", 1, NULL, 'r'},
  8.                  {"channels", 1, NULL, 'c'},
  9.                  {"frequency", 1, NULL, 'f'},
  10.                  {"buffer", 1, NULL, 'b'},
  11.                  {"period", 1, NULL, 'p'},
  12.                  {"method", 1, NULL, 'm'},
  13.                  {"format", 1, NULL, 'o'},
  14.                  {"verbose", 1, NULL, 'v'},
  15.                  {"noresample", 1, NULL, 'n'},
  16.                  {"pevent", 1, NULL, 'e'},
  17.                  {NULL, 0, NULL, 0},
  18.          };
  19.  
  20.          int err;
  21.          snd_pcm_hw_params_t *hwparams;
  22.          snd_pcm_sw_params_t *swparams;
  23.          int method = 0;
  24.          snd_pcm_t *handle;
  25.          snd_pcm_channel_area_t *areas;
  26.          signed short *samples;
  27.          unsigned int chn;
  28.  
  29.          snd_pcm_hw_params_alloca(&hwparams);
  30.          snd_pcm_sw_params_alloca(&swparams);
  31.  
  32.                 err = snd_output_stdio_attach(&output, stdout, 0);
  33.          if (err < 0) {
  34.                  printf("Output failed: %s\n", snd_strerror(err));
  35.                  return 0;
  36.          }
  37.  
  38.    /*      cout<<"Playback device is %s  "<<","<< device);
  39.          cout<<"Stream parameters are %iHz, %s, %i channels  "<<","<< rate<<","<< snd_pcm_format_name(format), channels;
  40.         cout<<"Sine wave rate is %.4fHz  "<<","<< freq;
  41.          cout<<"Using transfer method: %s  "<<","<< transfer_methods[method].name;
  42.  */
  43.          if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  44.                  printf("Playback open error: %s\n", snd_strerror(err));
  45.                  return 0;
  46.          }
  47.  
  48.          if ((err = set_hwparams(handle, hwparams, transfer_methods[method].access)) < 0) {
  49.                  printf("Setting of hwparams failed: %s\n", snd_strerror(err));
  50.                  exit(EXIT_FAILURE);
  51.          }
  52.          if ((err = set_swparams(handle, swparams)) < 0) {
  53.                  printf("Setting of swparams failed: %s\n", snd_strerror(err));
  54.                  exit(EXIT_FAILURE);
  55.          }
  56.  
  57.          if (verbose > 0)
  58.                  snd_pcm_dump(handle, output);
  59.  
  60.          samples = new signed short[(period_size * channels * snd_pcm_format_physical_width(format))];
  61.  
  62.  
  63.  
  64.          if (samples == NULL) {
  65.                  printf("Not enough memory\n");
  66.                  exit(EXIT_FAILURE);
  67.          }
  68.  
  69.          areas = new snd_pcm_channel_area_t [channels];
  70.  
  71.          if (areas == NULL) {
  72.                  printf("Not enough memory\n");
  73.                  exit(EXIT_FAILURE);
  74.          }
  75.          for (chn = 0; chn < channels; chn++) {
  76.                  areas[chn].addr = samples;
  77.                  areas[chn].first = chn * snd_pcm_format_physical_width(format);
  78.                  areas[chn].step = channels * snd_pcm_format_physical_width(format);
  79.          }
  80.  
  81.  
  82.          err = transfer_methods[method].transfer_loop(handle, samples, areas);
  83.          if (err < 0)
  84.                  printf("Transfer failed: %s\n", snd_strerror(err));
  85.  
  86.          delete [] areas;
  87.          delete [] samples;
  88.          snd_pcm_close(handle);
  89.  
  90.  
  91.  
  92. QApplication a(argc, argv);
  93. wave w;
  94. w.setWindowTitle("ESG Wave Creator");
  95.  
  96.  
  97. w.show();
  98.  
  99.     return a.exec();
  100.     return 0;
  101.  
  102. }

24 replies

August 8, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

It looks like your code freezes somewhere before w.show() and return a.exec().
Try to remove code before 92 line and see if it will help.

August 8, 2011

ogopa ogopa
Lab Rat
87 posts
Denis Kormalev wrote:
It looks like your code freezes somewhere before w.show() and return a.exec(). Try to remove code before 92 line and see if it will help.

Thanks for the reply. It works fine and the window pops up when I remove the code before line 92.
I put all the code back and it builds fine still but when I run it now, I get the following in my output pane and the window doesn’t pop up:

  1. Starting /home/test/Documents/Wave-build-desktop/Wave...
  2. Playback open error: Device or resource busy
  3. /home/test/Documents/Wave-build-desktop/Wave exited with code 0

August 8, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

try to debug it or place qDebug calls in your code to find out where is the problem.

August 8, 2011

ogopa ogopa
Lab Rat
87 posts
Denis Kormalev wrote:
try to debug it or place qDebug calls in your code to find out where is the problem.

The problem is line 43 in my int main() above. I’m not sure how to fix this error. WHen I ran this code last week, it wasn’t giving me such an error. wierd. anyway, this is line 43:

  1. if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  2.                  printf("Playback open error: %s\n", snd_strerror(err));
  3.                  return 0;
  4.          }

August 8, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Looks like your sound device is busy. I think you will quicker find answer on related forums, but maybe someone here will be able to help you to.

August 8, 2011

ogopa ogopa
Lab Rat
87 posts
Denis Kormalev wrote:
Looks like your sound device is busy. I think you will quicker find answer on related forums, but maybe someone here will be able to help you to.

Thanks for your help and the quick replies. :)

August 8, 2011

ogopa ogopa
Lab Rat
87 posts
Denis Kormalev wrote:
Looks like your sound device is busy. I think you will quicker find answer on related forums, but maybe someone here will be able to help you to.

Hi again Denis,
I resolved the problem with the sound device, you were right, it was busy being used somewhere else.
However, with this resolved, back to the original question :). There is still no window that pops up when I run the program. There are no error messages in the output pane this time.
Could you please help me

August 8, 2011

ogopa ogopa
Lab Rat
87 posts

I tried debugging my program: I placed the line

  1. return a.exec;
which is the line that executes the application and pops up the window. I placed this line at different parts of the code in int main() to see what line was stopping it from popping up. The window popped up when I placed it in all parts of the code starting from the top, but it stopped at the line:
  1. err = transfer_methods[method].transfer_loop(handle, samples, areas);
  2. if (err < 0)
  3. printf("Transfer failed: %s\n", snd_strerror(err));

This is the line of code that stops my window from popping up. I don’t know why. Any help would be greatly appreciated.

August 8, 2011

mlong mlong
Mad Scientist
1517 posts

Be sure and use @ tags instead of [CODE] tags to wrap your code.

Edit: Nevermind.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 8, 2011

mlong mlong
Mad Scientist
1517 posts

Are you getting the “Transfer failed” error message?

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 8, 2011

ogopa ogopa
Lab Rat
87 posts
mlong wrote:
Are you getting the “Transfer failed” error message?

Nope, no error message at all. Its wierd

August 8, 2011

mlong mlong
Mad Scientist
1517 posts

So, what exactly is happening here in your main()? Is your app supposed to play an audio sample or something before it opens the MainWindow()? (I’m assuming that the “wave” class is a QMainWindow.)

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 8, 2011

ogopa ogopa
Lab Rat
87 posts

mlong wrote:
So, what exactly is happening here in your main()? Is your app supposed to play an audio sample or something before it opens the MainWindow()? (I’m assuming that the “wave” class is a QMainWindow.)

Yes, the wave class is a QMainWindow. Yeah, I am trying to play a sine wave and in my QmainWindow, I can input frequency and amplitude. and it will play a sine wave according to those specifications.

I also tried this: I completely removed the line which was giving me a problem, as I am executing it in under my pushButton function. So the QMainWindow pops up now, but now the window terminates as soon as I click the pushButton.

August 8, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

I’m not aware of using low-level sound api, but transfer_loop is looking like it will be looping until something happens.

August 8, 2011

mlong mlong
Mad Scientist
1517 posts

It’s probably terminating because you’re deleting all of your resources before you ever call a.exec(). (See lines 86-88 in your original code posting.)

If you’ll pardon my candor, it looks like you’ve cut-and-pasted a C example from ALSA into your main() and have made a few tweaks to replace “calloc” with “new”, and are just kind of expecting it to seamlessly integrate with Qt. I think that you’re going to keep running into compilation and execution issues because you might not understand the nuances of the code that you’re trying to use, nor the way that the event loop and everything works under Qt.

Are there more fundamental issues with C++ and/or Qt that you may need to focus on further beforehand that you might still be a little uncertain of? If there are, it may help to ask about those issues first.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

Page  
1

  ‹‹ Problem using RSA algorythme in QCA      why is publishing currently not possible for project? ››

You must log in to post a reply. Not a member yet? Register here!