0

I have a QDateEdit field on my mainwindow.

I capture whatever date is selected by the user as QString before insert it into the database.

QString membership_Date = ui->memebershipDate->text();

Then, in case the user want to update the date, I retrieve the QString date from the database. Here is the last step in the process to retrieve the date as QString (and after checking, the whole process always return the correct date):

QString date_temp = modelToShow->record(row_index).value(11).toString(); 

Then I convert the QString to QDate as follow:

QDate membership_Date = QDate::fromString(date_temp,"dd/MM/yyyy");

However, when I try to set the QDateEdit field using the following:

ui->memebershipDate->setDate(membership_Date);

It doesn't work and the QDateEdit never change and shows the default date.

How to set this right?

McLan
  • 2,552
  • 9
  • 51
  • 85
  • 1
    Perhaps `QDate::fromString` fails, and debugging the value in membership_Date would shed some light on it? – hauron Mar 18 '16 at 14:35
  • 1
    What `membership_Date.isValid()` returns? – vahancho Mar 18 '16 at 14:35
  • 1
    maybe debug it and find out the actual problem cus that looks fine, my guess is that date_temp isnt correct – AngryDuck Mar 18 '16 at 14:40
  • @vahancho : the return value is false. But how to fix it, I mean what is the right syntax ? – McLan Mar 18 '16 at 15:15
  • @hauron : I am unable to debug , "locals & expressions" view is not operational. But, the return of `isValid` is false. I don't know any other syntax though – McLan Mar 18 '16 at 15:17
  • 1
    Why even use `QString`? The database probably supports date types, so they should be used instead of relying on a particular text format. – Sergei Tachenov Mar 18 '16 at 15:18
  • @AngryDuck: I am unable to debug , "locals & expressions" view is not operational. But, the return of `isValid` is false. I don't know any other syntax though and youself you said it looks fine. what might be the problem ? – McLan Mar 18 '16 at 15:18
  • 1
    i would say date temp string is incorrect, print the string to console and see what it is – AngryDuck Mar 18 '16 at 15:19
  • @SergeyTachenov : You are right and I will change it eventually (it is a very tedious job, I have to change so many things and I want to do it carefully). For now though I want to work all the functionalities first (e.g.: update record details) and then later refine the code. – McLan Mar 18 '16 at 15:21
  • 1
    Well then, try printing `date_temp.toUtf8().toHex()` so we can see whether there are any bogus characters lurking about. – Sergei Tachenov Mar 18 '16 at 15:24
  • 1
    or simply print the date and make sure its actually a date – AngryDuck Mar 18 '16 at 15:24
  • 1
    `std::cout << date_temp.toStdString() << std::endl;` – AngryDuck Mar 18 '16 at 15:29
  • It is working now. I had to change `"dd/MM/yyyy"` to `"yyyy/MM/dd"` .. and that is it. Thanks for your help. – McLan Mar 18 '16 at 15:49

0 Answers0