Russian Qt Forum

Qt => Общие вопросы => Тема начата: lolbla2 от Февраль 25, 2013, 06:43



Название: Не могу засунуть свой тип в QList....
Отправлено: lolbla2 от Февраль 25, 2013, 06:43
Создал свой класс:

channel.h
Код
C++ (Qt)
class Channel
{
public:
   Channel(){}
   Channel(const Channel &other){}
   ~Channel(){}
   Channel(QString provider, QString title, QString thumbnail, QString category, int id, int catId);
 
   //getters
   QString getProvider() const {return m_provider;}
   QString getTitle() const {return m_title;}
   QString getThumb() const {return m_thumbnail;}
   QString getCategory() const {return m_category;}
   int getId() const {return m_id;}
   int getCategoryId() const {return m_categoryId;}
private:
   QString m_provider;
   QString m_title;
   QString m_category;
   QString m_thumbnail;
   int m_id;
   int m_categoryId;
};
 
Q_DECLARE_METATYPE(Channel);
Q_DECLARE_METATYPE(QList<Channel>);
 
 

channel.cpp

Код
C++ (Qt)
#include "channel.h"
#include <QDebug>
 
Channel::Channel(QString provider, QString title, QString thumbnail, QString category, int id, int catId)
{
   m_provider = provider;
   m_title = title;
   m_thumbnail = thumbnail;
   m_category = category;
   m_id = id;
   m_categoryId = catId;
}
 
почему при попытке засунуть его в QList<Channel>  в списке данные превращаются в мусор?

Засовываю вот так:

Код
C++ (Qt)
QList<Channel> channels;
   for(int i=0; i<ls.size(); i++)
   {
       Channel c("ThickTV",ls.at(i).toMap()["name"].toString(),"...","Все",i,0); // ls.at(i).toMap()["name"].toString() -  данное преобразование  возвращает   вполне нормальное название канала, проверял
       channels.append(c);
   }
   qWarning()<< "size " <<  channels.size();
 
for(int i=0; i<channels.size(); i++)
   {
       qWarning() << channels[i].getProvider() << channels[i].getTitle() << channels[i].getThumb() <<
                     channels[i].getCategory() << channels[i].getCategoryId() << channels[i].getId();
   }
 

Получаю вывод:

[09:42:22] Warning: size  156
[09:42:22] Warning: "" "" "" "" 147339080 147466176
[09:42:22] Warning: "" "" "" "" 147466080 147466208
[09:42:22] Warning: "" "" "" "" 147339080 147466176
[09:42:22] Warning: "" "" "" "" 147466080 147466208
[09:42:22] Warning: "" "" "" "" 147339080 147466176
[09:42:22] Warning: "" "" "" "" 147339080 147466208
[09:42:22] Warning: "" "" "" "" 147466256 147466176
[09:42:22] Warning: "" "" "" "" 147466256 147466208
[09:42:22] Warning: "" "" "" "" 147466256 147466176
[09:42:22] Warning: "" "" "" "" 147466256 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466720 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466176
[09:42:22] Warning: "" "" "" "" 147466464 147467472
[09:42:22] Warning: "" "" "" "" 147466464 147466208
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147339056
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147339056
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147339056
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147339056
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147339056
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147467464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 147466464 147466160
[09:42:22] Warning: "" "" "" "" 147466464 147468888
[09:42:22] Warning: "" "" "" "" 7143521 7208960
[09:42:22] Warning: "" "" "" "" 147466464 147465832
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440
[09:42:22] Warning: "" "" "" "" 147466464 147468408
[09:42:22] Warning: "" "" "" "" 147466464 147468440


Название: Re: Не могу засунуть свой тип в QList....
Отправлено: Vass от Февраль 25, 2013, 08:08
Пустой конструктор копирования. Надо написать корректный.


Название: Re: Не могу засунуть свой тип в QList....
Отправлено: GreatSnake от Февраль 25, 2013, 08:17
Пустой конструктор копирования. Надо написать корректный.
В данном случае удалить его вообще.


Название: Re: Не могу засунуть свой тип в QList....
Отправлено: lolbla2 от Февраль 25, 2013, 08:32
Пустой конструктор копирования. Надо написать корректный.

Да вот это уже выяснил, а что в нём по умолчанию? Не объявить его вообще я не могу ибо Q_DECLARE_METATYPE  будет бузить что нет его.


Название: Re: Не могу засунуть свой тип в QList....
Отправлено: GreatSnake от Февраль 25, 2013, 08:47
Код
C++ (Qt)
Q_DECLARE_METATYPE(Channel);
Q_DECLARE_METATYPE(QList<Channel>);
Ты засовываешь Channel и QList<Channel> в QVariant?


Название: Re: Не могу засунуть свой тип в QList....
Отправлено: lolbla2 от Февраль 25, 2013, 09:14
Код
C++ (Qt)
Q_DECLARE_METATYPE(Channel);
Q_DECLARE_METATYPE(QList<Channel>);
Ты засовываешь Channel и QList<Channel> в QVariant?

Пока нет, но может и понадобится засунуть в вариант. А вообще нужно для того чтобы использовать в сигналах слотах свой тип, сигнал channelsReceived(QList<Channels>). Так-то сигналы и слоты работают, но плювался ворнингом QMetaProperty::read: Unable to handle unregistered datatype 'QList<Channel>' for property 'QDeclarativeBoundSignalParameters::channels'. Сейчас чот пропал этот ворнинг, хотя вроде ничего такого не делал, а наоборот убрал эти Q_DECLARE_METATYPE


Название: Re: Не могу засунуть свой тип в QList....
Отправлено: Vass от Февраль 25, 2013, 13:50
Да вот это уже выяснил, а что в нём по умолчанию? Не объявить его вообще я не могу ибо Q_DECLARE_METATYPE  будет бузить что нет его.

По умолчанию в нем поверхностное копирование всех полей, в конкретно вашем случае просто
приравняйте всем членам класса их же аналоги из объекта other