tags:

views:

45

answers:

2

i want to display a car photo.
after loading, user can move the car by pressing keys on keyboard.
when he press "up arrow", car will move upwards.
which methos should i use to show the photo. Qlabel.setpixmap??
The code below shows the image on a new widget, but i want to show it on my mainwindow page.
What should i do?
Thank you

void MainWindow::showIt()
{
    QLabel *image = new QLabel();
    image->setPixmap( QPixmap( "car.jpg" ) );
    image->show();
    update();
}

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   showIt();
}
+1  A: 

It's sounding a bit like you're attempting to write game code with the Qt layout system. This is not particularly recommended.

In your code above, you've placed the image on the QLabel image, but you haven't placed that QLabel on your mainwindow. The easiest way to do that is just to pass this as the first argument to the QLabel constructor.

If this type of animation is the whole point of the application, I'd recommend either using OpenGL and the corresponding class, or the graphics view system.

jkerian
Yes jkerian thank you, i changed to "this".
trante
I'm planning to make a simple game by using Qt. But due to your reccomendation i'm now reading about QGraphicsView and OpenGL. Can you compare this two (QGraphicsView and OpenGL). What are the advantages, disadv? Thank you very much.
trante
OpenGL is significantly more complicated, and really is designed for doing full-blown 3D display. The problem with using OpenGL in this environment is that it is a bit cumbersome to use, and has a bit of a learning curve. GraphicsView is pretty much described in the above link, but is mostly useful in this context because it lets you break away from the usual layout engines.
jkerian
dear jkerian, thank you very much for your help. then i will start with GraphicsView. when i learn that class adequately, i will start to OpenGL. possibly it would be easier that time so understand the OpenGL architecture :) best regards
trante
A: 

If you want to write a 2D game, using Qt - take a look at QPainter class. For example, if you want to have one widget representing your game window you should inherit it from a QWidget or it's subclass and reimplement paintEvent(QPaintEvent *event) function to perform your drawing. You can find a lot of useful code for drawing using QPainter in Qt examples.

Andrew