Qt World Summit: Register Today!

[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 mPath = QDir::homePath() + "/perfq_id_" + QString::number(this->idu);
           if (!mDir.exists(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.

  • No, I'm ussing Qt 5.

    Why did you think I was ussing Qt 4.8 ??

  • I fix the problem I so I didn't have to create a new thread every time, instate I just call the start function.


  • 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.

  • I didn't know this.

    it's there another way to take screenshots ussing non obsolete libraries in Qt5??

    Because this was the only way It works with multiple-monitors.

  • @SujaMM
    Checkout the Qt5 screenshot example.
    It uses QGuiApplication::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.

  • @SujaMM said:

       screenshot = screen->grabWindow(0);

    The reason is probably that you are using the same windows identifier on each call.

  • Oooh, should I use i insted of 0 in this line screenshot = screen->grabWindow(0); ??

Log in to reply