有一个QList成员变量名为m_noteList,它包含类Note的QSharedPointer元素。
private:
QList< QSharedPointer<Note> > m_noteList; 如果创建了新注释,则将其引用追加到列表中:
void Traymenu::newNote(){
QSharedPointer<Note> note(new Note(this));
m_noteList << note;
}对于在m_noteList中有指针的每个Note,我希望得到它的标题并将其添加到我的contextmenu中。目的是点击该标题打开便笺:
for ( int i = 0 ; i < m_noteList.count() ; i++ ) {
std::string menuEntryName = m_noteList[i].getTitle();
QAction *openNote = m_mainContextMenu.addAction(menuEntryName);
}我犯了个错误
C:\project\traymenu.cpp:43: Fehler: 'class QSharedPointer<Note>' has no member named 'getTitle'
std::string menuEntryName = &m_noteList[i].getTitle();
^基本上,我希望访问m_noteList中引用的对象。我该怎么做?我认为使用m_noteList[i]可以访问该元素,但显然编译器需要某种类型的QSharedPointer。为什么?
发布于 2014-02-23 13:26:37
QSharedPointer基本上封装了您的指针。因此,您不能直接使用“.”访问。运算符,这就是导致此错误的原因:getTitle不是类QSharedPointer的一部分。
但是,有多种方法检索实际指针:
data:不是最简单的方式,但它是明确的,有时很重要operator->:所以您可以像实际指针m_noteList[i]->getTitle();一样使用您的QSharedPointeroperator*:做一些像(*m_noteList[i]).getTitle();这样的事情https://stackoverflow.com/questions/21968575
复制相似问题