Open date in .txt in DateEdit Qt C ++

I am trying to save to .txt a DataEdit date in QtDesigner and then open the .txt and put the date in DataEdit.

save - okey:

QTextStream out(&sFile);

        out << ui.lineEdit_2->text()<< "\n"
            << ui.lineEdit->text()<< "\n" 
            << ui.dateEdit->text();

      

Open (this is the problem):

  QTextStream in(&sFile);
        QString text = in.readLine();
        QString name = in.readLine();
        QDateEdit fecha = in.readLine();

        sFile.close();
        ui.lineEdit_2->setText(text);
        ui.lineEdit->setText(name);
        ui.dateEdit->date(fecha);

      

Error 1 error C2440: "initialization": cannot convert from "QString" to "QDateEdit"
Error 2 errors C2660: "QDateTimeEdit :: date": function does not accept 1 argument

I don't know how to change this.

+3


source to share


3 answers


This is not the best way to save your settings. You save it as text, but you have no control over the text encoding, date format, etc.

In any case, the minimum changes to your code are as follows:



QDate fecha = QDate::fromString(in.readLine());
...
ui.dateEdit->setDate(fecha);

      

+1


source


QDateEdit is a widget, not a QDate and QDate can be serialized, which the widget doesn't make sense to serialize - you are actually writing down the QDateEdit text and want to create a QDateEdit from that text.



Instead, you need to serialize the QDate of that edit and then return the QDate and set it to ui.dateEdit.

+1


source


try a method QDate::fromString()

to get from it QDate

. then just callQDateEdit::setDate(const QDate& date)

soo long zai

0


source







All Articles