Я использую Qt Creator 4.5.2 (Qt 5.9.5, G CC 7.3.0 64-bit) и работаю в Ubuntu 18.04.
Я пытался отобразить картинку в ' QGraphicsView 'окно, которое QRectF (0,60,480,400) по координатам виджета centralWidget (из QT Creator). Другими словами, изображение должно отображаться на QRectF (0,60,480,400). Я использовал «QGraphicsView», «QGraphicsScene» и «QGraphicsPixmapItem». Но по какой-то причине он отображался на QRectF (0,0,480,400). После некоторых трудностей мне удалось заставить его отображаться в правильном положении, QRectF (0,60,480,400), но я не знаю, почему это сработало. Возможно, это как-то связано с координатами, но я просто не понимаю этого.
Вот проект и код, и я поставил некоторые вопросы в 'mainwindow. cpp'. Вы можете скачать любой файл изображения «.png», переименовать его в «car.png» и поместить в тот же каталог, что и исполняемый двоичный файл.
ordin1.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2020-04-05T11:56:12
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = coordinate1
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main. cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow. cpp:
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
#include "ui_mainwindow.h"
QGraphicsScene *mpScene;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsView *mpView;
QGraphicsPixmapItem *mpImageItem;
mpView = ui->gvCam;
mpScene = new QGraphicsScene;
mpView->setScene(mpScene);
mpScene->setSceneRect(mpView->x(), mpView->y(), mpView->width(), mpView->height());
// Here, mpView: (0, 60, 480, 60)
QRectF rf1 = mpScene->sceneRect();
// Here, rf1: (0, 60, 480, 400) -> make sense
rf1 = mpView->sceneRect();
// Here, rf1: (0, 60, 480, 400) -> make sense
QPixmap image("./car.png");
QPixmap resizedImage = image.scaled(mpView->width(), mpView->height());
mpImageItem = mpScene->addPixmap(resizedImage);
QGraphicsItem *gi1 = mpImageItem->parentItem();
// Here, gi1 = NULL. Make sense since it is the top item, no parent
rf1 = mpImageItem->boundingRect();
// Here, rf1: (0, 0, 480, 400).
// Question 1: this bounding Rect is based on which coordinate? mpView?
rf1 = mpImageItem->sceneBoundingRect();
// Here, rf1: (0, 0, 480, 400) -> make sense
QPointF pos1 = mpImageItem->offset();
// Here, pos1: (0, 0) in 'local coordinates'.
// Question2: which 'local'? 'mpImageItem' itself?
pos1 = mpImageItem->pos();
// Here, pos1: (0, 0) in 'mpScene' coordinates.
pos1 = mpImageItem->scenePos();
// Here, pos1: (0, 0) -> Make sense
// If I include the following line code, the picture will be fully inside the
// 'mpView'. This is what I want. But, I don't understand why.
// If not include the following line, the top part of the picture (60 pixels) will be cut off.
// In other words, it is displayed on QRectF(0,0,480,400). Why?
// mpImageItem->setOffset(mpView->x(), mpView->y());
mpView->show();
}
MainWindow::~MainWindow()
{
delete mpScene;
delete ui;
}
mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>480</width>
<height>460</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QGraphicsView" name="gvCam">
<property name="geometry">
<rect>
<x>0</x>
<y>60</y>
<width>480</width>
<height>400</height>
</rect>
</property>
<property name="font">
<font>
<family>Roboto</family>
<pointsize>16</pointsize>
</font>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>