Results 1 to 13 of 13

Thread: Installing Event Filters

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Installing Event Filters

    Good evening,

    I have code as follows:

    Qt Code:
    1. MChat::MChat(QMainWindow *parent) : QMainWindow(parent) {
    2. setupUi(this);
    3. inputChat->installEventFilter(this);
    4. connect(reinterpret_cast<QObject*>(this), SIGNAL(pressedReturn()),
    5. reinterpret_cast<QObject*>(sendButton), SLOT(animateClick()));
    6. }
    7.  
    8. bool MChat::eventFilter(QObject *obj, QEvent *event) {
    9. if (event->type() == QEvent::KeyPress) {
    10. QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
    11. if ( keyEvent->key() == Qt::Key_Return ) {
    12. emit pressedReturn();
    13. return true;
    14. }
    15. else {
    16. // continue standard event processing
    17. return QObject::eventFilter(obj, event);
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    I personally don't think this is good. I'd like to have the eventFilter as a method of inputChat. But to do this I'd have to directly edit the code generated by uic. At least, that's the only way I know. Perhaps someone could show me a better way?

  2. #2
    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: Installing Event Filters

    You can always subclass the widget and implement the functionality there.

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: Installing Event Filters

    If inputChat is an object of QTextEdit, how can I add signals to it (cause that's essentially what I'm doing). The way I see it, inputChat would have to be changed into a new specialized version of QTextEdit which is kind of hard to do unless I change the code uic generates directly.

    I'm sure there is a better way I just don't know how to do it.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Installing Event Filters

    Quote Originally Posted by Dumbledore View Post
    reinterpret_cast<QObject*>(this), reinterpret_cast<QObject*>(sendButton),
    You don't need any casts here as both sendButton and "this" are QObjects.

    Also if you use reinterpret cast anywhere, you should have very good reasons to do so, because it might break portability.

    Quote Originally Posted by Dumbledore View Post
    But to do this I'd have to directly edit the code generated by uic.
    You don't have to: http://doc.trolltech.com/4.3/designe...moting-widgets
    Another solution is to write a widget plugin, but it's a bit of work, so you usually don't do that for custom widgets that are used only in one place.

  5. #5
    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: Installing Event Filters

    You can subclass QTextEdit and implement the added functionality. In Designer you can then promote your QTextEdit widget to your subclass, hence avoiding the need to alter uic generated code.

  6. #6
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: Installing Event Filters

    I have been playing around with the promote feature of designer. I discovered it on my own and realized it must be pertinent. However, it is no simple task to specialize a widget if you are new to this. The compiler whines incessantly when I promote the TextEdit widget to TextEditEx specifying header file AllTextEx.h.

    When I do this:

    "AllTextEx.h"
    Qt Code:
    1. class QTextEditEx : public QTextEdit {
    2. Q_OBJECT
    3.  
    4. protected:
    5. bool eventFilter(QObject* obj, QEvent *event);
    6. signals:
    7. void pressedReturn();
    8. };
    To copy to clipboard, switch view to plain text mode 

    I get: ./AllTextEx.h:2: error: expected class-name before '{' token...

    When I do this:

    Qt Code:
    1. #include <QtCore/QVariant>
    2. #include <QtGui/QAction>
    3. #include <QtGui/QApplication>
    4. #include <QtGui/QButtonGroup>
    5. #include <QtGui/QMainWindow>
    6. #include <QtGui/QMenu>
    7. #include <QtGui/QMenuBar>
    8. #include <QtGui/QPushButton>
    9. #include <QtGui/QStatusBar>
    10. #include <QtGui/QTextBrowser>
    11. #include <QtGui/QWidget>
    12.  
    13. class QTextEditEx : public QTextEdit {
    14. Q_OBJECT
    15.  
    16. protected:
    17. bool eventFilter(QObject* obj, QEvent *event);
    18. signals:
    19. void pressedReturn();
    20. };
    To copy to clipboard, switch view to plain text mode 

    I get:

    ui_MChat.h: In member function `void Ui_MChat::setupUi(QMainWindow*)':
    ui_MChat.h:47: error: no matching function for call to `QTextEditEx::QTextEditEx(QWidget*&)'

    And even something like this yields linker errors:
    Qt Code:
    1. #include <QtCore/QVariant>
    2. #include <QtGui/QAction>
    3. #include <QtGui/QApplication>
    4. #include <QtGui/QButtonGroup>
    5. #include <QtGui/QMainWindow>
    6. #include <QtGui/QMenu>
    7. #include <QtGui/QMenuBar>
    8. #include <QtGui/QPushButton>
    9. #include <QtGui/QStatusBar>
    10. #include <QtGui/QTextBrowser>
    11. #include <QtGui/QWidget>
    12.  
    13. class QTextEditEx : public QTextEdit {
    14. Q_OBJECT
    15. private:
    16. QWidget* mainWindow;
    17. public:
    18. QTextEditEx(QWidget* widget) : mainWindow(widget) {}
    19. protected:
    20. bool eventFilter(QObject* obj, QEvent *event);
    21. signals:
    22. void pressedReturn();
    23. };
    To copy to clipboard, switch view to plain text mode 

    I get:
    MChat.cpp.text$_ZN8Ui_MChat7setupUiEP11QMainWindow[Ui_MChat::setupUi(QMainWindow*)]+0x343): undefined reference to `vtable for QTextEditEx'
    MChat.cpp.text$_ZN8Ui_MChat7setupUiEP11QMainWindow[Ui_MChat::setupUi(QMainWindow*)]+0x353): undefined reference to `vtable for QTextEditEx'
    Last edited by Dumbledore; 12th October 2007 at 23:39.

  7. #7
    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: Installing Event Filters

    What linker errors? QTextEditEx constructor should call QTextEdit constructor. And you don't need an event filter in that case. Simply override the event handler you wish to change.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Installing Event Filters

    Quote Originally Posted by Dumbledore View Post
    I get: ./AllTextEx.h:2: error: expected class-name before '{' token...
    Because of missing #include <QTextEdit>.

    Quote Originally Posted by Dumbledore View Post
    I get:
    MChat.cpp.text$_ZN8Ui_MChat7setupUiEP11QMainWind ow[Ui_MChat::setupUi(QMainWindow*)]+0x343): undefined reference to `vtable for QTextEditEx'
    MChat.cpp.text$_ZN8Ui_MChat7setupUiEP11QMainWind ow[Ui_MChat::setupUi(QMainWindow*)]+0x353): undefined reference to `vtable for QTextEditEx'
    Make sure that the QTextEditEx code is included in your .pro file.

  9. #9
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: Installing Event Filters

    OK, I have included it in my .pro file and I've specified it in Designer and set it to global, and I'm including <QTextEdit>

    "QTextEditEx constructor should call QTextEdit constructor"
    You would think so, but I'm getting this error still:

    ui_MChat.h: In member function `void Ui_MChat::setupUi(QMainWindow*)':
    ui_MChat.h:47: error: no matching function for call to `QTextEditEx::QTextEditEx(QWidge
    ./AllTextEx.h:3: note: candidates are: QTextEditEx::QTextEditEx()
    ./AllTextEx.h:3: note: QTextEditEx::QTextEditEx(const QTextEditEx&)

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Installing Event Filters

    Quote Originally Posted by Dumbledore View Post
    You would think so, but I'm getting this error still:

    ui_MChat.h: In member function `void Ui_MChat::setupUi(QMainWindow*)':
    ui_MChat.h:47: error: no matching function for call to `QTextEditEx::QTextEditEx(QWidge
    ./AllTextEx.h:3: note: candidates are: QTextEditEx::QTextEditEx()
    ./AllTextEx.h:3: note: QTextEditEx::QTextEditEx(const QTextEditEx&)
    Add:
    Qt Code:
    1. public:
    2. QTextEditEx( QWidget *parent = 0 ) : QTextEdit( parent) {}
    To copy to clipboard, switch view to plain text mode 
    to QRextEditEx definition.

  11. #11
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: Installing Event Filters

    Yes that works. But is this normal to have to do? (I got a bad vibe through me as I added that code)

    In any case: thanks for your help guys.

  12. #12
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: Installing Event Filters

    Qt Code:
    1. #include <QTextEdit>
    2.  
    3. class QTextEditEx : public QTextEdit {
    4. Q_OBJECT
    5. public:
    6. QTextEditEx(QWidget* parent = 0) : QTextEdit(parent) {}
    7. protected:
    8. bool event(QEvent*);
    9. signals:
    10. void pressedReturn();
    11. };
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Installing Event Filters

    Quote Originally Posted by Dumbledore View Post
    But is this normal to have to do? (I got a bad vibe through me as I added that code)
    Yes, constructors aren't inherited.

Similar Threads

  1. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  2. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

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.