Domanda

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?

È stato utile?

Soluzione

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top