PDA

View Full Version : Event handling problem



Mel Seyle
22nd August 2007, 23:40
I'm trying to get an object to handle a QCloseEvent. The object, DDS, is instantiated in Main_Widget, and pops up as a smaller window. In this case all the close event in the new DDS object is supposed to do is execute a printf that shows that closeEvent was indeed called. However, when the DDS object is closed, it appears that closeEvent is never called. Note that a similar closeEvent in Main_Widget will execute its printf, and the desired message appears.

I'm missing something fundamental here, but haven't been able to track it down.

Thanks.

Mel Seyle

code follows:



#include <qapplication.h>
#include <qfontdatabase.h>
#include "main_widget.h"
#include "app.xpm"

//int DDS::loLcdValue=0;

int main (int argc, char **argv)
{
QApplication app(argc, argv);
Main_Widget *w = new Main_Widget;;
app.setWindowIcon( QIcon(app_xpm) );
app.setStyleSheet("QFrame {border : 1px solid rgb(255,200,55)}");
w->show();
return app.exec();
}

#include "main_widget.h"

Main_Widget::Main_Widget(QWidget *parent)
: QWidget(parent)
{
setDds( 5 );
}

void Main_Widget::closeEvent( QCloseEvent * )
{
printf("Main_Widget::closeEvent\n");
finish();
}

void Main_Widget::finish()
{
// saveSettings();
exit( 0 );
}

void Main_Widget::setDds( int )
{
SDR_ShellDDS = new DDS;
}


#ifndef SDXCVR_MAINWIDGET_H
#define SDXCVR_MAINWIDGET_H

#include <qwidget.h>
#include <qapplication.h>

#include "DDS.h"

class Main_Widget : public QWidget
{


Q_OBJECT

private:
DDS *SDR_ShellDDS;

public:
Main_Widget(QWidget *parent = 0);

public slots:
void finish();
void setDds( int );

protected:
void closeEvent( QCloseEvent * );

};
#endif


#include "DDS.h"

DDS::DDS( QWidget *parent)
: QWidget( parent)
{

ddsFrame = new QFrame();
ddsFrame->setGeometry( 400, 200, 450, 300 );
ddsFrame->setMinimumWidth( 450 );
ddsFrame->setMinimumHeight( 300 );
ddsFrame->setWindowTitle("SDR-Shell : Frequency Control ");

ddsFrame->show();

}

void DDS::closeEvent( QCloseEvent *e )
{
printf("closeEvent( QCloseEvent * )\n");
e->accept();
finish();
}

void DDS::finish()
{
printf("finish()\n");
// exit( 0 );
}


#ifndef DDS_H
#define DDS_H

#include <QWidget>
#include <QFrame>
#include <QCloseEvent>
#include <QEvent>
#include <QAction>
#include <QObject>


class DDS : public QWidget
{
Q_OBJECT


private:

QFrame *ddsFrame;

public:

DDS(QWidget *parent = 0);

private slots:

void finish();

protected:
void closeEvent( QCloseEvent * );

};
#endif

jacek
22nd August 2007, 23:54
The problem is that you never show that DDS widget, only ddsFrame, so DDS doesn't receive any close events.

Mel Seyle
23rd August 2007, 05:15
Jacek,

Thanks for the reply. Yep, that was indeed the problem. I knew it had to be something fundamental. I'm still learning about C++ and Qt, and the forum has been a big help.

Thanks again for your response.


Mel Seyle