PDA

View Full Version : keypressevent



djwk
30th June 2010, 13:12
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]

high_flyer
30th June 2010, 13:51
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.

djwk
1st July 2010, 00:02
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

SixDegrees
1st July 2010, 00:09
Removed. Please post your code in CODE tags. It's too difficult to follow otherwise.

high_flyer
1st July 2010, 10:10
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)

djwk
1st July 2010, 10:51
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

high_flyer
1st July 2010, 11:34
There you go.
Though this wont help you if you don't understand how it works...

djwk
4th July 2010, 11:56
thanks man but wt is the file type .....??

squidge
4th July 2010, 13:25
You do understand .zip files, right?

djwk
5th July 2010, 02:12
yes but when i unzip it the file type is unknown