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();
}
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'.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With