Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt create a qmenu on a button click

import sys
from PyQt4 import QtGui, QtCore
class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        # create button
        self.button = QtGui.QPushButton("test button",self)       
        self.button.resize(100, 30)

        # set button context menu policy
        self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.connect(self.button, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
        self.popMenu = QtGui.QMenu(self)

    def on_context_menu(self, point):
        self.popMenu.clear()

        #some test list for test
        testItems = ['itemA', 'itemB', 'itemC']
        for item in testItems:
        action = self.popMenu.addAction('Selected %s' % item)
        action.triggered[()].connect(
            lambda item=item: self.printItem(item))
        self.popMenu.exec_(self.button.mapToGlobal(point))

    @QtCore.pyqtSlot(str)
    def printItem(self, item):
        print item

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: which is best between c and python for making application 
Python :: sklearn recognising sentences 
Python :: python consecutive numbers difference between 
Python :: Rewrite the equation shown in Figure 2.4 as a Python expression and get the result of the equation: Pay special attention to the order of operations. 
Python :: extract label from tf data 
Python :: Percentage change between the current and the prior element. 
Python :: remove brackets from string python 
Python :: extends template django file system 
Python :: how to copy items in list n times in list python 
Python :: python fibonacci numbers 
Python :: python code to print fibonacci series 
Python :: when was python 3 released 
Python :: EMAIL_BACKEND where to read 
Python :: Read large SAS file ilarger than memory n Python 
Python :: csv.DictReader Skip Lines 
Python :: qtoverlay 
Python :: pytorch rolling window 
Python :: mechanize python #2 
Python :: Lazada link 
Python :: download python for windows 7 32-bit 
Python :: pandas meerge but keep certain columns 
Python :: check variable type godot 
Python :: install requests-html modlule click on the link to learn more about requests-html 
Python :: how to make a new df from old 
Python :: allowed_hosts error ecs django 
Python :: html in nested structure 
Python :: List Comprehension simple example 
Python :: write console output in same place 
Python :: python to uml 
Python :: ouvrir fichier txt python et le lire 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =