Save file with QLineEdit data using getSaveFileName

0

I am writing an application using pyqt4 and python 3 . The data entry is by QLineEdit , mainly, and QTextEdit , the data is operated and the results are shown in other QLineEdit . On the monitor works well. Now I want to save this data to be able to access them in another session.

The problem is that even seeing many examples on the web I am unable to save the data in a file. The most I have ever been to save an empty file.

I write below a summary of what I have written:

The window is created with QtDesigner . After converting the file ui to py I write the code in Python . This is a summary:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import locale

from qtdesigner.cierzo_B import *

from classes.FEM_2131_2132.chapter_2.loads_A import AdditionalLoads

locale.setlocale(locale.LC_ALL, '')

class Window(QtGui.QDialog):

    def __init__(self, parent=None):
        ...
        self.wind_speed_travelling_ms = self.ui.lineEditTravellingSpeed_ms
        self.wind_speed_travelling_kmh = self.ui.lineEditTravellingSpeed_kmh
        ...
        # Wind speed travelling m/s to km/h
        QtCore.QObject.connect(self.ui.lineEditTravellingSpeed_ms,
                               QtCore.SIGNAL('editingFinished()'),
                               self.wind_speed_travelling_ms2kmh)
        # Wind speed travelling km/h to m/s
        QtCore.QObject.connect(self.ui.lineEditTravellingSpeed_kmh,
                               QtCore.SIGNAL('editingFinished()'),
                               self.wind_speed_travelling_kmh2ms)
        ...
        # Save project
        QObject.connect(self.ui.pushButtonSaveProject, 
                        QtCore.SIGNAL('clicked()'), self.save_project)
        ...

    def basic_pressure(self):

        vs_ms = float(self.wind_speed_in_service_ms.text())
        basic_press = self.wind_load.aerodynamic_wind_pressure(vs_ms)
        return basic_press

    def wind_speed_travelling_ms2kmh(self):

        vs_ms = float(self.wind_speed_travelling_ms.text())
        vs_kmh = int(3.6 * vs_ms)

        self.ui.lineEditTravellingSpeed_kmh.setText(str(vs_kmh))

        wind_pressure = self.wind_load.aerodynamic_wind_pressure(vs_ms)
        wind_pressure_ = locale.format('%.2f', wind_pressure)

        self.ui.lineEditTravellingPressure.setText(str(wind_pressure_))

        basic_pressure = self.basic_pressure()
        ratio = wind_pressure / basic_pressure
        ratio_ = locale.format('%.3f', ratio)

        self.ui.lineEditTravellingRatio.setText(str(ratio_))

    def wind_speed_travelling_kmh2ms(self):

        vs_kmh = float(self.wind_speed_travelling_kmh.text())
        vs_ms = int(vs_kmh / 3.6)

        self.ui.lineEditTravellingSpeed_ms.setText(str(vs_ms))

        wind_pressure = self.wind_load.aerodynamic_wind_pressure(vs_ms)
        wind_pressure_ = locale.format('%.2f', wind_pressure)

        self.ui.lineEditTravellingPressure.setText(str(wind_pressure_))

        basic_pressure = self.basic_pressure()
        ratio = wind_pressure / basic_pressure
        ratio_ = locale.format('%.3f', ratio)
    ...

In the method that I believe to save the data is where I have the problem.

def save_project(self):
    name = QtGui.QFileDialog.getSaveFileName(self, "Save file")
    f = open(name, 'w')

From here I have not managed to save the data in the file that is created.

Can you give me an example of how to save the data of QLineEdit and QTextEdit in a file? Thanks.

    
asked by Pedro Biel 02.07.2017 в 19:57
source

1 answer

1

I do not know exactly where you have the problem but you should simply get the text of your QLineEdit using the text() method.

In the case of QTextEdit it will depend on how you want to save the text, since they accept rich text. If you want to save it as plain text use the toPlainText method.

Then you open the file using the path that returns QtGui.QFileDialog().getSaveFileName and save the content. Use the with statement (context handler protocol) so that the file closes properly and automatically.

This is a functional example that allows you to save the contents of a LineEdit and a TextEdit in a text file:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("Guardar contenido")

        self.line_edit = QtGui.QLineEdit(self)
        self.text_edit = QtGui.QTextEdit(self)
        self.save_button = QtGui.QPushButton(text='Guardar', parent=self)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(QtGui.QLabel(text="QLineEdit"))
        layout.addWidget(self.line_edit)
        layout.addWidget(QtGui.QLabel(text="QTextEdit"))
        layout.addWidget(self.text_edit)
        layout.addWidget(self.save_button)

        self.save_button.clicked.connect(self.save_project)

        self.show()


    def save_project(self):
        name = QtGui.QFileDialog().getSaveFileName(self,
                                                   "Title",
                                                   "",
                                                   "Plain text file (*.txt)")
        with open(name, 'w') as f:
            f.write(self.line_edit.text() + "\n")
            f.write(self.text_edit.toPlainText())


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

You do not say anything about the structure that the file or encoding that you want must have. The previous code is limited to save the QString obtained from both widgets separated by a line break, if you need something more specific provides more data.

    
answered by 02.07.2017 / 22:55
source