Results 1 to 5 of 5

Thread: Newbie class design question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Newbie class design question

    Read about event filters. Instead of implementing the mouse events in the widget, implement the event handling in a separate QObject and install the event filter on all the widgets.

    Qt Code:
    1. void QObject::installEventFilter(QObject * filterObj)
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  2. The following user says thank you to Santosh Reddy for this useful post:

    qk (21st February 2017)

  3. #2
    Join Date
    Feb 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Newbie class design question

    Quote Originally Posted by Santosh Reddy View Post
    Read about event filters.
    Oooh, that exists? Nice! Thank you so much! I think this might solve my problem.

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie class design question

    Another option might be to define a base class derived from QWidget and DisplayElement; this base class implements your mouse event handlers. ScreenPushButton and your other GUI classes singly-inherit from this one. Be sure to include the Q_OBJECT macro in the definitions of these derived classes.

    If you go the event filter route, be sure to read carefully the parts about how to handle events you -aren't- interested in. In the alternative approach I suggested, you only implement event handlers for the events you are interested in; with an event filter, you get them all. On the other hand, you can install multiple event filters on the same object, so you could potentially handle the same event in different ways.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Newbie class design question

    Another option is delegation.

    You create a class that handles the common event processing and each of your classes holds an instance of that and delegates the events it received to that instance.
    Qt Code:
    1. class DisplayElement
    2. {
    3. protected:
    4. MouseEventHandler m_eventHandler;
    5. };
    To copy to clipboard, switch view to plain text mode 

    I would presonally keep the serialization methods out of the widget classes.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 6th May 2013, 07:35
  2. Newbie question about QToolBar
    By AlbertoN in forum Newbie
    Replies: 1
    Last Post: 28th May 2012, 06:38
  3. Newbie Question with Qextserialport.tar.gz
    By nomad in forum Qt-based Software
    Replies: 2
    Last Post: 13th July 2009, 13:32
  4. Replies: 1
    Last Post: 30th March 2009, 16:07
  5. QAbstractItemModel newbie question
    By okellogg in forum Qt Programming
    Replies: 14
    Last Post: 18th February 2008, 12:30

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
  •  
Qt is a trademark of The Qt Company.