Qt - getting the current date in mmmm YYYY format (as a string)

islandmonkey picture islandmonkey · Nov 2, 2013 · Viewed 9.7k times · Source

I'm trying to retrieve the current date as a string in mmmm YYYY format with QDate. However, I can't pass QDate::currentDate with an argument. Another problem is the fact that the function argument I am passing this in is QString, meaning that g++ throws conversion errors. How can get around this?

Here the code at the moment:

QDate date = QDate::currentDate();
Core::MessageUser(this->CurrentEdit->User, warning, QString::number(date),
                  title, true, dependency);

The compiler error I am constantly getting whatever I do is this:

mainwindow.cpp: In member function 'bool Huggle::MainWindow::Warn(QString, Huggle::RevertQuery*)':
mainwindow.cpp:463:77: error: no matching function for call to 'QString::number(QDate&)'
     Core::MessageUser(this->CurrentEdit->User, warning, QString::number(date),
                                                                             ^
mainwindow.cpp:463:77: note: candidates are:
In file included from /usr/include/qt5/QtCore/qobject.h:48:0,
                 from /usr/include/qt5/QtWidgets/qwidget.h:46,
                 from /usr/include/qt5/QtWidgets/qmainwindow.h:45,
                 from /usr/include/qt5/QtWidgets/QMainWindow:1,
                 from mainwindow.h:14,
                 from mainwindow.cpp:11:
/usr/include/qt5/QtCore/qstring.h:556:20: note: static QString QString::number(int, int)
     static QString number(int, int base=10);
                    ^
/usr/include/qt5/QtCore/qstring.h:556:20: note:   no known conversion for argument 1 from 'QDate' to 'int'
/usr/include/qt5/QtCore/qstring.h:557:20: note: static QString QString::number(uint, int)
     static QString number(uint, int base=10);
                    ^
/usr/include/qt5/QtCore/qstring.h:557:20: note:   no known conversion for argument 1 from 'QDate' to 'uint {aka unsigned int}'
/usr/include/qt5/QtCore/qstring.h:558:20: note: static QString QString::number(long int, int)
     static QString number(long, int base=10);
                    ^
/usr/include/qt5/QtCore/qstring.h:558:20: note:   no known conversion for argument 1 from 'QDate' to 'long int'
/usr/include/qt5/QtCore/qstring.h:559:20: note: static QString QString::number(ulong, int)
     static QString number(ulong, int base=10);
                    ^
/usr/include/qt5/QtCore/qstring.h:559:20: note:   no known conversion for argument 1 from 'QDate' to 'ulong {aka long unsigned int}'
/usr/include/qt5/QtCore/qstring.h:560:20: note: static QString QString::number(qlonglong, int)
     static QString number(qlonglong, int base=10);
                    ^
/usr/include/qt5/QtCore/qstring.h:560:20: note:   no known conversion for argument 1 from 'QDate' to 'qlonglong {aka long long int}'
/usr/include/qt5/QtCore/qstring.h:561:20: note: static QString QString::number(qulonglong, int)
     static QString number(qulonglong, int base=10);
                    ^
/usr/include/qt5/QtCore/qstring.h:561:20: note:   no known conversion for argument 1 from 'QDate' to 'qulonglong {aka long long unsigned int}'
/usr/include/qt5/QtCore/qstring.h:562:20: note: static QString QString::number(double, char, int)
     static QString number(double, char f='g', int prec=6);
                    ^
/usr/include/qt5/QtCore/qstring.h:562:20: note:   no known conversion for argument 1 from 'QDate' to 'double'
make: *** [mainwindow.o] Error 1

Answer

vahancho picture vahancho · Nov 2, 2013

I think you need to call:

Core::MessageUser(this->CurrentEdit->User, warning, date.toString(),
                  title, true, dependency);

instead

UPDATE

If you need to print out only the year value:

Core::MessageUser(this->CurrentEdit->User, warning, QString::number(date.year()),
                  title, true, dependency);