Results 1 to 4 of 4

Thread: focusNextChild Segmentatiion Fault

  1. #1
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default focusNextChild Segmentatiion Fault

    Hi, everyone!

    I'm getting a SegFault while implement an eventFilter in a QDialog subclass.

    The code bellow works fine in Qt 4.8.6, in Qt 5.3.1 it Segfault.

    Qt Code:
    1. ISCBase::ISCBase(QWidget *parent) : QDialog(parent)
    2. {
    3. setGeometry(0, 0, 768, 450);
    4.  
    5. this->installEventFilter(this);
    6. }
    7.  
    8. bool ISCBase::eventFilter(QObject *object, QEvent *event)
    9. {
    10. if (event->type() == QEvent::KeyPress) {
    11. QKeyEvent *keyEvent = (QKeyEvent*) event;
    12.  
    13. if ((keyEvent->key() == Qt::Key_Return) || (keyEvent->key() == Qt::Key_Enter)) {
    14. ISCBase *base = qobject_cast<ISCBase*>(object);
    15.  
    16. if (
    17. (base) && (
    18. (qobject_cast<QRadioButton*>(base->focusWidget())) ||
    19. (qobject_cast<QCheckBox*>(base->focusWidget())) ||
    20. (qobject_cast<QComboBox*>(base->focusWidget())) ||
    21. (qobject_cast<QDateEdit*>(base->focusWidget())) ||
    22. (qobject_cast<QDoubleSpinBox*>(base->focusWidget())) ||
    23. (qobject_cast<QFontComboBox*>(base->focusWidget())) ||
    24. (qobject_cast<QSlider*>(base->focusWidget())) ||
    25. #if QT_VERSION >= 0x050000
    26. (qobject_cast<QKeySequenceEdit*>(base->focusWidget())) ||
    27. #endif
    28. (qobject_cast<QLineEdit*>(base->focusWidget())) ||
    29. (qobject_cast<QRadioButton*>(base->focusWidget())) ||
    30. (qobject_cast<QSpinBox*>(base->focusWidget())) ||
    31. (qobject_cast<QTimeEdit*>(base->focusWidget())))
    32. ) {
    33. focusNextChild();
    34.  
    35. return true;
    36. }
    37. }
    38. }
    39.  
    40. return QObject::eventFilter(object, event);
    41. }
    To copy to clipboard, switch view to plain text mode 

    Any help will be welcome.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: focusNextChild Segmentatiion Fault

    Why are you installing an event filter on your own class? Event filters are intended so that some other class can monitor events in a different class.

    If you want to handle key press events, simply reimplement the QWidget::keyPressEvent() handler in your dialog class. Have you determined that using an event filter is the only way you can trap the return key?

    You are calling QObject::eventFilter() at the end, instead of QDialog::eventFilter(), but again, you shouldn't be using an event filter in this way in the first place. You are also not setting the accept() flag on the event when you do handle it.

  3. #3
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default Re: focusNextChild Segmentatiion Fault

    Installing the eventFilter in my own class give me the possibily to handle all kinds of events in one central location. And the Qt documentation doesn't point this as bad habit, nor recomend against it.

    Qt Creator use this kind of eventFilter control (src/plugins/coreplugin/locator/locatorwidget.cpp), see line 262.
    Qt Code:
    1. // We set click focus since otherwise you will always get two popups
    2. m_fileLineEdit->setButtonFocusPolicy(Utils::FancyLineEdit::Left, Qt::ClickFocus);
    3. m_fileLineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
    4. m_fileLineEdit->installEventFilter(this);
    5. this->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    The error persists calling QObject::eventFilter or QDialog::eventFilter.

    Oddly enough this same code runs correctly under Qt 5.3.1 in windows. It's seems that it could be related to the linux version of Qt.

    I track the error to a QPaintEvent using gdb.

    Anyway thanks for the help.

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

    Default Re: focusNextChild Segmentatiion Fault

    Quote Originally Posted by croscato View Post
    Installing the eventFilter in my own class give me the possibily to handle all kinds of events in one central location.
    In such case you should reimplement event() for the object. All events pass through there before reaching their respective event handlers. Installing an event filter on yourself is not a good solution. And in your particular case you are handling one type of event so there is no reason not to put it in keyPressEvent().

    Maybe you should state what exactly you are trying to do?
    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.


Similar Threads

  1. segmentation fault!!
    By Yayati.Ekbote in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2010, 16:10
  2. Seg Fault
    By giusepped in forum Qt Programming
    Replies: 8
    Last Post: 23rd October 2008, 09:12
  3. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 17:52
  4. segmentation fault
    By dreamer in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2008, 08:48
  5. Seg fault, but why?
    By Lykurg in forum Newbie
    Replies: 5
    Last Post: 8th March 2007, 23:49

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.