How to apply a drop shadow effect to QGraphicsItem? - c++

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.

Related

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).

Adding an image to QtGraphicsView?

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);
}

QMainWindow centralWidget border

I have a QMainWindow whose central widget has been set to a QGraphicsView viewing a black scene (for test purposes). Note that in the code below, I use my class derived from QGraphicsView, called CQtGlView, which reimplements only the resizeEvent function.
Regardless of whether I add the view directly,
CQtMainWindow::CQtMainWindow() {
m_glView = new CQtGlView();
setCentralWidget(m_glView);
}
or stick it in a layout with margins of 0 in a dummy widget,
CQtMainWindow::CQtMainWindow() {
m_glView = new CQtGlView();
QWidget* dummy = new QWidget();
QHBoxLayout* l = new QHBoxLayout();
l->setContentsMargins(0,0,0,0);
l->addWidget(m_glView);
dummy->setLayout(l);
setCentralWidget(dummy);
}
I get an unwanted grey border around the widget.
The screenshot below illustrates the problem, visible between my scene and the windows aero border.
This would not be a problem if my application did not allow switching to full screen. The border is very obvious once the rest of the screen is black.
It's possible this area represents the DockWidgetAreas around the outside of the central widget.
Is there anything I can do to solve this other than not use QMainWindow? (Undesirable due to my use of menuBar, tool bars, and statusBar.)
It turns out that QGraphicsView derives from QFrame, where I assumed it was only a QWidget.
The solution to this problem was to call setFrameStyle(QFrame::NoFrame); in the constructor of my QGraphicsView subclass. Or if it was not a subclass,
m_glView->setFrameStyle(QFrame::NoFrame);
Have you tried setFrameShape(QFrame::NoFrame) on the QGraphicsView?

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!

Qt - How to show gif(animated) image in QGraphicsPixmapItem

I am trying to use one blinking image in QGraphicsPixmapItem. The image has shown without the animation effect. The below is the original image, the following is the QGraphicsScene which uses this image in QGraphicsPixmapItem. Can anybody say how to achieve this?.
use this code
QGraphicsScene scene;
QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie(image);
gif_anim->setMovie(movie);
movie->start();
QGraphicsProxyWidget *proxy = scene.addWidget(gif_anim);