QMainWIndow not popping up when I run
-
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:@int main(int argc, char *argv[])
{
struct option long_option[] =
{
{"help", 0, NULL, 'h'},
{"device", 1, NULL, 'D'},
{"rate", 1, NULL, 'r'},
{"channels", 1, NULL, 'c'},
{"frequency", 1, NULL, 'f'},
{"buffer", 1, NULL, 'b'},
{"period", 1, NULL, 'p'},
{"method", 1, NULL, 'm'},
{"format", 1, NULL, 'o'},
{"verbose", 1, NULL, 'v'},
{"noresample", 1, NULL, 'n'},
{"pevent", 1, NULL, 'e'},
{NULL, 0, NULL, 0},
};int err; snd_pcm_hw_params_t *hwparams; snd_pcm_sw_params_t *swparams; int method = 0; snd_pcm_t *handle; snd_pcm_channel_area_t *areas; signed short *samples; unsigned int chn; snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); err = snd_output_stdio_attach(&output, stdout, 0); if (err < 0) { printf("Output failed: %s\n", snd_strerror(err)); return 0; }
/* cout<<"Playback device is %s "<<","<< device);
cout<<"Stream parameters are %iHz, %s, %i channels "<<","<< rate<<","<< snd_pcm_format_name(format), channels;
cout<<"Sine wave rate is %.4fHz "<<","<< freq;
cout<<"Using transfer method: %s "<<","<< transfer_methods[method].name;
*/
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
return 0;
}if ((err = set_hwparams(handle, hwparams, transfer_methods[method].access)) < 0) { printf("Setting of hwparams failed: %s\n", snd_strerror(err)); exit(EXIT_FAILURE); } if ((err = set_swparams(handle, swparams)) < 0) { printf("Setting of swparams failed: %s\n", snd_strerror(err)); exit(EXIT_FAILURE); } if (verbose > 0) snd_pcm_dump(handle, output); samples = new signed short[(period_size * channels * snd_pcm_format_physical_width(format))]; if (samples == NULL) { printf("Not enough memory\n"); exit(EXIT_FAILURE); } areas = new snd_pcm_channel_area_t [channels]; if (areas == NULL) { printf("Not enough memory\n"); exit(EXIT_FAILURE); } for (chn = 0; chn < channels; chn++) { areas[chn].addr = samples; areas[chn].first = chn * snd_pcm_format_physical_width(format); areas[chn].step = channels * snd_pcm_format_physical_width(format); } err = transfer_methods[method].transfer_loop(handle, samples, areas); if (err < 0) printf("Transfer failed: %s\n", snd_strerror(err)); delete [] areas; delete [] samples; snd_pcm_close(handle);
QApplication a(argc, argv);
wave w;
w.setWindowTitle("ESG Wave Creator");w.show();
return a.exec(); return 0;
}@
-
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. -
[quote author="Denis Kormalev" date="1312820192"]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.[/quote]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:
@Starting /home/test/Documents/Wave-build-desktop/Wave...
Playback open error: Device or resource busy
/home/test/Documents/Wave-build-desktop/Wave exited with code 0@ -
try to debug it or place qDebug calls in your code to find out where is the problem.
-
[quote author="Denis Kormalev" date="1312823936"]try to debug it or place qDebug calls in your code to find out where is the problem.[/quote]
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:
@if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
return 0;
}@ -
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.
-
-
[quote author="Denis Kormalev" date="1312824246"]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.[/quote]
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 -
I tried debugging my program: I placed the line @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:
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
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. -
[quote author="mlong" date="1312836921"]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.)
[/quote]
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.
-
I'm not aware of using low-level sound api, but transfer_loop is looking like it will be looping until something happens.
-
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.
-
[quote author="ogopa" date="1312833563"]
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
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.[/quote]
For me it sounds like this is an (perhaps endless) loop, which would mean you never get an error or a window.
-
[quote author="Gerolf" date="1312870616"][quote author="ogopa" date="1312833563"]
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
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.[/quote]
For me it sounds like this is an (perhaps endless) loop, which would mean you never get an error or a window.
[/quote]
Yes. You are right. Thank you. I removed the line from my int main() as I am calling it under my pushButton function to display the sine wave. My window pops up now but when I click the push button, my program terminates. i'm not sure why. this is my push button function:
@void wave::on_pushButton_clicked()
{snd_pcm_t *handle; signed short *samples; snd_pcm_channel_area_t *areas; double phase = 0; signed short *ptr; int err, cptr; while (isTrue) { generate_sine(areas, 0, period_size, &phase); ptr = samples; cptr = period_size; while (cptr > 0) { err = snd_pcm_writei(handle, ptr, cptr); if (err == -EAGAIN) continue; if (err < 0) { if (xrun_recovery(handle, err) < 0) { printf("Write error: %s ", snd_strerror(err)); exit(EXIT_FAILURE); } // break;// skip one period } ptr += err * channels; cptr -= err; } } }
@
-
Did you check, whether line 25 is executed?
Did you try to debug it and step through to check what happens?
You have an endless loop here (where is isTrue set to false?)
If you provide a full, runnable and debuggable example, perhaps others could also try with the debugger...
-
[quote author="ogopa" date="1312895011"]
[quote author="Gerolf" date="1312870616"][quote author="ogopa" date="1312833563"]
@err = transfer_methods[method].transfer_loop(handle, samples, areas);
if (err < 0)
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.[/quote]
For me it sounds like this is an (perhaps endless) loop, which would mean you never get an error or a window.
[/quote]
Yes. You are right. Thank you. I removed the line from my int main() as I am calling it under my pushButton function to display the sine wave. My window pops up now but when I click the push button, my program terminates. i'm not sure why. this is my push button function:
@void wave::on_pushButton_clicked()
{snd_pcm_t *handle; signed short *samples; snd_pcm_channel_area_t *areas; double phase = 0; signed short *ptr; int err, cptr; while (isTrue) { generate_sine(areas, 0, period_size, &phase); ptr = samples; cptr = period_size; while (cptr > 0) { err = snd_pcm_writei(handle, ptr, cptr); if (err == -EAGAIN) continue; if (err < 0) { if (xrun_recovery(handle, err) < 0) { printf("Write error: %s ", snd_strerror(err)); exit(EXIT_FAILURE); } // break;// skip one period } ptr += err * channels; cptr -= err; } } }
@
[/quote]
actually just solved this problem, there was code in my program that was terminating it. So everything compiles without errors and runs perfectly. Just when I input values of frequency and amplitude now, it doesn't output any sine wave :( . Why does my push button function not generate a sine wave. It does generate a sine wave when I run it in terminal. Any help will be appreciated.