Question

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.

Was it helpful?

Solution 2

    /*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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top