Important: Please read the Qt Code of Conduct -

[SOLVED] Creating custom QLineF

  • Hello, I need to create a custom QLineF, called MyQLineF. However, I've got some problems. Here's the code:

    #ifndef MYQLINEF_H
    #define MYQLINEF_H
    #include <QLineF>
    #include "node.h"
    #include <myqpointf.h>
    class MyQLineF : public QLineF
    	MyQLineF(QPointF p1, QPointF p2);
    	MyQLineF(node n1, node n2);
    	node n1();
    	node n2();
    	void setN1(node n);
    	void setN2(node n);
    	node src, dst;
    #endif // MYQLINEF_H


    #include "myqlinef.h"
    MyQLineF::MyQLineF(QPointF p1, QPointF p2)
      this->setLine(p1.x(), p1.y(), p2.x(), p2.y());
      node n1, n2;
      n1.x = p1.x();
      n2.x = p2.x();
      n1.y = p1.y();
      n2.y = p2.y();
    MyQLineF::MyQLineF(node n1, node n2)
      this->setLine(n1.x, n1.y, n2.x, n2.y);
    node MyQLineF::n1()
      return this->src;
    node MyQLineF::n2()
      return this->dst;
    void MyQLineF::setN1(node n)
      this->src = n;
    void MyQLineF::setN2(node n)
      this->dst = n;


    #ifndef NODE_H
    #define NODE_H
    typedef enum {START, TARGET} type;
    typedef struct node
      int x;
      int y;
      int id;
      type belongsTo;
    } node;
    #endif // NODE_H

    All goes well until I try to create a QVector<MyQLineF>.
    The following problems occur:

    In file included from /usr/include/qt/QtGui/qbrush.h:39:0,
                 from /usr/include/qt/QtGui/qpalette.h:39,
                 from /usr/include/qt/QtWidgets/qwidget.h:41,
                 from /usr/include/qt/QtWidgets/qmainwindow.h:37,
                 from /usr/include/qt/QtWidgets/QMainWindow:1,
                 from ../MyQPointF/mainwindow.h:4,
                 from ../MyQPointF/mainwindow.cpp:1:
    /usr/include/qt/QtCore/qvector.h: In instantiation of 'void QVector<T>::defaultConstruct(T*, T*) [with T = MyQLineF]':
    /usr/include/qt/QtCore/qvector.h:537:41:   required from 'void QVector<T>::reallocData(int, int, QArrayData::AllocationOptions) [with T = MyQLineF; QArrayData::AllocationOptions = QFlags<QArrayData::AllocationOption>]'
    /usr/include/qt/QtCore/qvector.h:607:20:   required from 'void QVector<T>::append(const T&) [with T = MyQLineF]'
    /usr/include/qt/QtCore/qvector.h:225:47:   required from 'void QVector<T>::push_back(const T&) [with T = MyQLineF]'
    ../MyQPointF/mainwindow.cpp:56:15:   required from here
    /usr/include/qt/QtCore/qvector.h:284:13: error: no matching function for call to 'MyQLineF::MyQLineF()'
             new (from++) T();
    In file included from ../MyQPointF/mainwindow.h:6:0,
                 from ../MyQPointF/mainwindow.cpp:1:
    ../MyQPointF/myqlinef.h:12:3: note: candidate: MyQLineF::MyQLineF(node, node)
       MyQLineF(node n1, node n2);
    ../MyQPointF/myqlinef.h:12:3: note:   candidate expects 2 arguments, 0 provided
    ../MyQPointF/myqlinef.h:11:3: note: candidate: MyQLineF::MyQLineF(QPointF, QPointF)
     MyQLineF(QPointF p1, QPointF p2);
    ../MyQPointF/myqlinef.h:11:3: note:   candidate expects 2 arguments, 0 provided
    ../MyQPointF/myqlinef.h:8:7: note: candidate: MyQLineF::MyQLineF(const MyQLineF&)
     class MyQLineF : public QLineF
    ../MyQPointF/myqlinef.h:8:7: note:   candidate expects 1 argument, 0 provided

    Why does this happen? Should I override each constructor in the QLineF class?
    Note: using std::vector instead of QVector solves this i ssue, but I'm curious about this problem.

  • Lifetime Qt Champion


    That's because your class doesn't have a default constructor. You can find more information on the The Container Classes chapter of Qt's documentation.

  • Thank you @SGaist . So, is it enough to add

    MyQLineF() {}

    as it is described in that page?

  • Lifetime Qt Champion

    It should be yes