Создание подкласса QList настраиваемых объектов

У меня есть класс Point, определенный следующим образом:

class Point
{
public:
    inline Point() { m_x=0; m_y=0;}

protected:
    int m_x;
    int m_y;
};
Q_DECLARE_METATYPE(Point)

Затем я хотел бы определить пользовательский QList из Point объектов, поскольку я хочу расширить базовые методы с помощью некоторых методов, связанных с Point, например hasX(int x) ниже:

class PointList : public QList<Point>
{
public:
    inline PointList() {;}
    inline PointList(const QList<Point>& points) : QList<PointList>(points){ ; }

    bool hasX(int x) const {
        for (const Point& p: *this)
            if (p.x() == x)
                return true;
        return false;
    }
};

Если я скомпилирую приведенный выше код, я получу следующие ошибки:

1>C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(111): error C2665: 'qHash' : none of the 25 overloads could convert all the argument types
1>          c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(372): could be 'uint qHash(const QTime &,uint) throw()'
1>          c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(371): or       'uint qHash(const QDate &,uint) throw()'
1>          c:\qt\qt5.7.0\5.7\msvc2013\include\qtcore\qdatetime.h(370): or       'uint qHash(const QDateTime &,uint)'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qlocale.h(62): or       'uint qHash(const QLocale &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qregexp.h(56): or       'uint qHash(const QRegExp &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(101): or       'uint qHash(QLatin1String,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(100): or       'uint qHash(const QBitArray &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(99): or       'uint qHash(const QStringRef &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(98): or       'uint qHash(const QString &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(97): or       'uint qHash(const QByteArray &,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(96): or       'uint qHash(const QChar,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(94): or       'uint qHash(long double,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(92): or       'uint qHash(double,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(91): or       'uint qHash(float,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(90): or       'uint qHash(qint64,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(86): or       'uint qHash(quint64,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(85): or       'uint qHash(long,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(79): or       'uint qHash(ulong,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(78): or       'uint qHash(int,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(77): or       'uint qHash(uint,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(76): or       'uint qHash(short,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(75): or       'uint qHash(ushort,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(74): or       'uint qHash(signed char,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(73): or       'uint qHash(uchar,uint) throw()'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhashfunctions.h(72): or       'uint qHash(char,uint) throw()'
1>          while trying to match the argument list '(const Point)'
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(920) : see reference to function template instantiation 'uint qHash<Key>(const T &,uint)' being compiled
1>          with
1>          [
1>              Key=Point
1>  ,            T=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(916) : while compiling class template member function 'QHashNode<Key,T> **QHash<Key,T>::findNode(const Key &,uint *) const'
1>          with
1>          [
1>              Key=Point
1>  ,            T=QHashDummyValue
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(760) : see reference to function template instantiation 'QHashNode<Key,T> **QHash<Key,T>::findNode(const Key &,uint *) const' being compiled
1>          with
1>          [
1>              Key=Point
1>  ,            T=QHashDummyValue
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qhash.h(756) : while compiling class template member function 'QHash<T,QHashDummyValue>::iterator QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)'
1>          with
1>          [
1>              T=Point
1>  ,            Key=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(205) : see reference to function template instantiation 'QHash<T,QHashDummyValue>::iterator QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)' being compiled
1>          with
1>          [
1>              T=Point
1>  ,            Key=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(251) : see reference to class template instantiation 'QHash<T,QHashDummyValue>' being compiled
1>          with
1>          [
1>              T=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(381) : see reference to class template instantiation 'QSet<T>' being compiled
1>          with
1>          [
1>              T=Point
1>          ]
1>          C:\Qt\Qt5.7.0\5.7\msvc2013\include\QtCore/qset.h(381) : while compiling class template member function 'QSet<T> QList<T>::toSet(void) const'
1>          with
1>          [
1>              T=Point
1>          ]
1>          c:\projects\points\point.h(139) : see reference to class template instantiation 'QList<Point>' being compiled

Что мне не хватает?


person ABCplus    schedule 05.12.2016    source источник
comment
Думаю, проблема во втором конструкторе. Строка должна быть inline PointList(const QList<Point>& points) : QList<Point>(points){ ; }. Класс не наследуется от QList ‹PointList›, но наследует QList ‹Point›. Но я не знаю, что находится в строке c: \ projects \ points \ point.h (139)   -  person Jirka Picek    schedule 05.12.2016
comment
Строка 139: class PointList: public QList ‹Point›   -  person ABCplus    schedule 05.12.2016
comment
@JirkaPicek: я получаю сообщение об ошибке, даже если я прокомментирую этот конструктор   -  person ABCplus    schedule 05.12.2016


Ответы (2)


У вас могут быть другие проблемы. Следующие компиляции и ссылки мне подходят:

#include <QtCore>

class Point
{
public:
   // all default constructors are OK
   int x() const { return m_x; }
protected:
   int m_x = 0;
};
Q_DECLARE_METATYPE(Point)

class PointList : public QList<Point>
{
public:
   using QList::QList;
   inline PointList() = default;
   inline PointList(const QList<Point>& points) : QList<Point>(points) {}

   bool hasX(int x) const {
      for (auto & p: *this)
         if (p.x() == x)
            return true;
      return false;
   }
};

int main() {
   PointList p0;
   QList<Point> p1;
   PointList p2((QList<Point>{Point()}));
   PointList p3{Point()};
   PointList p4(p1);
   PointList p5(p2);
}

Обратите внимание, что hasX также может быть реализовано как:

return std::find_if(begin(), end(), [x](const Point &p){ return p.x() == x; }) != end();

К сожалению, каждый компилятор, доступный в настоящее время через godbolt, действительно не может оптимизировать этот код под -O3 :(

person Kuba hasn't forgotten Monica    schedule 05.12.2016
comment
Спасибо, Куба. Я пропустил важную ключевую работу в моем опубликованном коде (извините, я не думал, что это было источником проблемы). Это ключевое слово Q_DECL_EXPORT, поскольку я хочу экспортировать классы Point и PointList. Итак, если вы добавите определение QTCLASSLIBRARY_EXPORT Q_DECL_EXPORT в заголовок и измените код на class QTCLASSLIBRARY_EXPORT Point { и class QTCLASSLIBRARY_EXPORT PointList {, код не будет компилироваться. - person ABCplus; 06.12.2016
comment
Возможно, мне следует отредактировать свой вопрос, но это сделает ваш ответ недействительным, и это неверно. - person ABCplus; 06.12.2016

Согласно результатам вашей компиляции, для вашего класса Point отсутствует пользовательская функция qHash. Вы можете найти документацию Qt, включая пример для этого здесь.

person tomvodi    schedule 05.12.2016
comment
Я не использую qHash, я считаю, что это что-то в реализации QList - person ABCplus; 05.12.2016