[Solved]Save image with dynamic name



  • I'm a beginning "developer" and i'm newbie to qt and c++.
    So, my problem: I've a custom c++ class that save an image to a file(in symbian device), it works perfectly. Now i want to set a dynamic filename for it,with current date and time. I've tried different solutions but i failed. Here's code. Can any one help me to get an easy solution?

    @SaveImage::SaveImage(QObject*parent):
    QObject(parent)
    {
    }
    void SaveImage::save(QDeclarativeItem *item)
    {
    QDateTime dateTime = QDateTime::currentDateTime();
    QString dateTimeString = dateTime.toString();
    QPixmap pix(item->width(), item->height());
    QPainter painter(&pix);
    QStyleOptionGraphicsItem option;
    item->paint(&painter, &option, NULL);
    //i've tried this but doesn't work: pix.save("E:/Images/"image"+dateTimeString+".jpg");
    }@


  • Moderators

    I'm not familiar with symbian. Does it HAVE the concept of named drives ("E:" in your code)?

    What I suggest, is: verify that the chosen path exists. Then, make sure you have write permission in that folder.



  • Oh the class works prefectly using this code: pix.save("E:/Images/"image/image.jpg");
    but my problem is that i want to subsitute the name of the file with current date and time.


  • Moderators

    QDateTime documentation says that default output of ::toString() is "Wed May 20 03:40:13 1998". Does symbian support spaces and colons in folder names? I would specify my own date format, if I were you (see QDateTime docs). Also, verify the path at runtime with qDebug() or by debugging.



  • Thanks, you were right symbian doesn't support colons in file names. So here the working code.
    @SaveImage::SaveImage(QObject*parent):
    QObject(parent)
    {
    }
    void SaveImage::save(QDeclarativeItem *item)
    {
    QDateTime dateTime = QDateTime::currentDateTime();
    QString dateTimeString = dateTime.toString("hh_mm_ss yyyy_MMM_dd");
    QPixmap pix(item->width(), item->height());
    QPainter painter(&pix);
    QStyleOptionGraphicsItem option;
    item->paint(&painter, &option, NULL);
    pix.save("E:/Images/"+dateTimeString+".jpg");
    qDebug() << "Captured on Symbian,here: E:/Images/";
    }@


  • Moderators

    Great!

    Please add "[Solved]" at the beginning of the subject of this thread to make it easier for other people with similar problem in the future.



  • Oh, and thanks for posting the working code, it's always a big help to see a code snippet for a problem and then the solution as well :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.