PDA

View Full Version : QMainWIndow not popping up when I run



duma
8th August 2011, 16:51
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();


}

mvuori
8th August 2011, 17:13
I can't see any windows getting created... unless your own type "wave" is such, but there is no way for anyone to know. If it is a subclassed QMainWindow, most likely its constructor has some kind of a problem.

duma
8th August 2011, 18:04
I can't see any windows getting created... unless your own type "wave" is such, but there is no way for anyone to know. If it is a subclassed QMainWindow, most likely its constructor has some kind of a problem.

There is a window being created at the bottom of my int main():

QApplication a(argc, argv);
wave w;
w.setWindowTitle("ESG Wave Creator");

w.show();

return a.exec();


Also, sorry for the lack of information. I used the designer to create the window widget wave.ui. The code above was the main in my main.cpp. wave.cpp and wave.h are below.

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 "wave.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 ampl;
static double amplitude;
static bool isTrue = true;


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


/* parent->
generate_sine(const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
int count, double *_phase );*/



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



void wave::on_pushButton_clicked()
{
freq = ui->frequency->text().toDouble();
ampl = ui->amplitude->text().toDouble();
snd_pcm_t *handle;
signed short *samples;
snd_pcm_channel_area_t *areas;
double phase = 0;
signed short *ptr;
int err, cptr;

while (1) {


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\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
break; /* skip one period */
}
ptr += err * channels;
cptr -= err;
}
}
}



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

}

wave.h:

#ifndef WAVE_H
#define WAVE_H
#include "ui_wave.h"
#include <alsa/asoundlib.h>
#include <QMainWindow>
#include <QObject>
#include "generate_sine.h"
#include "xrun_recovery.h"

namespace Ui {
class wave;
}

class wave : public QMainWindow
{
Q_OBJECT

public:
explicit wave(QWidget *parent = 0);
~wave();


private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();
private:
Ui::wave *ui;




};



#endif // WAVE_H

duma
8th August 2011, 20:57
I can't see any windows getting created... unless your own type "wave" is such, but there is no way for anyone to know. If it is a subclassed QMainWindow, most likely its constructor has some kind of a problem.
I created a program in Qt Creator called "wave" which is a QMainWindow.
As for my problem, 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 under in 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.

jacks916
9th August 2011, 21:17
I may be way off base, but aren't structs supposed to be declared outside of functions? in a global space?