Results 1 to 17 of 17

Thread: Events

  1. #1
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Events

    Hi guys,

    got some question concerning events in qt. at first i used the signal slot princip like this one :
    Qt Code:
    1. connect(b_loadDB, SIGNAL(clicked()),in,SLOT(loadDB()));
    To copy to clipboard, switch view to plain text mode 
    to connect an event with a method.

    but i want it to be like actionlisteners in java, i read something about events, but i can't help myself with it. anyway that's my question, is there a way to check events and do then methods and get return values, like in java (cause i think, it's a pretty easy principle)

    i hope somebody can help me
    thx
    tobi

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    Sure, you can:

    Qt Code:
    1. int QEvent::registerEventType ( int hint = -1 )
    To copy to clipboard, switch view to plain text mode 

    and then:

    Qt Code:
    1. void QCoreApplication::postEvent ( QObject * receiver, QEvent * event )
    2. bool QCoreApplication::sendEvent ( QObject * receiver, QEvent * event )
    To copy to clipboard, switch view to plain text mode 

    Making sure to override the event() method for each widget to process those events.

    A much easier way would be to use signals and slots though, as you would have to subclass QEvent for each type of data you want to send.

  3. The following user says thank you to squidge for this useful post:

    T0bi4s (15th November 2009)

  4. #3
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    i'm not sure if i understood what i wanted to tell me.

    Qt Code:
    1. int QEvent::registerEventType ( int hint = -1 )
    To copy to clipboard, switch view to plain text mode 

    why integer at the beging? or better, what does this line do? i thought of something like create an event like "clicked" or something like that.

    and what's the
    Qt Code:
    1. int hint = -1
    To copy to clipboard, switch view to plain text mode 
    ? i don't get this one


    what's the differenc between postEvent and sendEvent? and from where is the returnvalue or what does the returnvalue mean?:
    Qt Code:
    1. void QCoreApplication::postEvent ( QObject * receiver, QEvent * event )
    To copy to clipboard, switch view to plain text mode 

    i know some newbie question
    but thx for your quick answer, hope you're still there to help me again
    tobi
    Last edited by T0bi4s; 15th November 2009 at 19:49.

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    All event types have a number, hence the returning of an integer. User events are numbers >= 1000. Its upto you to decide what each number means. The hint is if you want a specific number. -1 means you want the next available number.

    postEvent returns immediately (before the event has been processed by the receiver)
    sendEvent returns when the event has been processed, and returns the boolean return value

    The event handler returns true if the event was recognised, false otherwise, and you get this value from sendEvent. postEvent can't return this as the event will not have been processed by the time the function returns.

  6. The following user says thank you to squidge for this useful post:

    T0bi4s (15th November 2009)

  7. #5
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    ok i got so far

    but still a question if i can specify the events and mark each event with a number, how do i specify the event?

    Qt Code:
    1. int QEvent::registerEventType ( int hint = -1 )
    To copy to clipboard, switch view to plain text mode 

    should create an event with the first avilable number (cause of the hint = -1) and how i know which it is? like clicked i mean

    or do i have to use it like
    Qt Code:
    1. int eventnumber_clicked;
    2.  
    3. QEvent e_clicked;
    4. eventnumber_clicked = e_clicked::registerEventType (int hint =-1);
    To copy to clipboard, switch view to plain text mode 

    it was just a propose, i don't know. anway how do i link the event with an object (like a button) and
    Qt Code:
    1. void QCoreApplication::postEvent ( QObject * receiver, QEvent * event )
    To copy to clipboard, switch view to plain text mode 
    what is meant here with receiver? is it a e.g. a button und meant to link event with widget or what?
    and what will be done when event occurs? or do i just know that it's done and check it with an "if"?

    thx 4 help
    tobi

  8. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    for each event, you have to subclass QEvent, eg:

    Qt Code:
    1. class myEvent : public QEvent
    2. {
    3. // data structure for event
    4. };
    5.  
    6. myEvent *event;
    7.  
    8. myevent_type = registerEventType();
    9. event = new myEvent(myevent_type);
    To copy to clipboard, switch view to plain text mode 

    and then override event method:

    Qt Code:
    1. bool event (QEvent *event)
    2. {
    3. if (event->type == myevent_type)
    4. {
    5. /* foo */
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    'receiver' is who should receive the event. So far example, to send the event to yourself, you would put 'this', and to send to a button, you would use maybe 'mybutton', which is perhaps of type QWidget.

  9. #7
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    man you're great!!!

    just one more thing, what you mean with datastructure of the event? is that something like clicked() or what is meant with that?

    thx tobi

  10. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    It means whatever data you want to send with the event. You might want to send a flag (eg. Pressed or Released) or perhaps some text. Your event handler can then access this information and do what it likes with it.

    So instead of having, for example, MousePressedEvent and MouseReleasedEvent, you would have MouseEvent, and in there you would have a variable called "Pressed".

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Events

    If you want to get a return value then most likely you know the object you expect to return such value. In that case you can call its method directly without using events or signals. Using events in your situation doesn't make that much sense (as you need to know where to send the event to), it's easier to obtain the same effect using signals anyway...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    Quote Originally Posted by T0bi4s View Post
    is there a way to check events and do then methods and get return values
    I think a combination of QObject::sender() and QObject::qobject_cast would allow you to do what you want, though I haven’t had occasion to try it yet. For example:
    Qt Code:
    1. FancyButton* fb = qobject_cast<FancyButton*>(sender());
    2. if (fb) {
    3. code here can use fb->any_accessible_member_or_method
    4. // (non-public members/methods only if FancyButton declares this class a friend)
    5. // If called (not signaled), or if signaled by something that is not a FancyButton
    6. // or a sub-class of FancyButton, fb will be 0 and the if block won't be entered.
    7. }
    To copy to clipboard, switch view to plain text mode 

    If you control the code for the signaling objects as well, a simpler and safer way would be to change the signal and slot to take the this pointer of the signaling object as an argument.
    Last edited by Coises; 16th November 2009 at 18:44.

  13. #11
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    hi again,

    long time ago i last answered. and again this way of sender and cast. how does it work? i tried to open a qdialog with a button click but it didn't work lik i did:

    Qt Code:
    1. QDialog dia_job;
    2. QDialog dia_package;
    3.  
    4. Ui::d_job uij;
    5. uij.setupUi(&dia_job);
    6.  
    7. Ui::d_package uip;
    8. uip.setupUi(&dia_package);
    9.  
    10.  
    11. uij.tb_set_package = qobject_cast<QToolButton*>(dia_package.show());
    12.  
    13. if(uij.tb_set_package){
    14. qDebug("clicked");
    15. dia_package.show();
    16. }
    To copy to clipboard, switch view to plain text mode 
    qcreator says incalid use of void expression, i can guess why, but what should it look like that it works?

    or found another thing

    Qt Code:
    1. void PaintArea::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) {
    4. if (brushInterface) {
    5. QPainter painter(&theImage);
    6. setupPainter(painter);
    7. const QRect rect = brushInterface->mouseMove(brush, painter, lastPos,
    8. event->pos());
    9. update(rect);
    10. }
    11.  
    12. lastPos = event->pos();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    this is just an example i found, but how can i link an event with an button? i tried
    Qt Code:
    1. void mouseclick(QMouseEvent *e){
    2. if(e){
    3. }
    4. }
    To copy to clipboard, switch view to plain text mode 
    in the headerfile of my ui file but actually i can't use the dot-extension for the event e, why? anyone got an idea?

    thx
    tobi
    Last edited by T0bi4s; 25th November 2009 at 15:51.

  14. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    Quote Originally Posted by T0bi4s View Post
    Qt Code:
    1. void mouseclick(QMouseEvent *e){
    2. if(e){
    3. }
    4. }
    To copy to clipboard, switch view to plain text mode 
    in the headerfile of my ui file but actually i can't use the dot-extension for the event e, why? anyone got an idea?
    dot-extension? If you mean you can't use 'e.' then of course you can't. 'e' is a pointer, not a reference.

    I've no idea what else your trying to do with the rest of the code.

    I said at the beginning of the thread, you should be using signals and slots. Thats how you handle events from objects.

  15. #13
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    i see your point, anyway got the problem that i can't use the connect function in the main part of my project, it says that connect is not declared.

    can't i use it in the main.cpp? --> if so, do i have to create a e.g. main working class?

    edit:
    by the way, what's the diffrence between an signal and a virtual signal?
    Last edited by T0bi4s; 25th November 2009 at 17:41.

  16. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    You can use connect() from any class that derives from QObject, so make sure your class does.

    I've no idea what a virtual signal is. Virtual slot, yes, but not signal.

  17. The following user says thank you to squidge for this useful post:

    T0bi4s (25th November 2009)

  18. #15
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    ok my bad i meant slots, just wrote signals

    thx for the connect answer()

  19. #16
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    Ok, a virtual slot is one where when you override the name in your own class that inherits the class with a virtual slot, your slot will be called rather than the base class even if the pointer type references the base rather than your class type.

    Wow, that came out like mud. Lets try again: If you have class 'a' that has virtual slot 'vslot' and you subclass that class and call it 'b'. You then create a variable "a *myclassInstance" and call vslot via myclassInstance->vslot(). Even though you don't reference class "b", class b's 'vslot' will be called rather than base call a's. If it wasn't virtual, it would call class a's vslot instead, unless myclassInstance definition was "b *myclassInstance".

    Hopefully thats more clear.

  20. #17
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Events

    thx, was clear enough

Similar Threads

  1. synchronizing events
    By zaphod.b in forum Qt Programming
    Replies: 0
    Last Post: 14th July 2009, 17:29
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 16:03
  3. Replies: 9
    Last Post: 22nd June 2008, 23:26
  4. How to suppress user defined events in processEvents()
    By Artschi in forum Qt Programming
    Replies: 5
    Last Post: 5th July 2007, 11:17
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 20:25

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.