Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add default “Open recent” macOS menu item in Qt application?
Forum Updated to NodeBB v4.3 + New Features

How to add default “Open recent” macOS menu item in Qt application?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EekTheCat
    wrote on 16 May 2018, 15:22 last edited by EekTheCat
    #1

    Hello!

    Need help with an application menu created using QMenu and QActions on macOS plaform.
    Menu and application works fine, except for recently opened files part. Haven't found anything about working with sandbox access rights on macOS platform using Qt, and my submenu "Recent files" fails to load saved urls after app is restarted.

    Googling of that problem showed that pretty easy way exists: use NSDocumentController thing and default "open recent" cocoa menu that handles everything about access rights.

    That's where the question starts: whatever i do there is no "Open recent" submenu inside "File". Found some info here, but i can't understand how to do it in qt based application. No info about that menu in QMenuBar documentation either.

    Any advice, example?

    G 1 Reply Last reply 16 May 2018, 15:30
    0
    • E EekTheCat
      16 May 2018, 15:22

      Hello!

      Need help with an application menu created using QMenu and QActions on macOS plaform.
      Menu and application works fine, except for recently opened files part. Haven't found anything about working with sandbox access rights on macOS platform using Qt, and my submenu "Recent files" fails to load saved urls after app is restarted.

      Googling of that problem showed that pretty easy way exists: use NSDocumentController thing and default "open recent" cocoa menu that handles everything about access rights.

      That's where the question starts: whatever i do there is no "Open recent" submenu inside "File". Found some info here, but i can't understand how to do it in qt based application. No info about that menu in QMenuBar documentation either.

      Any advice, example?

      G Offline
      G Offline
      Gojir4
      wrote on 16 May 2018, 15:30 last edited by
      #2

      @EekTheCat Hi,
      I don't know about NSDocumentController. But you can do this from the Qt side pretty easily. Please see this example: Recent Files Example.

      E 1 Reply Last reply 16 May 2018, 15:48
      0
      • G Gojir4
        16 May 2018, 15:30

        @EekTheCat Hi,
        I don't know about NSDocumentController. But you can do this from the Qt side pretty easily. Please see this example: Recent Files Example.

        E Offline
        E Offline
        EekTheCat
        wrote on 16 May 2018, 15:48 last edited by
        #3

        @Gojir4, my app do have "recent files" and it works fine on Windows. But! on macOS with enabled Sandbox mode it becomes useless list of file names which the app fails to read unless user explicitly opens or drag&drop that file to app window to give it access rights.
        I can't find how to solve it with Qt, and have too little knowlege of Objective C and cocoa to do it other way.

        G 1 Reply Last reply 16 May 2018, 15:57
        0
        • E EekTheCat
          16 May 2018, 15:48

          @Gojir4, my app do have "recent files" and it works fine on Windows. But! on macOS with enabled Sandbox mode it becomes useless list of file names which the app fails to read unless user explicitly opens or drag&drop that file to app window to give it access rights.
          I can't find how to solve it with Qt, and have too little knowlege of Objective C and cocoa to do it other way.

          G Offline
          G Offline
          Gojir4
          wrote on 16 May 2018, 15:57 last edited by
          #4

          @EekTheCat Sorry, I have misunderstood. Unfortunately I cannot help.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on 16 May 2018, 19:54 last edited by mpergand
            #5

            If i understand well, you want to call this from cocoa:

            [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:aFileName]];
            

            Have a look at this link, I explain how to call a cocoa method from Qt:
            https://forum.qt.io/topic/82609/remove-native-mac-menu-items-such-as-show-tab-bar/3

            You need to create your method like that:

            void  CocoaBridge::registerRecentUrl(const QUrl &url)
             {
            	[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: url.toNSURL()];
             }
            

            Hope it's work, i can't test it ...

            E 1 Reply Last reply 16 May 2018, 23:09
            1
            • M mpergand
              16 May 2018, 19:54

              If i understand well, you want to call this from cocoa:

              [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:aFileName]];
              

              Have a look at this link, I explain how to call a cocoa method from Qt:
              https://forum.qt.io/topic/82609/remove-native-mac-menu-items-such-as-show-tab-bar/3

              You need to create your method like that:

              void  CocoaBridge::registerRecentUrl(const QUrl &url)
               {
              	[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: url.toNSURL()];
               }
              

              Hope it's work, i can't test it ...

              E Offline
              E Offline
              EekTheCat
              wrote on 16 May 2018, 23:09 last edited by
              #6

              @mpergand, thanks, but that is not what the question about. Maybe i failed to explain the problem properly... sorry.

              I did wrote exactly same code with QUrl and noteNewRecentDocumentURL. As i said the part with NSDocumentController was easy to google.

              The problem is:
              Menu in my application created using QMenu and QMenu.addAction(...), which is then (i guess) translated by Qt to native cocoa menu.
              NSDocumentController should automatically add new menu item called "Open recent" to "File" menu (or first item?) right next to some specific menu item.
              I can't find how to create that specific menu item, or set some specific property using Qt menu.
              Or maybe Qt have something that handles macOS security-scoped bookmarks and all that apple annoying sandbox limitations?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on 17 May 2018, 12:59 last edited by
                #7

                I can't find how to create that specific menu item, or set some specific property using Qt menu.

                I think it's pretty hard, this item is created by IB.
                Look at this link:
                http://lapcatsoftware.com/blog/2007/07/10/working-without-a-nib-part-5-open-recent-menu/

                I wonder how this security issue is resolved in recent Qt apps, like QtCreator who has recent menu ?

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  EekTheCat
                  wrote on 18 May 2018, 15:12 last edited by
                  #8

                  I wonder how this security issue is resolved in recent Qt apps, like QtCreator who has recent menu ?

                  QtCreator is not on AppStore and works without sandbox mode on macOS, so it needs no entitlements to read files, just access rights that the user has.

                  Everything turned out easier than i thought.

                  I was able to get it to work without using 'default' cocoa menu, but using url list from NSDocumentController.
                  Now recent menu works even simplier on macOS since everything about saving/loading the url list and access rights NSDocumentController does itself. I just need to rebuild QMenu when the list changes.

                  And as a bonus 'recent files' appeared inside application context menu in dock panel.
                  (note: code in .mm file)

                  QStringList GetRecentList( )
                  {
                      QStringList result;
                      NSArray<NSURL *>* urls = [[NSDocumentController sharedDocumentController] recentDocumentURLs];
                  
                      for (int i=0; i< [urls count]; i++) {
                  	NSURL* url = [urls objectAtIndex:i];
                  	NSString* str = [url absoluteString];
                  			
                  	result.append( QString::fromNSString(str) );
                      }
                  
                      return result;
                  }
                  
                  bool SendToRecent( QString filepath )
                  {
                      [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: [NSURL     fileURLWithPath:filepath.toNSString()]];
                  
                  	return YES;
                  }
                  
                  M 1 Reply Last reply 4 Feb 2022, 17:15
                  2
                  • E EekTheCat
                    18 May 2018, 15:12

                    I wonder how this security issue is resolved in recent Qt apps, like QtCreator who has recent menu ?

                    QtCreator is not on AppStore and works without sandbox mode on macOS, so it needs no entitlements to read files, just access rights that the user has.

                    Everything turned out easier than i thought.

                    I was able to get it to work without using 'default' cocoa menu, but using url list from NSDocumentController.
                    Now recent menu works even simplier on macOS since everything about saving/loading the url list and access rights NSDocumentController does itself. I just need to rebuild QMenu when the list changes.

                    And as a bonus 'recent files' appeared inside application context menu in dock panel.
                    (note: code in .mm file)

                    QStringList GetRecentList( )
                    {
                        QStringList result;
                        NSArray<NSURL *>* urls = [[NSDocumentController sharedDocumentController] recentDocumentURLs];
                    
                        for (int i=0; i< [urls count]; i++) {
                    	NSURL* url = [urls objectAtIndex:i];
                    	NSString* str = [url absoluteString];
                    			
                    	result.append( QString::fromNSString(str) );
                        }
                    
                        return result;
                    }
                    
                    bool SendToRecent( QString filepath )
                    {
                        [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: [NSURL     fileURLWithPath:filepath.toNSString()]];
                    
                    	return YES;
                    }
                    
                    M Offline
                    M Offline
                    martial
                    wrote on 4 Feb 2022, 17:15 last edited by
                    #9

                    @EekTheCat perfect, thanks a lot

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved