I have a QDateEdit in my GUI from which I convert the QDate to QString and add it to my database. The QString date is saved in the database in this format: 20/12/2015.
In case a user want to edit the date, then I need to show the date on the QDateEdit field on the GUI again. Hence, I need to fetch the database, bring back the date (which is in QString format) and convert it to QDate back again in order to put it on the QDateEdit field on the GUI.
However, I cannot manage to convert that QString format (i.e.: 20/12/2015) to QDate using the following:
QString date_string_on_db = "20/12/2015";
QDate Date;
Date.fromString(date_string_on_db,"dd/MM/YYYY");
The Date is always returning invalid.
what should I do ?
First of all, the format string should be dd/MM/yyyy. Qt documentation for the QDate class says that yyyy is recognized as a four digit year number.
Second of all, fromString is a static function that returns a new QDate. Currently, the return value of that function is discarded : it is not written back into the Date variable, as you might think. The complete correct code should therefore look like this :
QString date_string_on_db = "20/12/2015";
QDate Date = QDate::fromString(date_string_on_db,"dd/MM/yyyy");
Related
I've already implemented a QTableView + QStandardItemModel in Qt5. At the beginning, I set the date data just as a string with the date format based on the application setting. For instance, it can be the US format like MM/dd/yyyy or the european format dd.MM.yyyy. The data comes from a json file with the european date format. My first implementation was like this:
shared_ptr<QStandardItemModel> _model;
// detect a date string with regex, get the submatches and create a QDate object from it
QDate date(stoi(submatches[3].str()), stoi(submatches[2].str()), stoi(submatches[1].str()));
QModelIndex index = _model->index(rowPos, colPos, QModelIndex());
// depends on the setting, the date can be shown on the table like this
_model->setData(index, QString(date.toString("dd.MM.yyyy"));
// activate the column sorting in the QTableView
ui->tableView->setSortingEnabled(true);
This implementation, however, cannot sort the date column correctly. The reason is because the QTableView sorts the column just like a string (sorted by day isntead of by year first) instead of a date entry.
I can change the implementation by setting the data directly with the date object:
_model->setData(index, date);
The sorting works perfectly by date. But, the format is now always shown in dd/MM/yyyy format.
How can I keep this sorting function, but change the date view depends on the date format setting?
I've read that it may be implemented using a custom subclass of QAbstractTableModel. How about implementing as a SubClass of QTableView? Or may be with a subclass of QAbstractItemModel like in here? I'm not an expert yet to implement and integrate a Qt5 subclass.
The solution is to pass the QDate as data to the model, and use a delegate to set as shown in the view:
_model->setData(index, date);
class DateDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QString displayText(const QVariant &value, const QLocale &locale) const{
return locale.toString(value.toDate(), "dd.MM.yyyy");
}
};
ui->tableView->setItemDelegateForColumn(col_date, new DateDelegate);
I have a problem with convert a current date.
I want to put a current date in a QLabel but i can't put a QDate type.
QDate today;
today=QDate::currentDate();
datamiasto->setText("Data wystawienia: "+today+", xyz");
I can put it when i convert it to string but when i do it date is not in digits.
How can i put a digit date in a QLabel ?
Try this:
QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
QDateTime::toString(const QString &format) : Returns the datetime as a string. The format parameter determines the format of the result string. (give your custom format to the function)
QDateTime::currentDateTime() : Returns the current datetime, as reported by the system clock, in the local time zone.
I am using Qt GUI to save data in MySQL using C++.
I am using QDateTimeEdit widget for this. Whatever the value changed by user in GUI of QDateTimeEdit, it should be inserted in MySQL.
Can anyone tell me how to do that?
How to access value from QDateTimeEdit and converting it in proper format like QString and using MySQL query inserting it into database?
An alternative is not to convert it to a QString but let the driver do that for you. If you expect some precision in the conversion some cases this might be better, other cases it can be worse:
QDateTime date = ui->dateTimeEdit->dateTime();
QSqlQuery query(myDatabase);
query.prepare("INSERT INTO my_table (id, date) "
" VALUES (:id, :date)");
query.bindValue(":id", 1001);
query.bindValue(":date", date);
query.exec();
The QSqlQuery::bindValue() function will take the QDateTime and pass it through as a QVariant and then the driver should know how to convert a QVariant::DateTime to the correct string that the database understands.
About second part "how to access value":
You somehow in code created object of QDateTimeEdit and place it on some layout. Typically it will be pointer with name for example mpDTPicker.
QDateTimeEdit * mpDTPicker = new QDateTimeEdit();
//place mpDTPicker on layout.
For access current time we need use method dateTime:
//User actions with date -> emitted signal -> execute slot with our logic
{
QDateTime momentum = mpDTPicker->dateTime();
// So here we need convert QDateTime to QString and we will use [toString Method](http://doc.qt.io/qt-4.8/qdatetime.html#toString)
QString result_string = momentum.toString("dd:mm:yy");
QDebug() << result_string;
}
So that is all about converting QDateTime to QString.
About first part of Question how to get that user changed value of DateTimeEdit is total another question.
And about third part how to store it in mysql database everything depended on structure of your table. But typicaly it can be solved with simple query:
QSqlQuery query;
QString mQuerry = "INSERT INTO mytable (id, date) VALUES (0, \"" +result_string "\" )";
query.exec(mQuerry);
And please READ DOCS especial when them so cool :)
I have this QDateEdit widget but it displays always a standard time like 01-01-2014 (in DD-MM-YYYY, but I can change this in the UI). But I want it to display the current time if the dialog (where this QDateEdit widget is) is opened.
I tested this just on a label (see the void form_dialog). But its printss out "sa nov 23 2013". But I want it like 23-11-2013. Also it gives me an error :
QDateTimeEdit::setDate' : cannot convert parameter 1 from 'QString' to 'const QDate &'
Reason: cannot convert from 'QString' to 'const QDate'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
So
void form_dialog::setCurrentDate()
{
QDate date = QDate::currentDate();
QString dateString = date.toString();
ui->DateEdit->setDate(dateString);
}
Thanks!
Maybe its usefull to have something visual, the widget that is displayed below should display the current date:
First, the docs indicate that the QDateEdit object has parameterized constructor that permits the date to be initialized on construction:
QDateEdit::QDateEdit ( const QDate & date, QWidget * parent = 0 )
Depending upon how you have instantiated your QDateEdit object, you might also consider subclassing QDateEdit with your own widget, and the setting up the QDateEdit object in your widget's constructor. This method may overkill, though (without code, it's tough to speculate what is best...)
The edited question provides more to go on. The QDateEdit docs indicate that a QDateEdit method accepts a QDate object, not a QString object. So your function should appear something more like:
void form_dialog::setCurrentDate()
{
QDate date = QDate::currentDate();
ui->DateEdit->setDate(date);
}
Note that your compiler is reporting the mismatched object types in the error message above.
If you want to show the current date when your dialog comes up, I would do the following:
handle the dialog's show event,
get the current date,
set current date to the date edit widget.
For example:
void MyDialog::showEvent(QShowEvent * event)
{
QDate date = QDate::currentDate();
m_dateEdit->setDate(date); // sets the current date to date edit.
QDialog::showEvent(event);
}
I need to get date and time of received and sent messages . I used deviceTimestamp() method of Message class but this returns obj of QDateTime but I want this in String . So could anyone please tell me how should I do this or any other way of getting message time and date ?
Once you obtain your QDateTime, can't use the toString() method?
https://qt-project.org/doc/qt-5.0/qtcore/qdatetime.html#toString
QString dateString = yourQDateTime.toString("MM-dd-yyyy hh:mm");