Results 1 to 5 of 5

Thread: About signals and events

  1. #1
    Join Date
    Jun 2012
    Location
    Spain
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default About signals and events

    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:

    Qt Code:
    1. #ifndef VFIT_W_H
    2. #define VFIT_W_H
    3.  
    4. #include <QMainWindow>
    5. #include <QLabel>
    6. #include <QActionGroup>
    7. #include <QEvent>
    8.  
    9. namespace Ui {
    10. class VFITW;
    11. }
    12.  
    13. class VFITW : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit VFITW(QWidget *parent = 0);
    19. ~VFITW();
    20.  
    21. private:
    22. Ui::VFITW *ui;
    23. ...
    24. [COLOR="#FF0000"]bool event(QEvent *e);[/COLOR]
    25. ...
    26.  
    27. public slots:
    28. ...
    29.  
    30. private slots:
    31.  
    32. [COLOR="#0000FF"]void FormActivate(); // This is the slot which will manage the WindowActivate event[/COLOR]
    33. ...
    34. };
    35.  
    36. #endif // VFIT_W_H
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About signals and events

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

  3. #3
    Join Date
    Jun 2012
    Location
    Spain
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: About signals and events

    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!

  4. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About signals and events

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: About signals and events

    all you have to do is overload your showEvent(), or capture it in an event filter.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. how to connect events with signals in QGraphicsScene?
    By nataly in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 15:20
  2. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  3. events interacting with signals/slots
    By gunhelstr in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 12:30
  4. signals and events queued...
    By soul_rebel in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2006, 13:32
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.