Different coordinates after setGeometry applied to QGraphicsView
-
Summary:
Windows 11, Qt 6. I apply setGeometry to QGraphicsView which is inside of QMainWindow, but in result I get QGraphicsView in different x and y coordinates.
I use view.setGeometry(120, 120, 320, 240); and get QGraphicsView in x 150 and y 150 coordinates, I expect it to be x 120, y 120.Code:
#include <QApplication> #include <QMainWindow> #include <QGraphicsScene> #include <QGraphicsView> #include <QGraphicsVideoItem> #include <QMediaPlayer> #include <QAudioOutput> int main(int argc, char *argv[]) { QApplication app(argc, argv); // Create the main window QMainWindow mainWindow; mainWindow.setWindowTitle("Video Player"); mainWindow.setGeometry(100, 100, 800, 600); // Create a central widget to hold the QGraphicsView QWidget centralWidget; mainWindow.setCentralWidget(¢ralWidget); // Create a QGraphicsView and a QGraphicsScene QGraphicsView view(¢ralWidget); QGraphicsScene scene; view.setScene(&scene); view.setGeometry(120, 120, 320, 240); // Create a QMediaPlayer and set the media content QMediaPlayer mediaPlayer; mediaPlayer.setSource(QUrl::fromLocalFile("PM5544.mp4")); // Create a QGraphicsVideoItem and set the media player QGraphicsVideoItem videoItem; QAudioOutput audioOutput; // Add the video item to the scene scene.addItem(&videoItem); mediaPlayer.setAudioOutput(&audioOutput); mediaPlayer.setVideoOutput(&videoItem); // Play the video mediaPlayer.play(); // Show the main window mainWindow.show(); return app.exec(); }
Is that a bug, or I am doing something wrong?
-
@DemensDeum said in Different coordinates after setGeometry applied to QGraphicsView:
and get QGraphicsView in x 150 and y 150 coordinates
Can you show in your code (e.g. a
qDebug()
statement) where you are seeing this, please? -
@DemensDeum
I think you're seeing the difference between logical and physical pixels.When I run your code, the following:
// Show the main window mainWindow.show(); qDebug() << view.geometry() << QGuiApplication::primaryScreen()->devicePixelRatio();
prints out:
QRect(120,120 320x240) 1.5
and when I look at a screen dump the graphics view is offset by 180x180 physical pixels, as expected from the pixel ratio printed.
-
@KenAppleby-0 Thank you.
Now I am trying to understand why every app adapt dpi differently, Gimp, Firefox and Qt.