当前位置:网站首页>Qmenu response in pyqt

Qmenu response in pyqt

2022-06-24 08:26:00 Qredsun

Demand scenarios :

stay qt In the interface , You need to add a response action to the menu .

Realization :

Look at the QMenu Object source code , Nothing like QPushButton.clicked() Signal function of . Only found QMenu.triggered() function , Already used QMenu.addAction(). That is to say , Only through triggered Trigger menu list , Add... To the menu list QAction(), Use QAction() Associate the corresponding slot function , Implement operational response .

Concrete realization :

  • qt The control association in the interface :
# UI  relation 
menuBar = QtWidgets.QMenuBar() #  menu bar 
menuBar.setGeometry(QtCore.QRect(0, 0, 1245, 23))
menuBar.setObjectName("menuBar")

menu = QtWidgets.QMenu() #  Menu buttons 
menu.setObjectName("menu")
menu_check = QtWidgets.QMenu(menuBar)
menu_check.setObjectName("menu_check")

actionSavePicture = QtWidgets.QAction() #  Action response , Modify the storage path 
actionSavePicture.setObjectName("actionSavePicture")

menu.addAction(actionSavePicture) #  Menu button add action 
  • The signal slot Association in the back-end response code
actionSavePicture.triggered.connect(changeImgSavePath)

def changeImgSavePath():
    #  Get directory path 
    save_path = QFileDialog.getExistingDirectory(None, caption=' Select the screenshot storage directory ')
    if save_path:
        img_save_path = save_path
        cam_conf = CameraConfig()
        cam_conf.img_save_path = save_path
        cam_conf._update_cfg_file()
        QMessageBox.about(None, ' Tips ', f' The screenshot storage directory is changed to :{
      save_path}')
    else:
        QMessageBox.about(None, ' Tips ', ' The screenshot storage directory has not been changed ')
原网站

版权声明
本文为[Qredsun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240520433186.html