Results 1 to 4 of 4

Thread: How to override setEnabled(..) method of QWidget ?

  1. #1
    Join Date
    Sep 2012
    Location
    Pune, India
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to override setEnabled(..) method of QWidget ?

    Hi,

    I have classes inherited from QWidget.

    class MyWidget : public QWidget
    {
    ----------- Methods -------
    public slots :
    void setEnabled(bool value);

    }


    QWidget* myWidget = new MyWidget(...);

    myWidget->setEnabled(false);
    // this will call to setEnabled(..) of QWidget I want it to callhere MyWidget's setEnabled(...).


    Please let me know if I am missing something here.

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

    Default Re: How to override setEnabled(..) method of QWidget ?

    What do you need it for?
    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.


  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: How to override setEnabled(..) method of QWidget ?

    Quote Originally Posted by anwar.qt View Post
    Please let me know if I am missing something here.
    You are missing something here.

    ...

    ...

    ...

    but you knew that. Here it is: for polymorphism to work, the overridden method has to be virtual (or pure virtual) in the parent class, which is not the case for QWidget::setEnabled(). In addition, you have to call the method using a pointer (or reference, i.e. address-of operator "&"), as you have done. So, you could sub-class QWidget and declare a virtual setEnabled() method, then sub-class that class and implement setEnabled() there. If this is too much bother for a polymorphic setEnabled(), then you'll have to cast myWidget back to a MyWidget * and then call setEnabled(), as follows:

    Qt Code:
    1. dynamic_cast<MyWidget *>(myWidget)->setEnabled(true);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to override setEnabled(..) method of QWidget ?

    When a widget's enabled property is changed, it gets a change event.
    What you could do is override QWidget::changeEvent and call your setEnabled whenever the base class' enabled gets changed.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    d_stranz (2nd December 2012)

Similar Threads

  1. QAction::setEnabled() Not Working as Expected
    By pmwalk in forum Qt Programming
    Replies: 6
    Last Post: 24th March 2012, 06:20
  2. qwtplot::setEnabled(false) ignored?
    By liversedge in forum Qwt
    Replies: 1
    Last Post: 7th August 2011, 20:07
  3. Problem with the QWidget::winId() method
    By yellowmat in forum Qt Programming
    Replies: 5
    Last Post: 22nd April 2008, 12:15
  4. QWidget::testWFlags() - method in Qt 4?
    By Amanda in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2006, 07:08
  5. How setEnabled() works on QFrames.
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 18th October 2006, 19:55

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.