【问题标题】:C++ Qt5.11 error :" C2661: No overloaded function takes 3 arguments"C++ Qt5.11 错误:“C2661:没有重载函数需要 3 个参数”
【发布时间】:2018-12-30 02:54:38
【问题描述】:

我是 Qt 新手,当我尝试编译和运行“Qt 开发基础”第 7 章中的 Qt 程序时,请参阅

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

我总是收到错误信息“错误:C2661:“QGraphicsRectItem::QGraphicsRectItem”:“没有重载函数需要 3 个参数”。我一次又一次地尝试,但都一样。有人可以帮我解决这个问题吗?谢谢。

我正在使用 Qt5.11.0 和 MSVC2017 以及 Windows 10 pro X64。

【问题讨论】:

  • 你应该把代码带到这里,然后专门问代码的问题。
  • 以后,当提到编译器错误时——你应该在你所附的例子中指出它们所指的行。 IE。添加注释或指向错误所在的箭头。

标签: c++ qt qt5 qgraphicsitem qgraphicsrectitem


【解决方案1】:

这正是它所说的,你试图用三个参数调用QGraphicsRectItem的构造函数:

... = new QGraphicsRectItem(QRect(x+40, 40, 120, 120), 0, scene);
                            \______________________/   |  \___/
                                        1              2    3

如果你查看documentation,你会发现不存在这样的构造函数:

QGraphicsRectItem(QGraphicsItem *parent = nullptr);
QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr);

第一个有一个可选参数(所以为零或一个),第二个有一个强制参数和一个可选参数(所以一或两个),第三个有四个强制参数和一个可选参数(所以四个或五个)。

如果您仔细检查上一段,您会注意到缺少“三”这个词 :-) 我建议放弃该教程,因为它已经很老了。 Qt 4.2(首次引入该类时)确实有一个包含场景的three-argument version,但这是非常短暂的并在4.3中删除。

对于 5.11,进一步阅读链接文档表明(我的重点):

QGraphicsRectItem 类提供了一个矩形项,您可以将其添加到QGraphicsScene

因此正确的方法来做你似乎需要的事情是:

QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRectF(x+40, 40, 120, 120));
scene->addItem(rectItem);

【讨论】:

    【解决方案2】:

    你采样的代码与Qt5不一致,更新翻译如下:

    #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
    

    【讨论】:

    • 在编写.pro 文件时,了解依赖关系是可传递的,并且不需要添加到QT 是有帮助的:您可以完全控制所需的模块。所以只需设置它:QT = widgets opengl。自动包含coregui 模块。对于 Qt 5.7 及更高版本,CONFIG += c++11 是不必要的:Qt 5.7 需要 C++11 支持,没有它代码将无法编译。 TEMPLATE = app 是默认设置,因此您也可以跳过它。你只需要两行:QT=SOURCES= :) 谢天谢地,现代 cmake 也没有差多少——它只需要 5 行 IIRC。
    • @KubaOber 我明白你告诉我的,但即使是 Qt Creator 也会产生更多不必要的元素,我已经消除了,我认为多放一点也不会伤害 :),如果我的 .pro 我会这样做是广泛的,但它不是,我会考虑到我的下一个出版物,谢谢。 :)
    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 2021-07-05
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多