Farzan Najipour
Farzan Najipour

Reputation: 2503

Qt- Appending to QList

I created QList from class in global.

global.h:

class CLastMessage
{
public:
    QString id;
    QString message;
};
typedef QList<CLastMessage> CLastMessageList;

I called it in another header:

message.h:

CLastMessageList m_lastMessage;

but I'm having problem to append new values to this Qlist. in message.cpp I want to append new id and messages, but I don't know how to do that in the best way. for example I want to add new id to this list. is it correct?

message.cpp:

CLastMessageList m_lastMessage;
m_lastMessage.id = "1";

Upvotes: 1

Views: 1009

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27611

Your custom class is currently assignable, so you don't need an explicit copy constructor or assignment operator.

The problem is how you add an item to the list:

CLastMessageList m_lastMessage; // this is declaring a new list
m_lastMessage.id = "1"; // this is invalid, as id is not a property of QList

This code re-declares an object of the list (m_lastMessage). You're then trying to set a variable to the list, but id is not a member of the list, but a member of an object which you can store in the list. So, create an object first, then add it to your list:

CLastMessage message;
message.id = "1";
m_lastMessage.append(message); 

Upvotes: 4

Related Questions