QMenu on QPushbutton sometimes doesn't attach? [PySide/Python 3]



  • I have several dynamically-generated buttons, each with their own attached QMenu. When I run my script, sometimes the QMenu attaches to all of the buttons, and sometimes it only attaches to a (seemingly random) number of buttons (leaving the other buttons menu-less). I can't detect a pattern to which ones "work" and which don't work each time I run the script, but the last few buttons are almost always correct.

    All of the QMenus and buttons are generated dynamically in a for loop, so I don't have any idea why some buttons would "work" and others wouldn't (or why the results would be different each time I run the script..).

    Each button has its own instance of the QMenu, but they are all identical. The QMenu's action texts are generated from a QSLQuery Model.

    I'll paste some relevant sample code below. I can't fit the whole script, but here is a description of some of the classes I haven't not included.

    class EntryRow(QWidget) -- includes a person's name (label), and several widgets to change that person's data, all arranged in an HBox layout. Each EntryRow's HBox is placed into its own grid location in the central widget.

    EACH ENTRY ROW INCLUDES A "self.commentsButton," which is a QPushButton with an attached QMenu.

    class CommentMenu(QMenu) -- each person's "commentsButton" has its own instance of the QMenu

    Here is how each menu is instantiated:
    @

    THIS OCCURS WITHIN METHOD "createWidgets" of the class EntryRow

        self.commentsButton = QPushButton("Com.")
        self.commentsButton.setFixedWidth(45)
        self.commentsButton.setCheckable(True)
        
        q = self.page.partCommentModel
    
        self.menu = CommentMenu(q, self, self.page)
        self.commentsButton.setMenu(self.menu)
    

    @

    @
    class CommentMenu(QMenu):
    def init(self, model, entryRow, page):
    super().init()
    self.actionList = []

        model.query().first()
        while model.query().next():
            text = model.query().record().value(0)
            #print("Value is {0}".format(text))
            newAction = QAction(entryRow)
            newAction.setText(text)
            newAction.setCheckable(True)
            self.actionList.append(newAction)
            self.addAction(newAction)@
    

    Any help would be appreciated. Let me know if there's any other relevant code you need me to post.

    Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.