Results 1 to 3 of 3

Thread: React on KeyEvent inside QTreeWidget

  1. #1
    Join Date
    Apr 2012
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default React on KeyEvent inside QTreeWidget

    Hi,
    I have this problem. I have main form where i have QTreeWidget and label. All I need is to change label text, when i press up and down in QTreeWidget accordingly. Here's sample code:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. tree = new QTreeWidget(this);
    5. label = new QLabel(this);
    6.  
    7. top->setText(0,"top");
    8. top2->setText(0,"top2");
    9. niz->setText(0,"lower");
    10. QTreeWidgetItem *lower2 = new QTreeWidgetItem(0);
    11. niz2->setText(0,"even lower");
    12.  
    13. tree->addTopLevelItem(top);
    14. tree->addTopLevelItem(top2);
    15. top2->addChild(lower);
    16. niz->addChild(lower2);
    17.  
    18. QWidget *widget = new QWidget(this);
    19. QHBoxLayout *layout = new QHBoxLayout();
    20. layout->addWidget(tree);
    21. layout->addWidget(label);
    22. widget->setLayout(layout);
    23. this->setCentralWidget(widget);
    24.  
    25. connect(tree,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(treeClicked(QTreeWidgetItem*,int)));
    26. }
    27.  
    28. void MainWindow::treeClicked(QTreeWidgetItem* item, int col)
    29. {
    30. label->setText(item->text(0));
    31. }
    32.  
    33. void MainWindow::keyPressEvent(QKeyEvent* event)
    34. {
    35. if((event->key() == Qt::Key_Up) || (event->key() == Qt::Key_Down))
    36. {
    37. label->setText(tree->currentItem()->text(0));
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    with mouse clicks, everything works fine, but keypress event works only when Im at the top or at the bottom of QTreeWidget (I assume that it's because QTreeWidget actually loses focus when im for example at the top of widget and i press Up, so MainWindow gets focus and is able to call keyPressEvent). But how to override keyPressEvent of treeWidget inside of main form? Thanks, so far I was unable to find working solution.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: React on KeyEvent inside QTreeWidget

    You need to intercept key events going to the tree widget not the main window.

    Event filter seems to be easiest solution in this case:
    Qt Code:
    1. MainWindow::MainWindow( QWidget* parent )
    2. : QMainWindow( parent )
    3. {
    4. [...] // your setup
    5.  
    6. this->tree->installEventFilter( this );
    7. }
    8.  
    9. bool MainWindow::eventFilter( QObject* o, QEvent* e )
    10. {
    11. if( o == this->tree && e->type() == QEvent::KeyRelease )
    12. {
    13. label->setText(tree->currentItem()->text(0));
    14. }
    15.  
    16. return false;
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Spitfire for this useful post:

    Raadush (10th April 2012)

  4. #3
    Join Date
    Apr 2012
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: React on KeyEvent inside QTreeWidget

    Thanks a lot, this is exactly what I needed. Works like charm. Thanks

Similar Threads

  1. Replies: 0
    Last Post: 11th January 2011, 09:00
  2. QTreeWidget row updates inside QDialog
    By innerhippy in forum Qt Programming
    Replies: 4
    Last Post: 19th December 2008, 18:32
  3. Drag and Drop item inside QTreeWidget
    By nina1983 in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2008, 11:43
  4. ScrollBar inside QTreeWidget is Blurring on Scrolling
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 11th April 2008, 14:33
  5. Resizing a QTreeWidget inside a layout
    By bruccutler in forum Qt Programming
    Replies: 11
    Last Post: 27th March 2007, 15:35

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.