Results 1 to 5 of 5

Thread: QFileDialog: Enable/Disable "Open" button in inherited class not working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Thumbs up Re: QFileDialog: Enable/Disable "Open" button in inherited class not working

    Yes, that was it. I made the following changes to my code. When I grab the Open button in the constructor, I install the event filter on that button. I overrode the eventFilter() function as shown. Fixed my problem. Thanks for the help. Now I know...

    Qt Code:
    1. MyFileDialog::MyFileDialog(QWidget* parent, QString startdir)
    2. : QFileDialog(parent, tr("Load Correct Format"), startdir),
    3. myOpenButton(0)
    4. {
    5. // constructor - on setting the myOpenButton pointer, I register
    6. // the event filter on that button.
    7. myOpenButton->installEventFilter(this);
    8. }
    9.  
    10. bool MyFileDialog::eventFilter(QObject* obj, QEvent* event)
    11. {
    12. if (obj == myOpenButton)
    13. {
    14. QEvent::Type type = event->type();
    15. bool valid = validSelection();
    16. bool enabled = myOpenButton->isEnabled();
    17. if (type == QEvent::EnabledChange && enabled && !valid)
    18. {
    19. // don't allow QFileDialog to alter the state of the
    20. // Open button. This is to be handled locally in the
    21. // MyFileDialog::fileSelectionChanged() function.
    22. myOpenButton->setEnabled(false);
    23. return true;
    24. }
    25. else
    26. return false;
    27. }
    28.  
    29. // pass unfiltered events on to the parent class
    30. return QFileDialog::eventFilter(obj, event);
    31. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QFileDialog: Enable/Disable "Open" button in inherited class not working

    I would suggest to check the event type right in the first if, otherwise you are calling validSelection() for every event that the button gets.
    As long as validSelection() is basically just returning a boolean that is fine either way, but if you ever change it to do something more, it becomes just unnecessary overhead.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QFileDialog: Enable/Disable "Open" button in inherited class not working

    Yeah, I had updated that myself after submitting, but thanks for catching it. The actual version of the if statement is shown in the following code block. validSelection() only returns a bool that was already figured out before this point, but it's better this way, because validSelection() never gets called if the type isn't EnabledChange, and if the button isn't currently enabled:

    Qt Code:
    1. if (obj == myOpenButton)
    2. {
    3. if (event->type() == QEvent::EnabledChange &&
    4. myOpenButton->isEnabled() &&
    5. !validSelection())
    6. {
    7. // don't allow QFileDialog to alter the state of the
    8. // Open button. This is to be handled locally in the
    9. // MyFileDialog::fileSelectionChanged() function.
    10. myOpenButton->setEnabled(false);
    11. return true;
    12. }
    13. else
    14. return false;
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. "Change widget class on button click" problem
    By utkozanenje in forum Newbie
    Replies: 3
    Last Post: 23rd May 2011, 00:40
  2. Replies: 3
    Last Post: 26th April 2009, 17:54
  3. Replies: 4
    Last Post: 19th March 2008, 17:47
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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.