Results 1 to 10 of 10

Thread: event(QEvent * event)

  1. #1

    Default event(QEvent * event)

    hi
    i am facing some problem to capture key events in subclass of QObject. i am using qt 3.3.5 . i wanted to implement key board navigation on QTabWidget objects contained in QObect

    Following is my code
    Qt Code:
    1. bool event(QEvent*); is declared in ABC.h
    2.  
    3.  
    4. bool ABC::event(QEvent * event)
    5. {
    6.  
    7. int cnt = m_pMasterTab->count();// m_pMasterTab is QTabWidget declared at private member of ABC class
    8.  
    9. if(event->type() == QEvent::KeyPress)
    10. {
    11. QKeyEvent *k = (QKeyEvent*)event;
    12. Qt::Key keyPressed = (Qt::Key)k->key();
    13. if(keyPressed == Qt::Key_Left)
    14. {
    15. int indx = m_pMasterTab->currentPageIndex();
    16. m_pMasterTab->setCurrentPage ((indx ==0)?cnt:indx-1 ) ;
    17. }
    18. if(keyPressed == Qt::Key_Right)
    19. {
    20. int indx = m_pMasterTab->currentPageIndex();
    21. m_pMasterTab->setCurrentPage ((indx+1)%cnt ) ;
    22. }
    23. return true;
    24. }
    25. else
    26. return QObject::event(event);
    27. }
    To copy to clipboard, switch view to plain text mode 

    Please do let me know your insigths . Thanks in advance.

    regards
    raja rao
    Last edited by jacek; 21st February 2007 at 14:13. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event(QEvent * event)

    Quote Originally Posted by rajaraob View Post
    hi
    i am facing some problem to capture key events in subclass of QObject.
    What kind of problems?

    Quote Originally Posted by rajaraob View Post
    Please do let me know your insigths
    The only "insight" I can give you at this point(i.e. without knowing what your problem is) is, that you swallow all keypress events. If that is not what you want you should probably rather do

    Qt Code:
    1. bool ABC::event(QEvent * event)
    2. {
    3.  
    4. int cnt = m_pMasterTab->count();// m_pMasterTab is QTabWidget declared at private member of ABC class
    5.  
    6. if(event->type() == QEvent::KeyPress) {
    7. QKeyEvent *k = (QKeyEvent*)event;
    8. Qt::Key keyPressed = (Qt::Key)k->key();
    9. if (keyPressed == Qt::Key_Left) {
    10. int indx = m_pMasterTab->currentPageIndex();
    11. m_pMasterTab->setCurrentPage ((indx ==0)?cnt:indx-1 ) ;
    12. return true;
    13.  
    14. } else if (keyPressed == Qt::Key_Right) {
    15. int indx = m_pMasterTab->currentPageIndex();
    16. m_pMasterTab->setCurrentPage ((indx+1)%cnt );
    17. return true;
    18. }
    19. }
    20. //is ABC a direct child of QObject?
    21. //Wouldn't it be better to call the event function of the parent class here?
    22. return QObject::event(event);
    23. }
    To copy to clipboard, switch view to plain text mode 

    Would it perhaps be easier to use an Event Filter?
    Last edited by camel; 22nd February 2007 at 09:56.

  3. #3

    Default Re: event(QEvent * event)

    thanks for you reply .

    In my above code ,
    all keyboard events have been recognised on QTAbWidget except left and right arrows.

    is it a bug or any reason in 3.3.5 versions?

    thanks
    raja rao

  4. #4
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event(QEvent * event)

    Quote Originally Posted by rajaraob View Post
    In my above code ,
    all keyboard events have been recognised on QTAbWidget except left and right arrows.
    I am sorry, I am not sure I understand. The QTabWidget receives all keyboard event except left and right arrows?


    Could you please create a small (but complete) programm that shows this behaviour? Any solution would have to include more information than you gave here. What type is ABC? How is ABC used etc.

    Quote Originally Posted by rajaraob View Post
    is it a bug or any reason in 3.3.5 versions?
    The chances are much better that there is a bug in your code, or that how you think event propagation works, is not how it really does work...

  5. #5

    Default eventFilter()

    Here is my modified code with eventfilter and installedeventfilters(...)

    class ABC : public QObject
    {
    private :
    QTabWidget *m_pMasterTab ;
    Q_OBJECT

    public :
    ABC();
    };

    void ABC::ABC()
    {
    m_pMasterTab = new QTabWidget("mytabwidget");
    m_pMasterTab->installEventFilter(this);//event filter
    }
    //Event filter
    bool ABC::eventFilter(QObject * object, QEvent * event)
    {
    int cnt = m_pMasterTab->count();

    if(event->type() == QEvent::KeyPress)
    {
    QKeyEvent *k = (QKeyEvent*)event;
    Qt::Key keyPressed = (Qt::Key)k->key();
    if(keyPressed == Qt::Key_Left)
    {
    int indx = m_pMasterTab->currentPageIndex();
    m_pMasterTab->setCurrentPage ((indx ==0)?cnt:indx-1 ) ;
    }
    if(keyPressed == Qt::Key_Right)
    {
    int indx = m_pMasterTab->currentPageIndex();
    m_pMasterTab->setCurrentPage ((indx+1)%cnt ) ;
    }

    return true;
    }
    else
    return QObject::eventFilter(object,event);
    }


    This is my complete code of implementation .
    If i print key value , i can see every key is recognised except left and right arrows.

    regards
    raja rao
    Last edited by rajaraob; 22nd February 2007 at 10:44. Reason: reformatted to look better

  6. #6
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event(QEvent * event)

    A complete example, is for example this:

    The main.cpp
    Qt Code:
    1. #include "qapplication.h"
    2. #include "qlabel.h"
    3. #include "qtabwidget.h"
    4.  
    5. class ABC : public QTabWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. ABC(QWidget * parent = 0, const char * name = 0, WFlags f = 0)
    10. : QTabWidget(parent, name, f) { }
    11.  
    12. protected:
    13. bool event(QEvent* e)
    14. {
    15. if(e->type() == QEvent::KeyPress) {
    16. QKeyEvent *k = (QKeyEvent*)e;
    17. Qt::Key keyPressed = (Qt::Key)k->key();
    18. if (keyPressed == Qt::Key_Left) {
    19. int indx = currentPageIndex();
    20. setCurrentPage ((indx ==0)?count():indx-1 ) ;
    21. return true;
    22.  
    23. } else if (keyPressed == Qt::Key_Right) {
    24. int indx = currentPageIndex();
    25. setCurrentPage ((indx+1)%count() );
    26. return true;
    27. }
    28. }
    29. return QTabWidget::event(e);
    30. }
    31. };
    32.  
    33. int main(int argc, char* argv[])
    34. {
    35. QApplication app(argc, argv);
    36.  
    37. ABC *abc = new ABC();
    38.  
    39. QLabel *label1 = new QLabel("Label1", abc);
    40. abc->addTab(label1, "Label1");
    41.  
    42. QLabel *label2 = new QLabel("Label2", abc);
    43. abc->addTab(label2, "Label2");
    44.  
    45. abc->show();
    46.  
    47. return app.exec();
    48. }
    49. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    And then:
    Qt Code:
    1. qmake -project
    2. qmake
    3. make
    To copy to clipboard, switch view to plain text mode 

    Sometimes it is better to inherrit than to wrap....

    Now, the problem is that you do want to change the behaviour of the tabwidget, but you do not recognize the key event?
    Last edited by camel; 22nd February 2007 at 12:30.

  7. #7
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event(QEvent * event)

    And here you got a version that uses eventfilters:
    Qt Code:
    1. #include "qapplication.h"
    2. #include "qlabel.h"
    3. #include "qtabwidget.h"
    4.  
    5. class MyEventFilter : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. MyEventFilter(QWidget *parent = 0, const char *name = 0)
    10. : QObject(parent, name) { }
    11.  
    12. bool eventFilter(QObject *watched, QEvent *e)
    13. {
    14. if (watched->inherits("QTabWidget")) {
    15. QTabWidget *tab = (QTabWidget*)watched;
    16. if(e->type() == QEvent::KeyPress) {
    17. QKeyEvent *k = (QKeyEvent*)e;
    18. Qt::Key keyPressed = (Qt::Key)k->key();
    19. if (keyPressed == Qt::Key_Left) {
    20. int indx = tab->currentPageIndex();
    21. tab->setCurrentPage ((indx ==0)?tab->count():indx-1 ) ;
    22. return true;
    23.  
    24. } else if (keyPressed == Qt::Key_Right) {
    25. int indx = tab->currentPageIndex();
    26. tab->setCurrentPage ((indx+1)%tab->count() );
    27. return true;
    28. }
    29. }
    30. }
    31. return false;
    32. }
    33. };
    34.  
    35. int main(int argc, char* argv[])
    36. {
    37. QApplication app(argc, argv);
    38.  
    39. QTabWidget *tab = new QTabWidget;
    40.  
    41. QLabel *label1 = new QLabel("Label1", tab);
    42. tab->addTab(label1, "Label1");
    43.  
    44. QLabel *label2 = new QLabel("Label2", tab);
    45. tab->addTab(label2, "Label2");
    46.  
    47. tab->installEventFilter(new MyEventFilter(tab));
    48.  
    49. tab->show();
    50.  
    51. return app.exec();
    52. }
    53. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event(QEvent * event)

    Now that we have something which we can actually work with(and guess what the problem is) we can try out what is wrong.

    What is wrong, is that the QTabWidget does not even get the left and right key-presses, since those are actually taken up by a child widget of its, QTabBar. To change the behaviour of the tabbar, we have to install an eventfilter onto it. And voila it works (As I would imaging you intendet...)
    Qt Code:
    1. #include "qapplication.h"
    2. #include "qlabel.h"
    3. #include "qtabwidget.h"
    4. #include "qtabbar.h"
    5. #include "qtextedit.h"
    6.  
    7. class ABC : public QTabWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. ABC(QWidget * parent = 0, const char * name = 0, WFlags f = 0)
    12. : QTabWidget(parent, name, f)
    13. {
    14. tabBar()->installEventFilter(this);
    15. }
    16.  
    17. virtual bool eventFilter(QObject *watched, QEvent *e)
    18. {
    19. if (watched == tabBar()) {
    20.  
    21. if(e->type() == QEvent::KeyPress) {
    22. QKeyEvent *k = (QKeyEvent*)e;
    23. Qt::Key keyPressed = (Qt::Key)k->key();
    24.  
    25. if (keyPressed == Qt::Key_Left) {
    26. int indx = currentPageIndex();
    27. setCurrentPage ((indx==0)?count()-1:indx-1 ) ;
    28. return true;
    29.  
    30. } else if (keyPressed == Qt::Key_Right) {
    31. int indx = currentPageIndex();
    32. setCurrentPage ((indx+1)%count() );
    33. return true;
    34. }
    35. }
    36. }
    37. return QTabWidget::eventFilter(watched, e);
    38. }
    39.  
    40. protected:
    41. };
    42.  
    43. int main(int argc, char* argv[])
    44. {
    45. QApplication app(argc, argv);
    46.  
    47. ABC *abc = new ABC();
    48.  
    49. QLabel *label1 = new QLabel("Label1", abc);
    50. abc->addTab(label1, "Label1");
    51.  
    52. QLabel *label2 = new QLabel("Label2", abc);
    53. abc->addTab(label2, "Label2");
    54.  
    55. QTextEdit *edit1 = new QTextEdit(abc);
    56. abc->addTab(edit1, "Edit1");
    57.  
    58. abc->show();
    59.  
    60. return app.exec();
    61. }
    62.  
    63. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  9. #9

    Default Re: eventFilter(..) & InstallEventFilters()

    Hi,
    why is my eventFilters() on QTabWidget (..) is not able filter events of left and right arrows key board . where as same code is recognising up and down arrows
    regards
    raja rao

  10. #10
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter(..) & InstallEventFilters()

    Quote Originally Posted by rajaraob View Post
    Hi,
    why is my eventFilters() on QTabWidget (..) is not able filter events of left and right arrows key board . where as same code is recognising up and down arrows
    Because those events (left and right) are taken by the child widget. (The Tabbar) Since the tabbar does not care for the other key events, they are propagated to the qtabwidget.

    This is why I had to install a event filter to override this behaviour (which you can only do through sub-classing, because this is the only way you can gain access to it.).

Similar Threads

  1. QDockWidget-title
    By moowy in forum Qt Programming
    Replies: 18
    Last Post: 23rd April 2014, 20:13
  2. 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
  3. event problem
    By windkracht8 in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2006, 11:52
  4. pixmap onmousemove event help
    By bluesguy82 in forum Qt Programming
    Replies: 11
    Last Post: 9th August 2006, 14:15
  5. 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.