Results 1 to 11 of 11

Thread: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp

  1. #1
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QGraphicsItem>
    4. #include <QGraphicsScene>
    5. #include <QGraphicsView>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
    12.  
    13. QGraphicsItem *item = scene.itemAt(50, 50);
    14. // item == rect
    15.  
    16. QGraphicsView view(&scene);
    17. view.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    At the line:

    Qt Code:
    1. QGraphicsItem *item = scene.itemAt(50, 50);
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

    /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,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QTransform>
    4. #include <QGraphicsItem>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsView>
    7.  
    8. int main( int argc, char **argv )
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QTransform transform;
    13. // Set to default identity matrix
    14. transform.reset();
    15.  
    16. QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
    17.  
    18. QGraphicsItem *item = scene.itemAt(50, 50, transform);
    19. // item == rect
    20.  
    21. QGraphicsView view(&scene);
    22. view.show();
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by mdavidjohnson; 26th October 2016 at 16:18.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp

    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-grap...e-example.html

    Cheers,
    _

  7. #7
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QGraphicsEllipseItem>
    4. #include <QGraphicsScene>
    5. #include <QGraphicsView>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    12.  
    13. QGraphicsItem *item = new QGraphicsEllipseItem( 0, &scene );
    14. item->setRect( -50.0, -50.0, 100.0, 100.0 );
    15.  
    16. QGraphicsView view( &scene );
    17. view.setRenderHints( QPainter::Antialiasing );
    18. view.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    The compiler complained about the two lines:

    Qt Code:
    1. QGraphicsItem *item = new QGraphicsEllipseItem( 0, &scene );
    2. item->setRect( -50.0, -50.0, 100.0, 100.0 );
    To copy to clipboard, switch view to plain text mode 

    But, when I replaced them with:

    Qt Code:
    1. QGraphicsItem *item = new QGraphicsEllipseItem( -50.0, -50.0, 100.0, 100.0, Q_NULLPTR );
    2. scene.addItem(item);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. del release\moc_mainwindow.cpp
    2. Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\moc_mainwindow.cpp
    3. del ui_mainwindow.h
    4. Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\ui_mainwindow.h
    5. del release\main.o release\mainwindow.o release\moc_mainwindow.o
    6. Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\main.o
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QGraphicsEllipseItem>
    4. #include <QGraphicsScene>
    5. #include <QGraphicsView>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    12.  
    13. QGraphicsItem *item = new QGraphicsEllipseItem( -50.0, -50.0, 100.0, 100.0, Q_NULLPTR );
    14. scene.addItem(item);
    15.  
    16. QGraphicsView view( &scene );
    17. view.setRenderHints( QPainter::Antialiasing );
    18. view.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mdavidjohnson; 26th October 2016 at 17:38.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Very Simple QGraphicsView Example for QT5.7 QtCreator mainwindow's main.cpp

    Quote Originally Posted by mdavidjohnson View Post
    One further question though: The compile output ended with "exited normally". But, within the output, it had reported:

    Qt Code:
    1. del release\moc_mainwindow.cpp
    2. Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\moc_mainwindow.cpp
    3. del ui_mainwindow.h
    4. Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\ui_mainwindow.h
    5. del release\main.o release\mainwindow.o release\moc_mainwindow.o
    6. Could Not Find C:\work\Qt5Work\Qt570\LearningTests\build-QGraphicsViewFirstTest-Desktop_Qt_5_7_0_MinGW_32bit-Debug\release\main.o
    To copy to clipboard, switch view to plain text mode 

    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,
    _

  9. #9
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  11. #11
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

Similar Threads

  1. Replies: 1
    Last Post: 29th February 2016, 14:19
  2. Problem with variable in main.cpp and mainwindow.cpp
    By adis1001 in forum Qt Programming
    Replies: 13
    Last Post: 14th July 2015, 10:03
  3. Refresh the mainwindow / functions called in the main loop
    By valerianst in forum Qt Programming
    Replies: 7
    Last Post: 2nd October 2013, 14:57
  4. Replies: 1
    Last Post: 29th April 2013, 07:56
  5. QGraphicsView + MainWindow
    By lioncub in forum Newbie
    Replies: 3
    Last Post: 26th February 2011, 10:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.