【问题标题】:QLineEdit returns empty stringQLineEdit 返回空字符串
【发布时间】:2018-05-03 11:13:54
【问题描述】:

我正在尝试从另一个类访问用 QLineEdit 编写的文本,但它返回一个空字符串。

这是一段简约的函数式代码(Python 2.7):

import sys

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.createmenus()
        self.initui()

        self.central_widget = QStackedWidget()
        self.setCentralWidget(self.central_widget)

        testWidgets = Container0()
        self.central_widget.addWidget(testWidgets)

    def initui(self):
        self.setWindowTitle("TestWindow")
        self.show()

    def createmenus(self):
        viewFile = self.menuBar().addMenu("File")

        expMenu = QMenu("Export", self)
        expAct = QAction("Save", self)
        expMenu.addAction(expAct)

        expMenu.triggered[QAction].connect(self.export_config)

        viewFile.addMenu(expMenu)

    def export_config(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getSaveFileName(self, "Save as", "C:/", "Text (*.txt);;All Files (*)",
                                              options=options)
        if fileName:
            Export().export(fileName)

class Container0(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()

        self.mainlayout = QVBoxLayout(self)
        self.vbox_layout = QVBoxLayout()

        self.text1 = QLineEdit()
        print self.text1

        self.vbox_layout.addWidget(self.text1)
        self.mainlayout.addLayout(self.vbox_layout)

class Export():
    def export(self, path):
        tw = Container0()

        t1 = tw.text1
        t1text = t1.text()

        print t1
        print t1text
        print path

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

关于代码:

首先我创建我的QMainWindow,它调用函数inituicreatemenus。在下一个类Container0 中,我实现了QLineEdit 小部件。我将这些类分开是因为我正在编写的应用程序有几个窗口和一个带有“下一步”和“后退”按钮的工具栏,这会将centralWidget 更改为我保存下一个要显示的窗口的类。 (我希望这不是一个坏主意)

我想要达到的目标

目前,当我在QLineEdit 中写入内容,然后转到菜单“文件-> 导出-> 保存”并插入路径时,代码调用类Export 及其函数export,它应该读取我在QLineEdit 中写的字符串并将其打印在路径中。问题是我的 QLineEdit 输出是一个空字符串。此外,函数Export 似乎从QLineEdit 创建了另一个实例。注意我调用print self.text1时不同的内存位置:

<PyQt5.QtWidgets.QLineEdit object at 0x0000000002968B88>
<PyQt5.QtWidgets.QLineEdit object at 0x0000000002968DC8>
<PyQt5.QtWidgets.QLineEdit object at 0x0000000002968DC8>
                                                        ### <- This is the empty line
C:/123

这让我想知道如何在不创建另一个实例的情况下调用QLineEdit 字符串。此外,这是处理多个窗口的正确方法吗?

TL,DR: QLineEdit 返回一个空字符串。它不应该。也欢迎对代码结构提出建议。谢谢!

【问题讨论】:

    标签: python pyqt menubar qlineedit


    【解决方案1】:

    每次使用以下表达式:

    some_variable = Container0()
    

    您正在创建一个新容器,因此在您的情况下,在MainWindow__init__ 中创建的容器与在Exportexport 方法中创建的容器不同。因此,即使你在主窗口中放置文本,其他 Container 中也没有文本。

    解决方法是让testWidgets成为类成员,并将文本内容传递给导出:

    import sys
    
    from PyQt5 import QtWidgets, QtGui, QtCore
    from PyQt5.QtWidgets import *
    
    class MainWindow(QMainWindow):
    
        def __init__(self):
            super(MainWindow, self).__init__()
    
            self.createmenus()
            self.initui()
    
            self.central_widget = QStackedWidget()
            self.setCentralWidget(self.central_widget)
    
            self.testWidgets = Container0()
            self.central_widget.addWidget(self.testWidgets)
    
        def initui(self):
            self.setWindowTitle("TestWindow")
            self.show()
    
        def createmenus(self):
            viewFile = self.menuBar().addMenu("File")
    
            expMenu = QMenu("Export", self)
            expAct = QAction("Save", self)
            expMenu.addAction(expAct)
    
            expMenu.triggered[QAction].connect(self.export_config)
    
            viewFile.addMenu(expMenu)
    
        def export_config(self):
            options = QFileDialog.Options()
            options |= QFileDialog.DontUseNativeDialog
            fileName, _ = QFileDialog.getSaveFileName(self, "Save as", "C:/", "Text (*.txt);;All Files (*)",
                                                  options=options)
            if fileName:
                Export().export(fileName, self.testWidgets.text1.text())
    
    class Container0(QWidget):
        def __init__(self):
            super(QWidget, self).__init__()
    
            self.mainlayout = QVBoxLayout(self)
            self.vbox_layout = QVBoxLayout()
    
            self.text1 = QLineEdit()
            print(self.text1)
    
            self.vbox_layout.addWidget(self.text1)
            self.mainlayout.addLayout(self.vbox_layout)
    
    class Export():
        def export(self, path, t1text):
            print(t1text)
            print(path)
    
    if __name__ == '__main__':
        app = QApplication([])
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 天啊,我现在感觉好傻。非常感谢你的帮助!顺便说一句,如果你没有一个QLineEdit,但像其中的 20 个,你会怎么做?你会用列表或字典将它们单独传递给函数export吗?还是你会完全改变结构?
    • @Iridium 你会做的是它们的列表或字典,视情况而定,然后创建一个迭代、连接和返回数据的方法。
    猜你喜欢
    • 2020-01-27
    • 2020-07-11
    • 2015-05-25
    • 2011-05-09
    • 1970-01-01
    • 2012-07-20
    • 2011-12-15
    • 2020-11-23
    • 2015-12-21
    相关资源
    最近更新 更多