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&#40;&#41;;
    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]

    Thanks for your help and the quick replies. :)



  • [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.


  • Moderators

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

    Edit: Nevermind.


  • Moderators

    Are you getting the "Transfer failed" error message?



  • [quote author="mlong" date="1312833791"]Are you getting the "Transfer failed" error message?
    [/quote]

    Nope, no error message at all. Its wierd


  • Moderators

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


  • Moderators

    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.



  • [quote author="mlong" date="1312839492"]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]

    Hi mlong. I agree. Although, before I started integrating my code with Qt, I made sure that my converted code compiled with a c++ compiler in terminal i.e. g++ and ran fine to produce a sine wave. I am using Qt Creator to build a gui that lets me input frequency and amplitude and produces a sine wave with those specifications. I have now reached a stage in Qt where I have dealt with all the errors. I have no more errors, my QMainWindow pops up. Only now, the problem thhat I am having is that my pushButton function doesn't seem to be generating a sine wave.
    I thank you for your help so far and I hope you will bear with me for just a little while longer. I feel like I am almost there.



  • [quote author="Gerolf" date="1312900157"]# 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]

    Hi, yes line 25 has executed. I just debugged it and there seems to be no issues.
    isTrue is set to false under my second pushButton function (pushButton2) which is to terminate the loop. I have actually solved the issue, it doesn't terminate anymore. There are no more errors, but now, the problem is when I hit the pushButton, it doesn't generate any sine wave:( Definitely, here is my wave.cpp:
    @#include <QtGui>
    #include <QApplication>
    #include <qlineedit.h>
    #include "ui_wave.h"
    #include <QString>
    #include <QObject>
    #include <QDebug>
    #include <stdio.h>
    #include <cstdio>
    #include <stdlib.h>
    #include <cstdlib>
    #include <string.h>
    #include <cstring>
    #include <sched.h>
    #include <errno.h>
    #include <getopt.h>
    #include <alsa/asoundlib.h>
    #include <sys/time.h>
    #include <sstream>
    #include <string>
    #include <math.h>
    #include <cmath>
    #include <iostream>
    #include "generate_sine.h"
    #include "xrun_recovery.h"
    #include "wave.h"
    #include "write_loop.h"
    using namespace std;

    static const char *device = "plughw:0,0"; // playback device
    static snd_pcm_format_t format = SND_PCM_FORMAT_S16; // sample format- change to 24-bit
    static unsigned int rate = 96000; // stream rate
    static unsigned int channels = 128; // count of channels
    static unsigned int buffer_time = 500000; // ring buffer length in us
    static unsigned int period_time = 100000; // period time in us
    static double freq; // sinusoidal wave frequency in Hz
    static int verbose = 0; // verbose flag
    static int resample = 1; // enable alsa-lib resampling
    static int period_event = 0; // produce poll event after each period
    static snd_pcm_sframes_t buffer_size;
    static snd_pcm_sframes_t period_size;
    static snd_output_t *output = NULL;
    static double amplitude;
    static bool isTrue;

    wave::wave(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::wave)
    {
    ui->setupUi(this);

    }
    wave::~wave()
    {
    delete ui;

    }

    void wave::on_pushButton_clicked()
    {
    snd_pcm_t *handle;
    snd_pcm_channel_area_t *areas;
    signed short *samples;

    write_loop(handle, samples, areas);
    

    }

    void wave::on_pushButton_2_clicked()
    {
    isTrue = false;

    }@

    write_loop.cpp:
    @
    static char device = "plughw:0,0"; / playback device /
    static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /
    sample format /
    static unsigned int rate = 96000; /
    stream rate /
    static unsigned int channels = 128; /
    count of channels /
    static unsigned int buffer_time = 500000; /
    ring buffer length in us /
    static unsigned int period_time = 100000; /
    period time in us /
    static double freq = 440; /
    sinusoidal wave frequency in Hz /
    static int verbose = 0; /
    verbose flag /
    static int resample = 1; /
    enable alsa-lib resampling /
    static int period_event = 0; /
    produce poll event after each period */
    static snd_pcm_sframes_t buffer_size;
    static snd_pcm_sframes_t period_size;
    static snd_output_t *output = NULL;
    static float amplitude;
    static bool isTrue;

    int write_loop(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;
                 }
         }
    

    }@



  • generate_sine.cpp:
    @
    //static const char *device = "plughw:0,0"; // playback device
    static snd_pcm_format_t format = SND_PCM_FORMAT_S16; // sample format- change to 24-bit
    static unsigned int rate = 96000; // stream rate
    static unsigned int channels = 128; // count of channels
    static unsigned int buffer_time = 500000; // ring buffer length in us
    static unsigned int period_time = 100000; // period time in us
    static double freq; // sinusoidal wave frequency in Hz
    static int verbose = 0; // verbose flag
    static int resample = 1; // enable alsa-lib resampling
    static int period_event = 0; // produce poll event after each period
    static snd_pcm_sframes_t buffer_size;
    static snd_pcm_sframes_t period_size;
    static snd_output_t *output = NULL;
    static double amplitude;

    void generate_sine(const snd_pcm_channel_area_t *areas,
    snd_pcm_uframes_t offset,
    int count, double *_phase)
    {
    static double max_phase = 2. * M_PI;
    double phase = _phase;
    double step = max_phase
    freq/(double)rate;
    unsigned char samples[channels];
    int steps[channels];
    unsigned int chn;
    int format_bits = snd_pcm_format_width(format);
    unsigned int maxval = (1 << (format_bits - 1)) - 1;
    int bps = format_bits / 8; /
    bytes per sample */
    int phys_bps = snd_pcm_format_physical_width(format) / 8;
    int big_endian = snd_pcm_format_big_endian(format) == 1;
    int to_unsigned = snd_pcm_format_unsigned(format) == 1;
    int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
    format == SND_PCM_FORMAT_FLOAT_BE);
    float amplitude_scale = amplitude/8.56;

         /* verify and prepare the contents of areas */
         for (chn = 0; chn < channels; chn++) {
                if ((areas[chn].first % 8) != 0) {
                        printf("areas[%i].first == %i, aborting...", chn , areas[chn].first);
                         exit(EXIT_FAILURE);
                 }
                 samples[chn] = (((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
                 if ((areas[chn].step % 16) != 0) {
                         printf("areas[%i].step == %i, aborting...  ", chn, areas[chn].step);
                         exit(EXIT_FAILURE);
                 }
                 steps[chn] = areas[chn].step / 8;
                 samples[chn] += offset * steps[chn];
         }
         // fill the channel areas
         while (count-- > 0) {
                 union {
                         float f;
                         int i;
                 } fval;
                 int res, i;
                 if (is_float) {
                         fval.f = amplitude_scale * sin(phase) * maxval;
                         res = fval.i;
                 } else
                         res = amplitude_scale * sin(phase) * maxval;
                 if (to_unsigned)
                         res ^= 1U << (format_bits - 1);
                 for (chn = 0; chn < channels; chn++) {
                          //Generate data in native endian format
                         if (big_endian) {
                                 for (i = 0; i < bps; i++)
                                         *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
                         } else {
                                 for (i = 0; i < bps; i++)
                                         *(samples[chn] + i) = (res >>  i * 8) & 0xff;
                         }
                         samples[chn] += steps[chn];
                 }
                 phase += step;
                 if (phase >= max_phase)
                         phase -= max_phase;
         }
         *_phase = phase;
    

    }
    @



  • [quote author="ogopa" date="1312902815"][quote author="Gerolf" date="1312900157"]# 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]

    Hi, yes line 25 has executed. I just debugged it and there seems to be no issues.
    isTrue is set to false under my second pushButton function (pushButton2) which is to terminate the loop. I have actually solved the issue, it doesn't terminate anymore. There are no more errors, but now, the problem is when I hit the pushButton, it doesn't generate any sine wave:( Definitely, here is my wave.cpp:
    [/quote]

    Hi,

    As the loop executed on a button press, which means runs inside the main event loop, tzhe second button press will never be executed, as the main thread is blocked.

    Regarding the other thing, I have no idea, as I have no experience in sound card programming...



  • [quote author="Gerolf" date="1312906849"]
    [quote author="ogopa" date="1312902815"][quote author="Gerolf" date="1312900157"]# 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]

    Hi, yes line 25 has executed. I just debugged it and there seems to be no issues.
    isTrue is set to false under my second pushButton function (pushButton2) which is to terminate the loop. I have actually solved the issue, it doesn't terminate anymore. There are no more errors, but now, the problem is when I hit the pushButton, it doesn't generate any sine wave:( Definitely, here is my wave.cpp:
    [/quote]

    Hi,

    As the loop executed on a button press, which means runs inside the main event loop, tzhe second button press will never be executed, as the main thread is blocked.

    Regarding the other thing, I have no idea, as I have no experience in sound card programming...

    [/quote]

    ok. Thanks for your help. Much appreciated.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.