-1

I am creating a GUI for windows PC. I want to store a series of images on the PC. The name of the images is identical. But I want to append them with timestamps. So, how to save image using timestamp in Qt? in addition to that, the images shud be saved something like this example: referenceImage<.date.><.time.>jpg where date and time correspond to the date on and time at which the image was saved on the windows PC. I have tried the following too

Here i have implemented this at the click of a push button:-

    void MainWindow::on_generateImagePushButton_clicked()
    {
    QDate date = QDate::currentDate();
    QString dateString = date.toString();
    QString path = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1].jpg").arg(dateString);
    qDebug()<<"path: "<<path;

    /*display current time*/
    QTime time = QTime::currentTime();
    QString timeString = time.toString();
    QString path2 = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1 %2].jpg").arg(dateString).arg(timeString);
    qDebug()<<"path2: "<<path2;

    /*converting from QString to char* */
    QByteArray bA = path2.toLocal8Bit();
    const char *c_charArray = bA.data();


    /*saving image*/
    IplImage *imgWithTimeStamp = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
    cvSaveImage(c_charArray, imgWithTimeStamp);

    }

The image gets saved with dateStamp, ie eg. goldenRefImg[Wed Feb 5 2014].jpg when I use string-path. But when I use string-path2, it does NOT save with dateStamp & timeStamp as i expect it to, i.e. goldenRefImg[Wed Feb 5 2014 10:47:32].jpg But the qDebug statements showing path and path2 are displayed correctly. Application Output:

Starting E:\QT1\timepass-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug\debug\timepass.exe...
path:  "E:\QT1\timeStampTrial\goldenRefImg[Wed Feb 5 2014].jpg" 
path2:  "E:\QT1\timeStampTrial\goldenRefImg[Wed Feb 5 2014 10:47:23].jpg" 

Now i have just recollected that an image cannot be saved with special characters such as the colon : which's there in timeStamp. Can the time format be changed? I tried this way:

path2.replace(":","-");

But the E:\ also gets converted into E-.Please guide. Thank u.

cappy0704
  • 557
  • 2
  • 9
  • 30
  • Qt, not QT. You should show code sample, and describe problem more exacly. – Dmitry Sazonov Feb 03 '14 at 09:54
  • Also, @DmitrySazonov, can u tell me where i was not sufficiently clear? then, I shall amend accordingly. thank you. – cappy0704 Feb 03 '14 at 10:15
  • Read documentation about sprintf arguments. It does not accept Qt types. Such "trick" may be done in next way: `QString path = QString( "E:\\Somdir\\..\\refImg[%1].jpg" ).arg( dateString );`. I recommend you to read some basic books about C/C++ to avoid such questions. And learn how to do step-by-step debugging. – Dmitry Sazonov Feb 03 '14 at 12:16

2 Answers2

6
const QDateTime now = QDateTime::currentDateTime();
const QString timestamp = now.toString(QLatin1String("yyyyMMdd-hhmmsszzz"));
const QString filename = QString::fromLatin1("/some/path/someimage-%1.jpg").arg(timestamp);

This takes the current date/time, converts it to a string using QDateTime::toString() (the documentation lists the formatting options) and constructs the file name out of it. Then just use filename with QImage::save() or QImageWriter.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • Thanks@Frank, i get error about QStringLiteral--that it was not declared in scope. I have included Qstring header, but to no avail. I tried replacing QStringLiteral with QString itself, (forgive me if I make wrong method, I am new), then i get the following error: QString::arg: Argument missing: .jpg, 20140203-1521120 can u help? – cappy0704 Feb 03 '14 at 09:58
  • as u said, i saved using QImage. QImage *image = new QImage(); image->save(filename); this is correct way to save the image, i hope? – cappy0704 Feb 03 '14 at 10:11
  • QStringLiteral is new in Qt 5, I'll adapt to make it work for Qt 4. Your code creates an empty image (which you should create on the stack, not on the heap) and then saves that - so there won't be anything in it. So you need to obtain your image first before you can save it. – Frank Osterfeld Feb 03 '14 at 10:21
  • oh that;d be great. coming to save part, instead of filename, i've inserted a string- – cappy0704 Feb 03 '14 at 10:36
  • As for the argument, you need to add "%1" to your string, in the place where you expect the timestamp. – Frank Osterfeld Feb 03 '14 at 12:20
1
    /*display current date*/
    QDate date = QDate::currentDate();
    QString dateString = date.toString();
    QString path = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1].jpg").arg(dateString);
    qDebug()<<"path: "<<path;

    /*display current time*/
    QTime time = QTime::currentTime();
    QString timeString = time.toString();
    QString path2 = QString("E:\\QT1\\timeStampTrial\\goldenRefImg[%1 %2].jpg").arg(dateString).arg(timeString);
    qDebug()<<"path2: "<<path2;

    path2.replace(":","-");
    path2.replace(1,1,":");
    QByteArray bA = path2.toLocal8Bit();
    const char *c_charArray = bA.data();
    IplImage *imgWithTimeStamp = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
    cvSaveImage(c_charArray, imgWithTimeStamp);

Thank you for all your suggestions @Dmitri Sazonov and @Frank Osterfeld

cappy0704
  • 557
  • 2
  • 9
  • 30