Hello! I write the code that shows a part of file system as the menu. Subfolders are the submenus, etc. All works fine, but now I need to write a slot for the "filename" menu item selection.
In that slot, I need to determine a sequence of parent titles to make a path for the file.
For example, my menu structure is: Foo - Bar - file.txt
When I click on the file.txt menu item, I need to iterate through the parents to get their titles.
So my code is:
Qt Code:
  1. void rvln::test_slot()
  2. {
  3. QAction *a = qobject_cast<QAction *>(sender());
  4. QString path;
  5. path.prepend (a->text());
  6.  
  7. QMenu *m_parent = qobject_cast<QMenu *> (a->parentWidget());
  8.  
  9. while (m_parent)
  10. {
  11. path.prepend("/").prepend (m_parent->title());
  12. m_parent = qobject_cast<QMenu *> (m_parent->parentWidget());
  13. }
  14. qDebug() << path;
  15. }
To copy to clipboard, switch view to plain text mode 
This code have a strange behavior - the nearest (previous) upper level "directory" is dropped out. So I have not Foo/Bar/file.txt, but Foo/file.txt instead.