Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: show image on widget

Tags:

image

jpeg

qt

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();
}
like image 580
trante Avatar asked Oct 02 '10 17:10

trante


People also ask

How do I create a custom widget in Qt?

Adding the Custom Widget to Qt Designer. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. Click New Widget so that we are ready to add our new widget. Change the Class name from 'MyCustomWidget' to 'Vcr'.

How do I use graphics view in Qt Designer?

The view widget is a scroll area, and provides scroll bars for navigating through large scenes. To enable OpenGL support, you can set a QOpenGLWidget as the viewport by calling QGraphicsView::setViewport(). QGraphicsScene scene; myPopulateScene(&scene); QGraphicsView view(&scene); view. show();


2 Answers

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.

like image 168
jkerian Avatar answered Oct 11 '22 19:10

jkerian


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.

like image 24
Andrew Avatar answered Oct 11 '22 19:10

Andrew