Reputation: 31
I am new to Qt and when I tried to compile and run a Qt program from "Foundations of Qt Development " Chapter 7, see
http://www.java2s.com/Code/Cpp/Qt/QGraphicsViewQGraphicsItemandQGraphicsScene.htm
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGLWidget>
QGraphicsItem *createItem( int x, QGraphicsScene *scene )
{
QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( x+40, 40, 120, 120 ), 0, scene );
rectItem->setPen( QPen(Qt::black) );
rectItem->setBrush( Qt::gray );
QGraphicsRectItem *innerRectItem = new QGraphicsRectItem( QRect( x+50, 50, 45, 100 ), rectItem, scene );
innerRectItem->setPen( QPen(Qt::black) );
innerRectItem->setBrush( Qt::white );
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( x+105, 50, 45, 100 ), rectItem, scene );
ellipseItem->setPen( QPen(Qt::black) );
ellipseItem->setBrush( Qt::white );
return rectItem;
}
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QGraphicsScene scene( QRect( 0, 00, 1000, 200 ) );
QGraphicsItem *item1 = createItem( 0, &scene );
QGraphicsItem *item2 = createItem( 200, &scene );
item2->translate( 300, 100 );
item2->rotate( 30 );
item2->translate( -300, -100 );
QGraphicsItem *item3 = createItem( 400, &scene );
item3->translate( 500, 100 );
item3->scale( 0.5, 0.7 );
item3->translate( -500, -100 );
QGraphicsItem *item4 = createItem( 600, &scene );
item4->translate( 700, 100 );
item4->shear( 0.1, 0.3 );
item4->translate( -700, -100 );
QGraphicsItem *item5 = createItem( 800, &scene );
item5->translate( 900, 100 );
item5->scale( 0.5, 0.7 );
item5->rotate( 30 );
item5->shear( 0.1, 0.3 );
item5->translate( -900, -100 );
QGraphicsView view;
view.setScene( &scene );
view.setViewport( new QGLWidget() );
view.show();
return app.exec();
}
I always got the error info " error: C2661: “QGraphicsRectItem::QGraphicsRectItem”: "No overloaded function takes 3 arguments".I tried again and again but all the same.Could somebody help me solving this problem? Thanks.
I am using Qt5.11.0 and MSVC2017 and Windows 10 pro X64.
Upvotes: 0
Views: 953
Reputation: 882028
It means exactly what it says, that you are trying to call the constructor of QGraphicsRectItem
with three arguments:
... = new QGraphicsRectItem(QRect(x+40, 40, 120, 120), 0, scene);
\______________________/ | \___/
1 2 3
If you look at the documentation, you'll see that no such constructor exists:
QGraphicsRectItem(QGraphicsItem *parent = nullptr);
QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr);
The first has one optional argument (so zero or one), the second has one mandatory and one optional (so one or two) and the third has four mandatory and one optional (so four or five).
If you examine that previous paragraph closely, you'll notice that one thing missing is the word "three" :-) I'd suggest ditching that tutorial since it's very old. Qt 4.2 (when that class was first introduced) did have a three-argument version which included the scene, but that was very short-lived and removed in 4.3.
For 5.11, further reading of the linked documentation shows up the fact that the (my emphasis):
QGraphicsRectItem
class provides a rectangle item that you can add to aQGraphicsScene
.
Hence the correct way to do what you appear to need is:
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRectF(x+40, 40, 120, 120));
scene->addItem(rectItem);
Upvotes: 1
Reputation: 244132
The code you sample is inconsistent with Qt5, an updated translation is as follows:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QOpenGLWidget>
static QGraphicsItem *createItem( int x, QGraphicsScene *scene )
{
QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRectF( x+40, 40, 120, 120 ));
scene->addItem(rectItem);
rectItem->setPen(QPen(Qt::black));
rectItem->setBrush( Qt::gray );
QGraphicsRectItem *innerRectItem = new QGraphicsRectItem( QRect( x+50, 50, 45, 100 ), rectItem);
innerRectItem->setPen( QPen(Qt::black) );
innerRectItem->setBrush( Qt::white );
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem( QRect( x+105, 50, 45, 100 ), rectItem);
ellipseItem->setPen( QPen(Qt::black) );
ellipseItem->setBrush( Qt::white );
return rectItem;
}
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QGraphicsScene scene( QRect( 0, 00, 1000, 200 ) );
QGraphicsItem *item1 = createItem( 0, &scene );
QGraphicsItem *item2 = createItem( 200, &scene );
QTransform tr2;
tr2.translate( 300, 100 );
tr2.rotate( 30 );
tr2.translate( -300, -100 );
item2->setTransform(tr2);
QGraphicsItem *item3 = createItem( 400, &scene );
QTransform tr3;
tr3.translate( 500, 100 );
tr3.scale( 0.5, 0.7 );
tr3.translate( -500, -100 );
item3->setTransform(tr3);
QGraphicsItem *item4 = createItem( 600, &scene );
QTransform tr4;
tr4.translate( 700, 100 );
tr4.shear( 0.1, 0.3 );
tr4.translate( -700, -100 );
item4->setTransform(tr4);
QGraphicsItem *item5 = createItem( 800, &scene );
QTransform tr5;
tr5.translate( 900, 100 );
tr5.scale( 0.5, 0.7 );
tr5.rotate( 30 );
tr5.shear( 0.1, 0.3 );
tr5.translate( -900, -100 );
item5->setTransform(tr4);
QGraphicsView view;
view.setScene( &scene );
view.setViewport( new QOpenGLWidget() );
view.show();
return app.exec();
}
*.pro
QT += core gui widgets opengl
TEMPLATE = app
CONFIG += c++11
SOURCES += main.cpp
Upvotes: 1