Дмитрий
Aug. 7, 2017, 12:56 a.m.

Using JumpList to store files opened in the application when using Qt

JumpList, one of the features that are present only in Windows (version 7 and above). In this sheet, in particular, you can store links to the most recently opened files in the application.


Step-by-step instruction

To use this function, you need to

Connect the module

  1. QT += winextras

Connect library

  1. #include <QtWinExtras>

Make the appropriate JumpList section active

  1. QWinJumpList jumplist;
  2. jumplist.recent()->setVisible(true);

Associate all the required file extensions with our application using the function

  1. static void associateFileTypes(const QStringList &fileTypes)
  2. {
  3. QString displayName = QGuiApplication::applicationDisplayName();
  4. QString filePath = QCoreApplication::applicationFilePath();
  5. QString fileName = QFileInfo(filePath).fileName();
  6. QSettings settings("HKEY_CURRENT_USER\\Software\\Classes\\Applications\\" + fileName, QSettings::NativeFormat);
  7. settings.setValue("FriendlyAppName", displayName);
  8. settings.beginGroup("SupportedTypes");
  9. foreach (const QString& fileType, fileTypes)
  10. settings.setValue(fileType, QString());
  11. settings.endGroup();
  12. settings.beginGroup("shell");
  13. settings.beginGroup("open");
  14. settings.setValue("FriendlyAppName", displayName);
  15. settings.beginGroup("Command");
  16. settings.setValue(".", QChar('"') + QDir::toNativeSeparators(filePath) + QString("\" \"%1\""));
  17. }

Which is convenient to use in the main

  1. QApplication a(argc, argv);
  2. a.setApplicationDisplayName("QtWinExtras Test");
  3. associateFileTypes(QStringList(".txt"));

Now when you open a new file, a link to it will be added to the last JumpList section.

When clicking on one of the links that appears, our application will start with the file name as the second parameter (argv [1]). With such a configuration, it is reasonable to modify the main function so that the selected file is opened.

  1. if(argc == 2)
  2. w.open(QString::fromLocal8Bit(argv[1]).replace(QString("\\"),QString("/")));

It is also possible to implement when, when the application is started, it is checked if there is a copy of it running. If such a copy is found, the application will send the name of the selected file to it and finish its work. I wrote the following implementation, where the name of the file is written to the registry.

  1. HANDLE hCorvetEvent = CreateEventA(NULL, FALSE, FALSE, ("winExJumpList"));
  2. if (GetLastError() == ERROR_ALREADY_EXISTS)
  3. {
  4. if(argc == 2)
  5. {
  6. QSettings *settings;
  7. settings = new QSettings("Company", "my programm");
  8. settings->setValue("AddFile", QString::fromLocal8Bit(argv[1]).replace(QString("\\"),QString("/") ) );
  9. CloseHandle(hCorvetEvent);
  10. }
  11. return 0;
  12. }

The program uses a timer-based function to monitor the registry change and open the added file.

  1. connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::openFile);
  2. QTimer *openFile = new QTimer;
  3. connect(openFile, &QTimer::timeout, this, &MainWindow::nextFile);
  4. openFile->start(500);
  5. void MainWindow::nextFile()
  6. {
  7. QSettings *settings;
  8. settings = new QSettings("Company", "my programm");
  9. QString s = settings->value("AddFile").toString();
  10. if(!s.isEmpty())
  11. {
  12. settings->setValue("AddFile", "");
  13. open(s);
  14. }
  15. }

Of course, you will see the work of the program after you add the libraries necessary for the snack to the compiled exe-file.

Project with the source code of the program

Do you like it? Share on social networks!

c
  • April 25, 2018, 3:31 a.m.
This is a nice start to learning how to use QWinJumplist but the demo files do not work when I try to load a file from the jump list Windows can't find the location of the file.
c
  • April 25, 2018, 9:41 a.m.

Unless I right click on the Recent items and select to open the txt file through a second context menu as opposed to a straight double click of the item.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html