0

There is a QList member variable named m_noteList containing QSharedPointer elements of class Note.

private: 
   QList< QSharedPointer<Note> > m_noteList; 

If a new note is created, its reference is appended to the list:

void Traymenu::newNote(){
    QSharedPointer<Note> note(new Note(this));
    m_noteList << note;
}

For each Note-element, whichs pointers are in m_noteList, I want to get its title and add it to my contextmenu. Purpose is to click on that title to open the note:

for ( int i = 0 ; i < m_noteList.count() ; i++ ) {
    std::string menuEntryName = m_noteList[i].getTitle();
    QAction *openNote = m_mainContextMenu.addAction(menuEntryName);
}

I get an error

C:\project\traymenu.cpp:43: Fehler: 'class QSharedPointer<Note>' has no member named 'getTitle'
     std::string menuEntryName = &m_noteList[i].getTitle();
                                                ^

Basically I want to get access to the objects that are referenced in the m_noteList. How do I do that? I thought with m_noteList[i] to get access to that element, but obviously the compiler expects something of type QSharedPointer. Why?

demonplus
  • 5,613
  • 12
  • 49
  • 68
user2366975
  • 4,350
  • 9
  • 47
  • 87
  • The error says that a `QSharedPointer` does not have the method `getTitle`. That's true. You should dereference your pointer to access members of the `Note` class : `m_noteList[i].data()->getTitle();` – Kiwi Feb 23 '14 at 13:13
  • Ah, so this is they way it works. Thank you, will you post an answer? – user2366975 Feb 23 '14 at 13:19

1 Answers1

1

QSharedPointer basically wraps your pointer. So you can't access directly with the '.' operator, this is the reason why you get this error : getTitle is not part of the class QSharedPointer.

However you have multiple ways to retrieve the actual pointer :

  1. data : not the simplest way but it's explicit, and it sometimes matters
  2. operator-> : so you can use your QSharedPointer like an actual pointer m_noteList[i]->getTitle();
  3. operator* : to do something like (*m_noteList[i]).getTitle();
Kiwi
  • 2,698
  • 16
  • 15