Results 1 to 10 of 10

Thread: keypressevent

  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default keypressevent

    Hi to all I’m new to programming and I read a C++ book and now I’m exploring QT and I’m trying to write a simple code just to learn how things work and I’m having errors in this code can any 1 guide me to what will fix this code and make it work

    Thanks to all


    Ok this is what the code dose:

    If you press x you will get a message box with a label that says "Hello XXQt!"
    And if you press y you will get a message that says "Hello YYQt!"
    And then exits

    This is the errors that I get when running it:

    1- C:/Documents and Settings/User/Desktop/testcase/main.cpp:13: error: a function-definition is not allowed here before '{' token

    2- C:/Documents and Settings/User/Desktop/testcase/main.cpp:29: error: expected '}' at end of input

    3- C:/Documents and Settings/User/Desktop/testcase/main.cpp:29: warning: no return statement in function returning non-void



    #include <QApplication>
    #include <QLabel>
    #include <QKeyEvent>
    #include <QWidget>


    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    //QKeyPressEvent *keyprs = new QKeyPressEvent *k;
    void QWidget::keyPressEvent ( QKeyEvent *k )

    {
    switch ((k->ascii()) ) {
    case 'x':
    QLabel *label = new QLabel("Hello XXQt!");
    label->show();
    break;
    case 'y':
    QLabel *label = new QLabel("Hello YYQt!");
    label->show();
    default:
    QApplication::exit();
    break;
    }
    }

    return app.exec();
    }
    [/B][/B][/B]

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

    Default Re: keypressevent

    Am... you should leave Qt ,and start with basic C concepts, like what function definitions and declarations are.
    Once you understand basic C syntax and programming concepts, try learning C++.
    After that, try Qt again.
    ==========================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
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keypressevent

    Thanks a lot for the advice and I know you are right but if you can please show me the corrections for my mistakes


    Thank you

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: keypressevent

    Removed. Please post your code in CODE tags. It's too difficult to follow otherwise.

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

    Default Re: keypressevent

    but if you can please show me the corrections for my mistakes
    In a normal spoken human language, if you write a sentence that has some grammatical or syntax error in it, you can fix that sentence.
    But how can you fix a sentence that is written in gibberish?
    You can't since there is no way to make any coherent sense of it.
    Your code is the same, in the sense that it is C++ gibberish.
    There is no sense what so ever in it, not in the logic flow of it, or its syntax.

    Which is why I suggested you start with the basics.

    The only thing I can offer you is, explain in words what it is you intended for you code to do, and we can then post code that will do that. (depending on how complicated it is)
    ==========================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.

  6. #6
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keypressevent

    what i wanted is to write a code that if i press for example 'x' on the keyboard i get a "label " message
    and if i press y i get a different label message


    Thanks a lot

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

    Default Re: keypressevent

    There you go.
    Though this wont help you if you don't understand how it works...
    Attached Files Attached Files
    ==========================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.

  8. #8
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keypressevent

    thanks man but wt is the file type .....??

  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: keypressevent

    You do understand .zip files, right?

  10. #10
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: keypressevent

    yes but when i unzip it the file type is unknown

Similar Threads

  1. How to block keyPressEvent ?
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2009, 09:14
  2. keyPressEvent issue
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 15:39
  3. keyPressEvent() reimplementation
    By aurelius in forum Newbie
    Replies: 5
    Last Post: 30th October 2008, 16:23
  4. Replace keyPressEvent (dot with tab)
    By the_bis in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2008, 10:43
  5. Getting real key in keyPressEvent
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2008, 14:39

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.