PDA

View Full Version : Implementing an "open recently opened files" menu?



joelthelion
5th March 2009, 19:24
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


connect(my_action,SIGNAL(triggered(),this,SLOT(ope nFiles(myfile)));

is legal :( . Any insights?

spud
6th March 2009, 00:17
Checking out the Recent Files Example (http://doc.trolltech.com/snapshot/mainwindows-recentfiles.html) might be helpful.;)

faldzip
6th March 2009, 00:26
Part of implementation in Qt Creator:


void MainWindow::openRecentFile()
{
QAction *a = qobject_cast<QAction*>(sender());
if (m_recentFilesActions.contains(a)) {
editorManager()->openEditor(m_recentFilesActions.value(a));
editorManager()->ensureEditorManagerVisible();
}
}


So it seems that one way is to set file path as a text for each action and connect all actions to one slot:


connect(my_action,SIGNAL(triggered(),this,SLOT(ope nFile()));

And then, in openFile(), with a QObject::sender() you can get sender, it means the action that triggered that slot.


QAction *a = qobject_cast<QAction*>(sender());

and now:


a->text();

gives you QString with a file path to open.

So you can use your previosly created slot:


openFiles(a->text());

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.

joelthelion
6th March 2009, 11:49
Thanks a lot to both of you for your very useful answers!