Results 1 to 5 of 5

Thread: Newbie class design question

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

    Default Newbie class design question

    Hi,

    I'm currently trying to design a program that allows me to add and remove QWidgets(e.g. QPushButtons and QLineEdits) to a QMainWindow at runtime. So far this works pretty well, but now i recognize that my class design isn't that great and results in redundant code. I was hoping that some of you guys might have a better idea.

    My QWidgets that I will add to the QMainWindow(more specifically a custom QScreenLayout) all need some special functions for serialization and special mouse events. This is what I've got so far:

    Qt Code:
    1. class ScreenPushButton : public QPushButton, public DisplayElement
    2. {
    3. Q_OBJECT
    4. public:
    5. ScreenPushButton(QWidget *parent = 0);
    6. ~ScreenPushButton();
    7.  
    8. virtual void serialize(QDataStream &outstream) override;
    9. virtual void deserialize(QDataStream &instream) override;
    10.  
    11. private:
    12. QPoint offset_;
    13. virtual void mousePressEvent(QMouseEvent *event) override;
    14. virtual void mouseMoveEvent(QMouseEvent *event) override;
    15. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class DisplayElement
    2. {
    3. public:
    4. virtual void serialize(QDataStream &outstream) = 0;
    5. virtual void deserialize(QDataStream &instream) = 0;
    6.  
    7. quint8 dataType() {return dataType_;}
    8.  
    9. protected:
    10. quint8 dataType_;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Now, any other custom widget like for example ScreeLineEdit look quite the same as ScreenPushButton. Serialize() and deserialize() will always differ, but the following part could(and should) be shared among all custom widgets.
    Qt Code:
    1. private:
    2. QPoint offset_;
    3. virtual void mousePressEvent(QMouseEvent *event) override;
    4. virtual void mouseMoveEvent(QMouseEvent *event) override;
    To copy to clipboard, switch view to plain text mode 

    My first very naive try was to move the definition of the mouse events to DisplayElement but that of course won't work because then these function signatures would be ambiguous. Calling this function could either be a call to QPushButton::mousePressEvent() or a call to DisplayItem::mousePressEvent().

    Another problem I stumbled upon is that only the first inherited class can be a Q_OBJECT.

    Any ideas how to resolve this issue? Do I need to provide more information?

    Kind regards
    k

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

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

    qk (21st February 2017)

  4. #3
    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.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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.

  6. #5
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.