PDA

View Full Version : About signals and events



jcbaraza
30th June 2012, 12:47
Hi everyone,

I'm migrating an ancient application from Borland Builder 3 to Qt4. In Builder, there exists an extensive management of events in VCL classes. When designing VCL classes, besides the constructor and destroy functions, lots of event managing functions associated to every object in the form can be implemented. Every kind of objects (labels, buttons, lists, the keyboard, the mouse, and even the main form) have their own set of associated events: OnEnter, On Change, OnClick, OnKeyDown, etc.

In the particular case of the main form, there exists an event called "FormActivate" (managed by an event manager called "OnFormActivate"), which is launched after the form has been created and made visible, and just before the user can take control (i.e., to push keys or click the mouse). In this event manager, it is possible to make some tasks like configuration (loading configuration from files, o auto-configure the application), showing additional windows (info or dialogs), fulfilling the fields of the main form, and so on.

Well, for the last days I've been trying to find in Qt a SIGNAL in QMainWindow class equivalent to Builder's "FormActivateE event, in order to CONNECT my former "OnFormActivate" Builder function (adapted of course) to to such signal, and thus do in the same way as in Builder. However, I haven't found any equivalent nor similar signal. What I've found is a QEvent called "WindowActivate". So, I've thought that I could to create my own signal when "WindowActivate" is shot.

After reading Qt handbooks and Molkentin's "The Book of Qt 4 - The art of building Qt applications", I've "learnt" that I should use general event() event handler, and there capture the corresponding event and generate a custom "FormActivate" signal, to be connected to "OnFormActivate" method/function. However, when I've written in the header of my main window class the declaration of the event handler I've got a compilation error:


#ifndef VFIT_W_H
#define VFIT_W_H

#include <QMainWindow>
#include <QLabel>
#include <QActionGroup>
#include <QEvent>

namespace Ui {
class VFITW;
}

class VFITW : public QMainWindow
{
Q_OBJECT

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

private:
Ui::VFITW *ui;
...
bool event(QEvent *e);
...

public slots:
...

private slots:

void FormActivate(); // This is the slot which will manage the WindowActivate event
...
};

#endif // VFIT_W_H

The error is error: collect2: ld returned 1 exit status. What am I doing wrong? Am I forgetting anything? Is there another option (easier than managing QEvents ;-P) to emulate my "FormActivate" Builder event?

Thanks a lot in advance!

sonulohani
30th June 2012, 13:45
May be you've not declared that event function in your header file. You've to specify it there. and also, you've to implement the definition of that event in your source file. I can't able to figure the error with this code. Place your source file here, then only we could figure out what is the problem...

jcbaraza
30th June 2012, 17:39
Hi,

First of all, thanks for your answer. When copying the source code in the post I've realized what was the cause of the compilation error: I didn't uncomment the code of the event manager event().

The problem now is to know when the main window becomes active after creation. As I mentionned in my previous post, there exist in Builder a set of predefined event handler for every VCL object. In forms, there is one particularly intresting to me, called "OnFormActivate", which runs when the event "FormActivate" shots. This happens after the form (window) is created and made visible, but still is unavailable for the user. Within this event it is possible to perform configuration tasks, and show status messages and launch dialogs. Most of these configuration tasks actually could be performed by the constructor, but the window would not be visible yet (at least, I think so...). That's why I want to know the moment of the form activation. Moreover, as my original Builder program uses several custom forms that I fulfill online, I repeat this process many times.

I include the source code of the main window just in case.

That's the header of the main window:


#ifndef VFIT_W_H
#define VFIT_W_H

#include <QMainWindow>
#include <QLabel>
#include <QActionGroup>
#include <QEvent>

#include "GStrings.h"

namespace Ui {
class VFITW;
}

class VFITW : public QMainWindow
{
Q_OBJECT

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

private:
Ui::VFITW *ui;
...

void event(QEvent *e);

private slots:
void OnFormActivate();
...
};

#endif // VFIT_W_H

And that's the source code of the main window:


#include <QtGui>
#include <QWidget>
#include <QAction>

...

#include "VFIT_W.h"
#include "ui_VFIT_W.h"

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

// This will be the future connection of OnFormActivate private slot to the custom signal FormActivate (still to be defined)
// connect(this, SIGNAL(FormActivate()), this, SLOT(OnFormActivate()));

...
}

VFITW::~VFITW()
{
delete ui;
}
...

void VFITW::event(QEvent *e)
{
// It's still empty...
}
...

void VFITW::OnFormActivate()
{
if (Initialized)
return ;

Initialized = true;
}
...

Any track please? I can't find any signal nor event from the QMainWindow class similar to the Builder's "FormActivate" event. I've read about QEvent::WindowActivate, but I'm not sure this will be good for mi idea.

Thanks again!

sonulohani
4th July 2012, 11:18
why you've override the method event(QEvent). Remove that. Read the documentation--->Reimplemented from QObject::event().

This is the main event handler; it handles event event. You can re implement this function in a subclass, but we recommend using one of the specialized event handlers instead. So, use specialized event handlers.... And where you're emitting the signal FormActivate(). If it is still undefined then dont use it ... comment that..... ALso comment the event method.

high_flyer
4th July 2012, 11:46
all you have to do is overload your showEvent() (http://qt-project.org/doc/qt-4.8/qwidget.html#showEvent), or capture it in an event filter.