开发者

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) :
        Q开发者_如何学运维MainWindow(parent),
        ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   showIt();
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜