Results 1 to 4 of 4

Thread: Implementing an "open recently opened files" menu?

  1. #1
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Implementing an "open recently opened files" menu?

    Hi all,

    I'm trying to implement an "open recently opened files" menu. For now I have successfully implemented the functions that write and retrieve the list of frequently needed files on the disk. I have also a piece of code that creates the menu entries dynamically.

    My problem is that I need to connect the menu qActions to the function that will actually load the files, something like openFiles(QString& filename). How can you do the connection? I don't think something like

    Qt Code:
    1. connect(my_action,SIGNAL(triggered(),this,SLOT(openFiles(myfile)));
    To copy to clipboard, switch view to plain text mode 

    is legal . Any insights?

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Implementing an "open recently opened files" menu?

    Checking out the Recent Files Example might be helpful.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Implementing an "open recently opened files" menu?

    Part of implementation in Qt Creator:
    Qt Code:
    1. void MainWindow::openRecentFile()
    2. {
    3. QAction *a = qobject_cast<QAction*>(sender());
    4. if (m_recentFilesActions.contains(a)) {
    5. editorManager()->openEditor(m_recentFilesActions.value(a));
    6. editorManager()->ensureEditorManagerVisible();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    So it seems that one way is to set file path as a text for each action and connect all actions to one slot:
    Qt Code:
    1. connect(my_action,SIGNAL(triggered(),this,SLOT(openFile()));
    To copy to clipboard, switch view to plain text mode 
    And then, in openFile(), with a QObject::sender() you can get sender, it means the action that triggered that slot.
    Qt Code:
    1. QAction *a = qobject_cast<QAction*>(sender());
    To copy to clipboard, switch view to plain text mode 
    and now:
    Qt Code:
    1. a->text();
    To copy to clipboard, switch view to plain text mode 
    gives you QString with a file path to open.

    So you can use your previosly created slot:
    Qt Code:
    1. openFiles(a->text());
    To copy to clipboard, switch view to plain text mode 
    Of course it would be nice to also add some error checking.

    P.S. Notice that in the example given by my preposter the QSettings class is used instead of saving recent files to some file.
    Last edited by faldzip; 5th March 2009 at 23:32.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Implementing an "open recently opened files" menu?

    Thanks a lot to both of you for your very useful answers!

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.