Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Creating custom QLineF

[SOLVED] Creating custom QLineF

Scheduled Pinned Locked Moved General and Desktop
subclassingqlinefqline
4 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alogim
    wrote on 22 Sept 2015, 14:06 last edited by alogim
    #1

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

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

    MyQLineF.cpp

    #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();
      setN1(n1);
      setN2(n2);
    }
    
    MyQLineF::MyQLineF(node n1, node n2)
    {
      this->setLine(n1.x, n1.y, n2.x, n2.y);
      setN1(n1);
      setN2(n2);
    }
    
    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;
    }
    

    node.h:

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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Sept 2015, 15:09 last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alogim
        wrote on 22 Sept 2015, 15:22 last edited by alogim
        #3

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

        MyQLineF() {}
        

        as it is described in that page?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Sept 2015, 19:14 last edited by
          #4

          It should be yes

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          2/4

          22 Sept 2015, 15:09

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved