Results 1 to 5 of 5

Thread: KeyEvent on StackedWidget

  1. #1
    Join Date
    Apr 2017
    Posts
    2

    Default KeyEvent on StackedWidget

    Hey, at first i must apologize for my poor english. I am new in Qt and seek for an solution of the follow issue. First my code:

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *ev){
    2. if((obj == ui->stackedWidget) && (ui->stackedWidget->currentIndex() == 1)){
    3. if(ev->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent key = static_cast<QKeyEvent>(ev);
    6. if(key.key() == Qt::Key_Control){
    7.  
    8. ui->stackedWidget->setCurrentIndex(0);
    9. this-> tcpSocket->closeSocket();
    10.  
    11. }
    12.  
    13. }
    14. else{
    15. QPoint position = this->mapToGlobal(this->pos());
    16. int x = position.x(); int y=position.y();
    17. this->cursor().setPos((x/2)+175, (y/2)+175);
    18. }
    19.  
    20. return true;
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    after my attempt to check wether control key was pressed, about a key event in this function, i received folllow error message:

    Error: no matching function for call to 'QKeyEvent::QKeyEvent(QEvent*&)'
    QKeyEvent key = static_cast<QKeyEvent>(ev);

    somebody has an idea??

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: KeyEvent on StackedWidget

    you forgot the pointer '*':
    Qt Code:
    1. QKeyEvent* key = static_cast<QKeyEvent>(ev);
    To copy to clipboard, switch view to plain text mode 
    Why are you using static_cast??
    I think what you are after is dynamic_cast<>
    Last edited by high_flyer; 15th April 2017 at 01:01.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,309
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: KeyEvent on StackedWidget

    you forgot the pointer '*':
    So did you

    Qt Code:
    1. QKeyEvent * key = dynamic_cast<QKeyEvent *>(ev);
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Apr 2017
    Posts
    2

    Thumbs up Re: KeyEvent on StackedWidget

    I have the pointers set now, such as bottom in the cut out of my code. It works finaly, Thank You!!!

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *ev){
    2. if((obj == ui->stackedWidget) && (ui->stackedWidget->currentIndex() == 1)){
    3. if(ev->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *key = dynamic_cast<QKeyEvent*>(ev);
    6. if(key->key() == Qt::Key_Control){
    7.  
    8. ui->stackedWidget->setCurrentIndex(0);
    9. ui->stackedWidget->setCursor(Qt::ArrowCursor);
    10. this->tcpSocket->closeSocket();
    11. return true;
    12. }
    13.  
    14. }
    15. else{
    16. this->position = this->mapToGlobal(this->pos());
    17. int x = position.x(); int y=position.y();
    18. this->cursor().setPos((x/2)+175, (y/2)+175);
    19. return true;
    20. }
    21.  
    22. return false;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: KeyEvent on StackedWidget

    So did you
    Hehe, true! :-)


    Added after 11 minutes:


    I have the pointers set now, such as bottom in the cut out of my code. It works finaly, Thank You!!!
    Our help here was giving you a fish, but it seems to me you need to learn how to fish, do you understand what your error was?
    Its seems to me you do not understand what pointers are, and what static_cast vs dynamic_cast do.
    I urge you to look C++ basics first before you do any further work with C++ otherwise you will get in to a lot of frustration.
    Last edited by high_flyer; 16th April 2017 at 23:02.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. pressing and holding a keyEvent
    By Alex22 in forum Newbie
    Replies: 2
    Last Post: 5th March 2016, 17:42
  2. KeyEvent Handling
    By parulkalra14 in forum Qt Programming
    Replies: 5
    Last Post: 21st January 2014, 13:17
  3. keyevent
    By mero in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2011, 11:32
  4. keyevent/zoom
    By bhogasena in forum Qt Programming
    Replies: 7
    Last Post: 23rd January 2009, 15:28
  5. KeyEvent propagation to Desktop
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2007, 13:38

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.