Code works in main but not in other classes - c++

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!

Related

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.

How to update Graphics View in a Qt Widgets Project(Qt Creator)

I have created a Qt Widgets Application using Qt Creator(Windows 7, MinGW).
I have added a GraphicsView (named as graphicsView) and a push button (named as pbClick).
The on_pbClick_clicked() function is given below:
void MainWindow::on_pbClick_clicked()
{
QGraphicsScene scene;
//adding some text to the scene
scene.addText("Hello, world!", QFont("Times", 20, QFont::Bold));
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
}
When I click the pbClick button, nothing happens within the graphicsView.
How can I make the "Hello, world!" text be shown inside the graphicsView.
You create your scene on stack, it is a problem, try to create it on heap (use pointers) in this case if there are not any mistakes, all should works fine. As doc said:
The view does not take ownership of scene.
http://qt-project.org/doc/qt-5/qgraphicsview.html#setScene
It means that when you create scene on stack, this scene will be deleted "in the end" of on_pbClick_clicked slot. So your scene does not exist anymore, and you can't see nothing.
QGraphicsScene *scene = new QGraphicsScene;
//adding some text to the scene
scene->addText("Hello, world!", QFont("Times", 20, QFont::Bold));
ui->graphicsView->setScene(scene);
ui->graphicsView->show();

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

Qt/C++ Not displaying QGraphicview as QWidget

My project consists of operations between geometric figures on a cartesian plane.I would include a graph that have to be updated after each operation.
Thats the source:
http://pastebin.com/s5Fu9dHJ
I've created the wrapper "disegna" (: public QWidget) because of I am sending to display everything as separate widgets (I have a widget for the virtual keyboard, another for Qlineedits, etc.) and I need an QWidget object can be used with view-> addWidget (QWidget,int,int) because I cannot pass directly a QMainWindow object.
The Program run with no errors,but no "hello world" is drawed (and no blank-space for istance QGraphicView is created).
where am I doing it wrong?
Change
QGraphicsView view(&scene);
view.show();
to
QGraphicsView * view = new QGraphicsView(&scene);
view->show();
The way you have it now, the instance of QGraphicView is allocated on the stack and gets destroyed right after the disegna constructor is executed, that's why you can't see it.
Don't forget to free the memory.

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?