roxton
17th January 2009, 11:44
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:
void rvln::test_slot()
{
QAction *a = qobject_cast<QAction *>(sender());
QString path;
path.prepend (a->text());
QMenu *m_parent = qobject_cast<QMenu *> (a->parentWidget());
while (m_parent)
{
path.prepend("/").prepend (m_parent->title());
m_parent = qobject_cast<QMenu *> (m_parent->parentWidget());
}
qDebug() << path;
}
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.
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:
void rvln::test_slot()
{
QAction *a = qobject_cast<QAction *>(sender());
QString path;
path.prepend (a->text());
QMenu *m_parent = qobject_cast<QMenu *> (a->parentWidget());
while (m_parent)
{
path.prepend("/").prepend (m_parent->title());
m_parent = qobject_cast<QMenu *> (m_parent->parentWidget());
}
qDebug() << path;
}
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.