July 2, 2012

daljit97 daljit97
Ant Farmer
68 posts

[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?

  1. SaveImage::SaveImage(QObject*parent):
  2.     QObject(parent)
  3. {
  4. }
  5. void SaveImage::save(QDeclarativeItem *item)
  6. {
  7.     QDateTime dateTime = QDateTime::currentDateTime();
  8.     QString dateTimeString = dateTime.toString();
  9.     QPixmap pix(item->width(), item->height());
  10.     QPainter painter(&pix);
  11.     item->paint(&painter, &option, NULL);
  12.     //i've tried this but doesn't work: pix.save("E:/Images/"image"+dateTimeString+".jpg");
  13. }

6 replies

July 2, 2012

sierdzio sierdzio
Area 51 Engineer
2323 posts

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.

 Signature 

(Z(:^

July 2, 2012

daljit97 daljit97
Ant Farmer
68 posts

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.

July 3, 2012

sierdzio sierdzio
Area 51 Engineer
2323 posts

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.

 Signature 

(Z(:^

July 3, 2012

daljit97 daljit97
Ant Farmer
68 posts

Thanks, you were right symbian doesn’t support colons in file names. So here the working code.

  1. SaveImage::SaveImage(QObject*parent):
  2.     QObject(parent)
  3. {
  4. }
  5. void SaveImage::save(QDeclarativeItem *item)
  6. {
  7.     QDateTime dateTime = QDateTime::currentDateTime();
  8.     QString dateTimeString = dateTime.toString("hh_mm_ss yyyy_MMM_dd");
  9.     QPixmap pix(item->width(), item->height());
  10.     QPainter painter(&pix);
  11.     item->paint(&painter, &option, NULL);
  12.     pix.save("E:/Images/"+dateTimeString+".jpg");
  13.     qDebug() << "Captured on Symbian,here: E:/Images/";
  14. }

July 3, 2012

sierdzio sierdzio
Area 51 Engineer
2323 posts

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.

 Signature 

(Z(:^

July 3, 2012

CreMindES CreMindES
Robot Herder
208 posts

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 :)

 
  ‹‹ Windows Mobile 5 - Maximized window      Basic Program unexpectedly finished ››

You must log in to post a reply. Not a member yet? Register here!