简体   繁体   English

从QLineEdit读取文本作为Qmenu选项的子菜单

[英]reading text from QLineEdit as a submenu of a Qmenu option

Reading the text a QlineEdit which is part of a Qmenu 读取文本QlineEdit是Qmenu的一部分

As shown in the code initially I though I could use a trigger function to determine what option changed and then read the text. 如最初的代码所示,尽管我可以使用触发函数来确定更改了哪个选项,然后读取文本。 If I could figure out how to read the QlineEdit with currenttext() from the submenu I could check if the user has changed the input when required. 如果我能弄清楚如何从子菜单中使用currenttext()读取QlineEdit,则可以检查用户是否在需要时更改了输入。

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QActionGroup, QMenu, QApplication, QLineEdit, QWidgetAction

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.menubar = self.menuBar()
        self.menubar.installEventFilter(self)
        self.fileMenu = self.menubar.addMenu('&Circuit Set-Up')
        self.populate()

        self.setGeometry(300, 300, 300, 200)
        self.show()


    def triggered(self, action):
        print(action.text())


    def populate(self):

        for m in range(3):

            setattr(self,'impMenu'+str(m),QMenu('Channel'+str(m), self))

            factors=['Enter Transducer Calibration Constant [default 1] = 1',
            'Enter Gauge Factor [default 2] = 2',
            'Passion Ratio [default 0.3] = 0.3']

            for n in range(3):

                ql = QLineEdit(factors[n])
                ql.setMinimumWidth(350)
                wAction = QWidgetAction(self)
                wAction.setDefaultWidget(ql)
                getattr(self,'impMenu'+str(m)).addAction(wAction)

            self.fileMenu.addMenu(getattr(self,'impMenu'+str(m)))
            setattr(self,'triggered'+str(m),self.triggered)
            getattr(self,'impMenu'+str(m)).triggered.connect(getattr(self,'triggered'+str(m)))


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

The QLineEdit is above the menu and does not notify you if the user has changed the text so using the information from the QMenu/QAction is unproductive. QLineEdit在菜单上方,如果用户更改了文本,则不会通知您,因此使用QMenu / QAction中的信息无效。 If you want to get the text when the user changes it then use the QLineEdit signals. 如果要在用户更改文本时获取文本,请使用QLineEdit信号。

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QAction, QApplication, QLineEdit, QMainWindow, QMenu, QWidgetAction


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.menubar = self.menuBar()
        self.menubar.installEventFilter(self)
        self.fileMenu = self.menubar.addMenu("&Circuit Set-Up")
        self.populate()

        self.setGeometry(300, 300, 300, 200)
        self.show()

    @pyqtSlot(str)
    def onTextChanged(self, text):
        print(text)

    def populate(self):

        factors = [
            "Enter Transducer Calibration Constant [default 1] = 1",
            "Enter Gauge Factor [default 2] = 2",
            "Passion Ratio [default 0.3] = 0.3",
        ]

        for m in range(3):
            menu = QMenu("Channel{}".format(m), self)
            self.fileMenu.addMenu(menu)
            for n in range(3):
                ql = QLineEdit(factors[n])
                ql.setMinimumWidth(350)
                ql.textChanged.connect(self.onTextChanged)
                wAction = QWidgetAction(self)
                wAction.setDefaultWidget(ql)
                menu.addAction(wAction)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM