Results 1 to 5 of 5

Thread: creating a "path" for the menu item

  1. #1
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default creating a "path" for the menu item

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: creating a "path" for the menu item

    You could use QAction::setData() to store the full path in the action. Then you wouldn't have to iterate anything...
    J-P Nurmi

  3. #3
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating a "path" for the menu item

    Thank you! I'll use it. But why the iteration through the parents works in a such strange way?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: creating a "path" for the menu item

    Quote Originally Posted by roxton View Post
    But why the iteration through the parents works in a such strange way?
    Hmm, good question. According to my test case it works fine:
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QMenu menu("Foo");
    9. QMenu* subMenu = menu.addMenu("Bar");
    10. QAction* action = subMenu->addAction("File");
    11.  
    12. QString path = action->text();
    13. QMenu *m_parent = qobject_cast<QMenu *> (action->parentWidget());
    14. while (m_parent)
    15. {
    16. path.prepend("/").prepend (m_parent->title());
    17. m_parent = qobject_cast<QMenu *> (m_parent->parentWidget());
    18. }
    19. qDebug() << path; // outputs "Foo/Bar/File" for me
    20. }
    To copy to clipboard, switch view to plain text mode 
    Perhaps there's something wrong with the menu hierarchy construction?
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: creating a "path" for the menu item

    I don't know how the objects are constructed in this particular case, but in general the menu object is not (or at least doesn't have to be) the parent of the QAction object, so there is a possibility items in the deepest menu in the hierarchy are owned by its parent.

Similar Threads

  1. Menu Item has no highlight
    By blackfox in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2008, 12:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.