Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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:
Code:
#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsView>
int main( int argc, char **argv )
{
// item == rect
view.show();
return app.exec();
}
At the line:
I get "no matching function for call to 'QGraphicsScene::itemAt(int,int)'.
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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
Quote:
/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,
_
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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\QGraphicsView FirstTest\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?
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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,
_
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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:
Code:
#include "mainwindow.h"
#include <QApplication>
#include <QTransform>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsView>
int main( int argc, char **argv )
{
QTransform transform;
// Set to default identity matrix
transform.reset();
// item == rect
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.
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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:
Code:
#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>
int main( int argc, char **argv )
{
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
item->setRect( -50.0, -50.0, 100.0, 100.0 );
view.
setRenderHints( QPainter::Antialiasing );
view.show();
return app.exec();
}
The compiler complained about the two lines:
Code:
item->setRect( -50.0, -50.0, 100.0, 100.0 );
But, when I replaced them with:
Code:
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:
Code:
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:
Code:
#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>
int main( int argc, char **argv )
{
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
scene.addItem(item);
view.
setRenderHints( QPainter::Antialiasing );
view.show();
return app.exec();
}
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
Quote:
Originally Posted by
mdavidjohnson
One further question though: The compile output ended with "exited normally". But, within the output, it had reported:
Code:
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,
_
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
Thank you -
So, how do I mark this thread as "SOLVED" so that it might be more helpful to others?
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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,
_
Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp
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.