Results 1 to 10 of 10

Thread: event(QEvent * event)

Hybrid View

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

    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 

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

    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 

  3. #3

    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

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

    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
  •  
Qt is a trademark of The Qt Company.