Results 1 to 8 of 8

Thread: events don't work

  1. #1
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default events don't work

    I tried setting up both a mouse and keyboard event to try to learn a bit more about Qt and C++. The mouse event is to make the centre mouse button available for paste and the keyboard event to mean when the return key is pressed in a tablewidget the cursor moves to the next cell down. Both compile neither work and no errors are given. The mouse would be nice the return key is annoying and hence more important. Where am I going wrong?

    Class and function defs
    Qt Code:
    1. public:// public
    2. void moveDown();
    3.  
    4. protected: // protected
    5. void x11mouseEvent(QMouseEvent *event);
    6. bool returnKey(QKeyEvent* event);
    7. private:
    8. QTableWidget *table;
    9. QEvent *event;
    To copy to clipboard, switch view to plain text mode 

    function code

    Qt Code:
    1. // centre mouse uses an existing paste() function
    2.  
    3. void Spreadsheet::x11mouseEvent(QMouseEvent *event)
    4.  
    5. {
    6. QClipboard *clipboard = QApplication::clipboard();
    7. if (event->button()==Qt::MidButton && clipboard->supportsSelection())
    8. {
    9. QString text = clipboard->text(QClipboard::Selection);
    10. paste(); // calls paste function for spreadsheet (this function works fine just not with centre mouse)
    11. }
    12. }
    13.  
    14. // cell down functions
    15.  
    16. void Spreadsheet::moveDown()
    17. {
    18.  
    19. int row=currentRow();
    20. row += 1;
    21. int column=currentColumn();
    22.  
    23. if (row>=RowCount) // check bounds are possible
    24. {
    25. row=0;
    26.  
    27. }
    28.  
    29. setCurrentCell(row, column);
    30.  
    31. }
    32. bool Spreadsheet::returnKey(QKeyEvent* event)
    33. {
    34. if (event->type()==QEvent::KeyPress)
    35. {
    36. QKeyEvent *keyEvent=static_cast<QKeyEvent *>(event);
    37. if ((keyEvent->key()==Qt::Key_Return) && (table->hasFocus()) ) // hasFocus probably not needed doesn't help
    38. {
    39. moveDown();
    40. return true;
    41. }
    42.  
    43. }
    44.  
    45. return QWidget::event(event);
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: events don't work

    use mousePressEvent() and keyPressEvent()

  3. #3
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: events don't work

    Thanks MrDeath

    This code below works perfectly for keyboard haven't tried mouse yet but I'm sure it will be fine, incidentally how do you formally post a thanks? I cannot work out what to do.

    Qt Code:
    1. void Spreadsheet::keyPressEvent( QKeyEvent *e )
    2. {
    3. if ( e->key() == Qt::Key_Return )
    4. {
    5. moveDown();
    6. }
    7.  
    8.  
    9. else
    10. return;
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: events don't work

    Actually I spoke too soon there is a problem! Pressing return works fine, the problem is pressing any other key doesn't work. To enter anything in the cells you have to double click with a mouse. The F2 key doesn't work either. I tried altering the code to this, but it doesn't work although it compiles and again no run errors.....

    Can anyone tell me what's wrong thanks in advance...

    Qt Code:
    1. void Spreadsheet::keyPressEvent( QKeyEvent *event )
    2. {
    3. if ( event->key() == Qt::Key_Return )
    4.  
    5. {
    6. moveDown();
    7. }
    8.  
    9. else
    10. {
    11. QWidget::keyPressEvent(event);
    12. }
    13.  
    14. }
    15. void QWidget::keyPressEvent(QKeyEvent *event)
    16. {
    17. event->ignore();
    18. }
    To copy to clipboard, switch view to plain text mode 

  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: events don't work

    Are you sure you are overriding events of proper widgets? What is the point of handling events that are meant for a child widget (the table widget) in its parent (the "Spreadsheet" widget)?

    And by the way - you are probably doing the wrong thing in the first place. Middle mouse button already has the paste functionality if you are running on a x11 system so it's just a matter of convincing your table it should actually perform the paste and the return key should also do what you want by itself if you tweak the values of properties of the table widget.
    Last edited by wysota; 24th March 2010 at 13:55.
    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.


  6. #6
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: events don't work

    wysota you are right about the X11 mouse issue. There is some functionality without the code I posted although not as much as calc, in calc you copy with the left button and centre click in any cell and then the contents are pasted in. In a tablewidget you have to double (left)click first. Gnumeric has no centre button capabilities. I was hoping to replicate this calc behaviour but can live w/o it. The enter key not moving a cell down is annoying I'm surprised it is not default behaviour for the widget. I wonder if its possible to look for a regular expression for all other keys? Excuse my ignorance still a beginner....

  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: events don't work

    You are thinking low-level. Don't think in terms of keys, events and buttons. Think in terms of functionality you want to achieve.

    You can start by taking a look at QAbstractItemView::closeEditor()
    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.


  8. #8
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: events don't work

    Thanks I think that is beyond me I'll comment out the code and live with the behaviour until my knowledge grows....

Similar Threads

  1. Replies: 2
    Last Post: 13th December 2009, 21:27
  2. Events
    By T0bi4s in forum Newbie
    Replies: 16
    Last Post: 25th November 2009, 23:09
  3. Replies: 3
    Last Post: 15th January 2008, 13:11
  4. QT Events
    By Sreeja in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2006, 09:25
  5. Events - how to
    By eleanor in forum Newbie
    Replies: 2
    Last Post: 13th November 2006, 08:23

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.