Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Menu Actions with variable number of actions
Forum Updated to NodeBB v4.3 + New Features

Menu Actions with variable number of actions

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 316 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.
  • A Offline
    A Offline
    AdamG
    wrote on last edited by AdamG
    #1

    I'm trying to create a menu item that has a variable number of options dependent on what is in a MySQL database. Right now I have the following (pseudo)code:

    def __init__(self, db, parent, logger=None):
         self.menu = QMenuBar(parent)
         self.parent = parent
         bookmarks = self.menu.addMenu("Bookmarks")
         bookmarks = self.menu.addMenu("Bookmarks")
         for action in self.createBookmarks():
             bookmarks.addAction(action)
    
    def createBookmarks(self): 
         self.bookmarkData = self.db.getBookmarks()
         for i in range(self.bookmarkData.shape[0]):
             action = QAction(self.bookmarkData.Name[i], self.parent)
             action.setStatusTip("Go to {}".format(self.bookmarkData.Name[i]))
             action.triggered.connect(lambda: self.parent.go_to_URL(QUrl("{}".format(self.bookmarkData.Address[i]))))
             yield action
    

    where self.db.getBookmarks() returns a pandas DataFrame object that is

    Name                    Address
    Facebook                www.facebook.com
    Instagram               www.instagram.com
    DuckDuckGo              www.duckduckgo.com
    :
    :
    :
    

    and the idea is to create a menu that adjusts the entries dependent on what is in the database table. Right now the bookmarks menu is set up correctly, but all actions go to the very last entry that was created in the menu (in the example data I provided it would always go to www.duckduckgo.com no matter what you click on). Can you please help me fix how the bookmarks menu is created to connect to the correct addresses?

    1 Reply Last reply
    0
    • A AdamG

      @SGaist can you please help me fix my lambda invocation? I'm new to using lambdas. Thank you.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      This stack overflow answer shows it nicely.

      The short version is:

      bookmarked_url = QUrl("{}".format(self.bookmarkData.Address[i]))
      action.triggered.connect(lambda url=bookmarked_url: self.parent.go_to_URL(url))
      

      Beware that you are creating objects that can be garbage collected when doing so. I strongly encourage you to refactor your code following my suggestion above.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        Your code issue is that you are not capturing the variable in your lambda.

        That said, you have a design issue. Child widgets should not know anything about their parent. This create tight coupling which is bad.

        To have something cleaner, you should add a signal (for example bookmarkActivated) to your current widget and connect your parent to that signal. Then in your current widget, have a private slot that will emit the signal based on the action triggered.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Your code issue is that you are not capturing the variable in your lambda.

          That said, you have a design issue. Child widgets should not know anything about their parent. This create tight coupling which is bad.

          To have something cleaner, you should add a signal (for example bookmarkActivated) to your current widget and connect your parent to that signal. Then in your current widget, have a private slot that will emit the signal based on the action triggered.

          A Offline
          A Offline
          AdamG
          wrote on last edited by
          #3

          @SGaist can you please help me fix my lambda invocation? I'm new to using lambdas. Thank you.

          SGaistS 1 Reply Last reply
          0
          • A AdamG

            @SGaist can you please help me fix my lambda invocation? I'm new to using lambdas. Thank you.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            This stack overflow answer shows it nicely.

            The short version is:

            bookmarked_url = QUrl("{}".format(self.bookmarkData.Address[i]))
            action.triggered.connect(lambda url=bookmarked_url: self.parent.go_to_URL(url))
            

            Beware that you are creating objects that can be garbage collected when doing so. I strongly encourage you to refactor your code following my suggestion above.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • A AdamG has marked this topic as solved on

            • Login

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