Results 1 to 6 of 6

Thread: how couild one simplify calling of same methods for different inherited classes.

  1. #1
    Join Date
    Feb 2013
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how couild one simplify calling of same methods for different inherited classes.

    in my code using several qt widget classes I ses code like this:

    } else if (caMenu *widget = qobject_cast<caMenu *>(w)) {
    //qDebug() << "we have a menu";

    if(data.edata.connected) {
    // set no connection color
    } else {
    widget->setAlarmColors(NOTCONNECTED);
    widget->setProperty("Connect", false);
    }
    widget->setAccessW(data.edata.accessW);

    // choice ================================================== ================================================== ==============
    } else if (caChoice *widget = qobject_cast<caChoice *>(w)) {
    //qDebug() << "we have a choiceButton" << String << value;

    if(data.edata.connected) {
    // set no connection color
    } else {
    widget->setAlarmColors(NOTCONNECTED);
    widget->setProperty("Connect", false);
    }
    widget->setAccessW(data.edata.accessW);

    } else if .....



    ===============================
    how could one call a routine using the same code with the same methods after dynamic casting ?

  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: how couild one simplify calling of same methods for different inherited classes.

    You got it worng..
    how couild one simplify calling of same methods for different inherited classes.
    They not not same methods, they are two different methods each operating on a different TYPE (caMenu and caChoice). The thing common between them is they use the same logic / algorithm / techinique...

    What you have is a compact form possible. The other way I can think of is have a template function somthing like
    Qt Code:
    1. template <typename T>
    2. bool isMyType<T>(QObject * object)
    3. {
    4. T * obj = qobject_cast<T *>(object);
    5. return obj != 0;
    6. }
    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. #3
    Join Date
    Feb 2013
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how couild one simplify calling of same methods for different inherited classes.

    you are right, my phrasing was not correct. However I meant it your way.

    How would you use the templating then in my example?

  4. #4
    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: how couild one simplify calling of same methods for different inherited classes.

    Qt Code:
    1. if(isMyType<caMenu *>(w))
    2. qDebug() << "we have a menu";
    3. ...
    4. if(isMyType<caChoice *>(w))
    5. qDebug() << "we have a choiceButton" << String << value;
    6. ...
    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.

  5. #5
    Join Date
    Feb 2013
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how couild one simplify calling of same methods for different inherited classes.

    yes I understand the way you can test the type of the widget. However I do not see how I can then call the methods (widget->SetPropoerty) correctly casted and how the code is then simplified.

    many thanks for your answers.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how couild one simplify calling of same methods for different inherited classes.

    setAlarmColors
    setProperty
    setAccessW

    what is the highest common class that has all of those methods? Lets say it is ClassX. If there isn't a common clsss then you should make an interface that does contain all the methods, then make all relevant classes inherit the interface (e.g. InterfaceX)
    Then you just need one check:

    Qt Code:
    1. if (ClassX *widget = qobject_cast<ClassX*>(w)) { // or InterfaceX
    2. //qDebug() << "we have a ... not sure exactly what it is, but we know it has all the methods required...
    3.  
    4. if(data.edata.connected) {
    5. // set no connection color
    6. } else {
    7. widget->setAlarmColors(NOTCONNECTED);
    8. widget->setProperty("Connect", false);
    9. }
    10. widget->setAccessW(data.edata.accessW);
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. How to leave including inherited classes?
    By szisziszilvi in forum General Programming
    Replies: 1
    Last Post: 19th May 2011, 16:12
  2. Qt & OpenGl, calling gl-commands within other classes
    By KnufflPuffl in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2011, 10:44
  3. Replies: 5
    Last Post: 17th November 2010, 17:29
  4. Issue in using dynamic property in QT inherited classes.
    By Ratheendrans in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2010, 15:21
  5. can't reuse a pure c++ .h file's methods and classes
    By sincnarf in forum General Programming
    Replies: 11
    Last Post: 3rd August 2007, 03:39

Tags for this Thread

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.