[SOLVED] How can I used the same thread without having to create a new one ??
-
Hello,
I have a Thread that takes a screenshot, I call this Thread every time an action happens, so I nedd to create a new Thread everytime I need to take a screenshot.
I'm doing it saying QThread Thread = new QThread ();
but this it's making my program to consum to much memoria every time I create a thread my memory it's up t 4mb.
How can I start a thread in various ocassion or how can I delete the memory ??
I try with deletelater or ~Qthread dosen't seems to clear the memory
-
Probably you should use a QThreadPool
In the detailed description you can read:
QThreadPool manages and recyles individual QThread objects to help reduce thread creation costs in programs that use threads. Each Qt application has one global QThreadPool object, which can be accessed by calling globalInstance().That is the only help I can offer at the moment.
However, 4 mb is a typical size of a picture. Therefore, I am not sure if you are actually having a problem with creating a new thread each time. It might be that you create an image of approximately this size and your are having a memory leak with the picture.
Do you delete the image correctly? -
I'm not sure if I'm even deleting the image correctly.
QDesktopWidget* dw = QApplication::desktop(); QPixmap pixmap = QPixmap::grabWindow( dw->winId(), 0, 0, dw->width(), dw->height() ); QDir mDir(QDir::homePath()); QString format = "jpg"; QString filename = "/perfq_" + QString::number(QDate::currentDate().year())+ "_" +QString::number(QDate::currentDate().month())+ "_" +QString::number(QDate::currentDate().day())+ "_" +QString::number(QTime::currentTime().hour())+ "_" +QString::number(QTime::currentTime().minute())+ "_" +QString::number(QTime::currentTime().second()); QString mPath = QDir::homePath() + "/perfq_id_" + QString::number(this->idu); if (!mDir.exists(mPath)) mDir.mkpath(mPath); pixmap.save(mPath+filename+".jpg", format.toLatin1().constData());
however how can I use the QThreadPool ??
-
You are using Qt 4.8. The links I had provided are for Qt 5. grabWindow is not defined for QPixMap in Qt 5.
Here is the link for the proper Qt 4.8 QThreadPool documentation.I though that you might allocate memory for the picture and do not release properly. I cannot see a reason either.
With QThreadPool I have to stay out of discussion. Probably I do not know more about threading than you do.
-
Good to know that you have fixed your problem.
My conclusion was that you are using Qt4 since you use grabWindow which I could not find in Qt 5 documentation.
However, grabWindow and grabWidget are marked as obsolete in Qt 5. Apparently, those are still working, but this may be something to be changed, since there is no real support anymore. -
@SujaMM
Checkout the Qt5 screenshot example.
It usesQGuiApplication::primaryScreen()
to take the screenshot. pimaryScreen There is also screens() and other stuff. -
I try this before I use this code:
QApplication a(argv, argc); QScreen *screen = a.primaryScreen(); QPixmap screenshot = screen->grabWindow(0); screenshot.save('screenshot.png', 'png'); QList<QScreen*> screens = a.screens(); QScreen *screen; QPixmap screenshot; for(int i = 0; i < screens.length(); i++){ screen = screens.at(i); screenshot = screen->grabWindow(0); screenshot.save(QString::number(i) + ".png", 'png'); }
but it will take various screenshots of the primary screen.