Adding an image to QtGraphicsView? - c++

Hi iv looked at other questions and solutions for this but none seem to help for my specific problem.
Im simply trying to add a picture to my GraphicsView that i have added using Qt designer
.cpp
void test::populateScene()
{
QImage image(":/images/myFile.png");
QGraphicsPixmapItem item(QPixmap::fromImage(image));
QGraphicsScene *scene = new QGraphicsScene;
scene->addItem(&item);
ui->graphicsView->setScene(scene);
}
i have the necessaary includes but when i click run the program just puts up a not responding message straight away
i have no compiler errors just hit run and then then get test.exe is not responding
any ideas, this seems like it should be really simple but i cant work out why this isnt right (mainly due to no compiler errors to look through and find the cause of the crash)

[SOLVED]
QImage image(":/images/myFile.png");
QGraphicsScene *scene = new QGraphicsScene();
scene->addPixmap(QPixmap::fromImage(image));
scene->setSceneRect(0,0,image.width(),image.height());
ui->graphicsView->setScene(scene);
No other solution i found here or elsewhere for getting an image into a graphicsview have worked so i will post this to help others now my problem is solved feel free to ask questions

You have to allocate your new item on the heap!
http://harmattan-dev.nokia.com/docs/library/html/qt4/qgraphicsscene.html#addItem
Adds or moves the item and all its childen to this scene. This scene takes ownership of the item.
The description of the function is quite clear: it will need pointer that is valid for more that the scope of your function, so that it can take ownership of it.
void test::populateScene()
{
QImage image(":/images/myFile.png");
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
QGraphicsScene *scene = new QGraphicsScene;
scene->addItem(item);
ui->graphicsView->setScene(scene);
}

Related

How to remove a QWidget from a QGraphicsScene?

I am trying to accomplish the following:
(1) add a QPushButton to a QGraphicsScene with addWidget()
(2) display this QGraphicsScene with QGraphicsView
(3) remove that button later (for example, when I click it).
I have done (1) and (2) but I have no idea how to do (3). Another question on this site asked a similar question, one of whose answers suggested something like the following:
QPushButton* startGame = new QPushButton("Start Game");
QGraphicsScene* scene = new QGraphicsScene;
QGraphicsProxyWidget* proxy = scene->addWidget(startGame);
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
scene->removeItem(proxy);// without this everything is fine
But I received this error: no matching function for call to QGraphicsScene::removeItem(QGraphicsProxyWidget*&)'. Why is this? How can I solve it?
Btw, I am using Qt creator 4.7.0 based on Qt 5.11.1 (MSVC2015, 32 bit).
Any help is greatly appreciated.

Video not fitting in properly in QGraphicsView

I am trying a play a video(640 * 360) through rtsp in QGraphicsView. But the issue is that it is not fitting completely within the view and scroll bar is appearing, which should not happen. And moreover, I am able to get the same peace of code working properly in Linux environment but I am getting the issue in windows.
Please find the code snippet below, If anyone can point out the error I am making will be helpful.
scene = new QGraphicsScene(this);
view= new graphicsView();
view->setScene(scene);
videoItem = new QGraphicsVideoItem;
player= new QMediaPlayer;
player->setVideoOutput(videoItem);
view->scene()->addItem(videoItem);
controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(view);
view->setSceneRect(scene->sceneRect());
view->scale(1.97,1.97);
ui.m_pframePlay->setLayout(controlLayout);
ui.m_pframePlay->show();
player->setMedia(QUrl("rtsp:..."));
player->play();
The documentation for QGraphicsView sais about setSceneRect
The scene rectangle defines the extent of the scene, and in the view's case, this means the area of the scene that you can navigate using the scroll bars.
This means, setSceneRect does not resize the visible area of the view but only which area of the scene is visible in the view. So I guess you simply have to resize your view, e.g.
view->resize(scene->width()*1.97, scene->height()*1.97)
(I scaled width/height with 1.97 because you scale your view using factor 1.97 for some reason).

QGraphicsItem scaling produces too large bounding rect

Problem: When using a QGraphicsItem with the flag QGraphicsItem::ItemIgnoresTransformations, the View does not scale properly and shows unnecessary scroll bars.
To reproduce, place a QGraphicsView on a Form and use this code:
#include <QGraphicsScene>
#include <QGraphicsTextItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene();
// Set the scene to the view. Has to be done before
// transformation in order for the problem to occur.
ui->graphicsView->setScene(scene);
// Add some text, make it transformation-invariant.
QGraphicsTextItem* txt = scene->addText("Hello World!");
txt->setFlag(QGraphicsItem::ItemIgnoresTransformations);
// Scale the scene, re-calculate the bounding rect.
ui->graphicsView->scale(10, 5);
QRectF rect = scene->itemsBoundingRect();
scene->setSceneRect(rect);
}
This also works with other Items like QGraphicsEllipseItem or QGraphicsRectItem.
Without setting the flag (simply comment out the line txt->setFlag...), the output is as expected:
However, when the flag is set, I would expect the scroll bar to disappear, because the text clearly fits into the view. But instead it looks like this:
I know that the Scene does only automatically grow, but not shrink to its content, so I am explicitly setting the Scene Rect at the end. But even this does not help.
It seems to me like this is a bug in Qt, but maybe I also simply misunderstood something. Any idea what the problem (and solution) is?
Using Qt 5.5, Ubuntu 14.04.
PS: Yes, the scene is never freed. This is of course no production code ;-)
I had a similar issue with addLine and addEllipse on top of an QImage.
The problem seems to come from the QGraphicsItem::ItemIgnoresTransformations flag that seems to enlarge the scene to hold the "transformed" object placement, but then ignores the transform for placement and size, however the damage is already done because it has incorrectly changed the scene scale.
I have a work-around that might be able to help you.
If you reset the scene scale to a known correct value after positioning the objects that ignore the transform. It will avoid your problem.
For example:
m_firstImageScene->setSceneRect(m_firstImageRect);

How to apply a drop shadow effect to QGraphicsItem?

I am new to QT and I want to draw shadow for my QGraphicsRectItem but it doesn't seem to be working
I use following code in my QGraphicsScene
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(50);
QGraphicsRectItem * item = new QGraphicsRectItem(100,100,50,50);
item->setBrush(QBrush(Qt::cyan));
item->setGraphicsEffect(effect);
scene->addItem(item);
However, this works fine if I apply this to my graphicsView like this
ui->graphicsView->setGraphicsEffect(effect);
What am I doing wrong?
Probably you create effect locally and this pointer is erased, so move declaration of effect to header file.

Code works in main but not in other classes

I'm trying to use QGraphicsView and QGraphicsScene in my Qt application but for some reason I can't get it to work. I have the following code which will work if I execute it from the main class but not from a controller class which inherits QObject:
QGraphicsScene scene;
scene.setSceneRect(0,0,200,200);
scene.setBackgroundBrush(Qt::blue);
QGraphicsView *view = new QGraphicsView();
view->setScene(&scene);
view->show();
If I do it in main the scene is blue but if I do it in the other class the scene is white. What is going on?
Change to this:
{
QGraphicsScene * scene = new QGraphicsScene();//note that we allocate it on the heap
scene->setSceneRect(0,0,200,200);
scene->setBackgroundBrush(Qt::blue);
QGraphicsView *view = new QGraphicsView();
view->setScene(scene);
view->show();
<...>
}//your function ends here
In your version, the scene is created on the stack, so if you put this code anywhere in the class, the scene will die immediately at the end of the function. That's why it is white. If you allocate it on the heap, it will stay alive after the closing bracket, and you will be able to see it.
Please do not forget to delete it after!