PDA

View Full Version : Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp



mdavidjohnson
26th October 2016, 01:05
Could someone please point me to a very simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp that actually works and doesn't throw a "no matching function for call" error.

I've read the documentation for QGraphicsView, QGraphicsScene, and QGraphicsItem. I've tried various code published here and elsewhere which is supposed to work. But everything seems to throw that error.

For example, I start a new Qt widgets application in Qt Creator. I change nothing except main.cpp, which becomes:


#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main( int argc, char **argv )
{
QApplication app(argc, argv);

QGraphicsScene scene;
QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));

QGraphicsItem *item = scene.itemAt(50, 50);
// item == rect

QGraphicsView view(&scene);
view.show();

return app.exec();
}

At the line:


QGraphicsItem *item = scene.itemAt(50, 50);

I get "no matching function for call to 'QGraphicsScene::itemAt(int,int)'.

anda_skoa
26th October 2016, 12:33
The compiler gave you the reason it couldn't compile this.

There is no such method.

You are probably trying to call this one http://doc.qt.io/qt-5/qgraphicsscene.html#itemAt-3

If you look at the full error message, it even tells you why it couldn't select that one



/dvl/Qt5.7.0/5.7/gcc_64/include/QtWidgets/qgraphicsscene.h:179:27: note: candidate: QGraphicsItem* QGraphicsScene::itemAt(qreal, qreal, const QTransform&) const
inline QGraphicsItem *itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
^
/dvl/Qt5.7.0/5.7/gcc_64/include/QtWidgets/qgraphicsscene.h:179:27: note: candidate expects 3 arguments, 2 provided


Cheers,
_

mdavidjohnson
26th October 2016, 14:30
Thank you -

I'll look into that.

But, my copy of Qt 5.7 QtCreator 4.0.2 didn't give me all of that error message. All I got was:

"C:\work\Qt5Work\Qt570\LearningTests\QGraphicsViewF irstTest\main.cpp:14: error: no matching function for call to 'QGraphicsScene::itemAt(int, int)'
QGraphicsItem *item = scene.itemAt(50, 50);"

Is there some setting I need to adjust to get the more verbose error messages?

anda_skoa
26th October 2016, 15:18
Error messages depend on the compiler.
In my case that is g++ 5.3.1

Did you look at the compile output or just the issues tab?

Cheers,
_

mdavidjohnson
26th October 2016, 17:18
Oops!

Yes, I was only looking at the issues tab. The full compile output did have the additional info.

I'm using kit: Desktop Qt 5.7.0 MinGW 32bit, btw; Qt Creator 4.0.2.

But, I must be missing something else as well, because the code was provided on a forum (I don't remember which one) in response to a question similar to mine. The person making the request then reported back that the code worked fine. Perhaps it was a different version of Qt? I don't know. But I tried three our four different pieces of similarly provided code, all of which had also been reported as working by the requesters, and all of which failed with "no matching function for call" errors on my installation.

Would it be possible to point me to a piece of very simple QGraphicsView code which will work properly on my installation? I think if I can get past this first hurdle, I'll be able to add new features one-by-one, testing along the way.

Added after 27 minutes:

Interesting -

I added the transform required by QGraphicsItem like so:


#include "mainwindow.h"
#include <QApplication>
#include <QTransform>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main( int argc, char **argv )
{
QApplication app(argc, argv);

QTransform transform;
// Set to default identity matrix
transform.reset();

QGraphicsScene scene;
QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));

QGraphicsItem *item = scene.itemAt(50, 50, transform);
// item == rect

QGraphicsView view(&scene);
view.show();

return app.exec();
}

and it worked.

Of course, it complained that rect and item were unused variables, and all that was shown was a blank view, but at least it compiled and ran.

anda_skoa
26th October 2016, 17:29
The examples pacge lists several examples for that framework.
http://doc.qt.io/qt-5/examples-graphicsview.html

E.g. http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html

Cheers,
_

mdavidjohnson
26th October 2016, 18:33
Yes it does.

One might conjecture, though, that none of them qualify as "simple", let alone "very simple". :-)

Added after 12 minutes:

Of additional interest, I tried another one of the "answers" which had been posted to a similar question:


#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main( int argc, char **argv )
{
QApplication app(argc, argv);

QGraphicsScene scene;
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

QGraphicsItem *item = new QGraphicsEllipseItem( 0, &scene );
item->setRect( -50.0, -50.0, 100.0, 100.0 );

QGraphicsView view( &scene );
view.setRenderHints( QPainter::Antialiasing );
view.show();

return app.exec();
}

The compiler complained about the two lines:


QGraphicsItem *item = new QGraphicsEllipseItem( 0, &scene );
item->setRect( -50.0, -50.0, 100.0, 100.0 );

But, when I replaced them with:


QGraphicsItem *item = new QGraphicsEllipseItem( -50.0, -50.0, 100.0, 100.0, Q_NULLPTR );
scene.addItem(item);

it compiled, ran, and produced a very nice ellipse (circle) inside the QGraphicsView.

One further question though: The compile output ended with "exited normally". But, within the output, it had reported:


del release\moc_mainwindow.cpp
Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\moc_mainwindow.cpp
del ui_mainwindow.h
Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\ui_mainwindow.h
del release\main.o release\mainwindow.o release\moc_mainwindow.o
Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\main.o

One wonders why it reported that it couldn't find an item immediately after it had reported that it had just deleted that item ??

And, one final question: Since this last example, as corrected, satisfies my request for a very simple example, I'm going to post it here in its complete final form.

How do I then mark this thread as "SOLVED" so it might be helpful to others?

Added after 20 minutes:

SOLUTION

The following very simple example (as main.cpp) compiled, ran, and displayed a very nice ellipse (circle) in the QGraphicsView on my Qt 5.7.0 MinGW 32-bit QtCreator 4.0.2 installation running on Windows 7 Pro SP1:


#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main( int argc, char **argv )
{
QApplication app(argc, argv);

QGraphicsScene scene;
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

QGraphicsItem *item = new QGraphicsEllipseItem( -50.0, -50.0, 100.0, 100.0, Q_NULLPTR );
scene.addItem(item);

QGraphicsView view( &scene );
view.setRenderHints( QPainter::Antialiasing );
view.show();

return app.exec();
}

anda_skoa
26th October 2016, 19:45
One further question though: The compile output ended with "exited normally". But, within the output, it had reported:


del release\moc_mainwindow.cpp
Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\moc_mainwindow.cpp
del ui_mainwindow.h
Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\ui_mainwindow.h
del release\main.o release\mainwindow.o release\moc_mainwindow.o
Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\main.o

One wonders why it reported that it couldn't find an item immediately after it had reported that it had just deleted that item ??

That is the reaction to the delete attempt. I.e. the delete failed because the file was not there.
Which of course is no problem, after all the intent of the delete is that it should not be there afterwards.

Cheers,
_

mdavidjohnson
27th October 2016, 16:53
Thank you -

So, how do I mark this thread as "SOLVED" so that it might be more helpful to others?

anda_skoa
27th October 2016, 19:20
I think you can edit the title when editing the first posting of the thread, but anyone reading the thread will see the solution anyway.

Cheers,
_

mdavidjohnson
27th October 2016, 22:11
I see I can edit my post of this morning, but none of the others - I think it's been too long since they were originally posted. Yes, anyone reading the post will probably see the solution. But, sometimes, you do a search and get multiple hits. If one of the hits is marked "SOLVED" it'll save you some time.