trante
trante

Reputation: 33996

Qt: show image on widget

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();
}

Upvotes: 8

Views: 25554

Answers (2)

jkerian
jkerian

Reputation: 17046

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.

Upvotes: 10

Andrew
Andrew

Reputation: 24866

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.

Upvotes: 1

Related Questions