Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How can I used the same thread without having to create a new one ??

[SOLVED] How can I used the same thread without having to create a new one ??

Scheduled Pinned Locked Moved General and Desktop
qthreadmemorycleanup
12 Posts 2 Posters 4.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koahnig
    wrote on 23 Jun 2015, 16:20 last edited by
    #2

    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?

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SujaMM
      wrote on 23 Jun 2015, 18:14 last edited by
      #3

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on 23 Jun 2015, 18:33 last edited by
        #4

        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.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SujaMM
          wrote on 23 Jun 2015, 19:19 last edited by
          #5

          No, I'm ussing Qt 5.

          Why did you think I was ussing Qt 4.8 ??

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SujaMM
            wrote on 23 Jun 2015, 20:39 last edited by
            #6

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

            Thanks.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on 24 Jun 2015, 07:26 last edited by
              #7

              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.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SujaMM
                wrote on 24 Jun 2015, 13:34 last edited by
                #8

                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.

                K 1 Reply Last reply 24 Jun 2015, 13:41
                0
                • S SujaMM
                  24 Jun 2015, 13:34

                  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.

                  K Offline
                  K Offline
                  koahnig
                  wrote on 24 Jun 2015, 13:41 last edited by
                  #9

                  @SujaMM
                  Checkout the Qt5 screenshot example.
                  It uses QGuiApplication::primaryScreen() to take the screenshot. pimaryScreen There is also screens() and other stuff.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SujaMM
                    wrote on 24 Jun 2015, 15:09 last edited by
                    #10

                    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.

                    K 1 Reply Last reply 24 Jun 2015, 15:38
                    0
                    • S SujaMM
                      24 Jun 2015, 15:09

                      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.

                      K Offline
                      K Offline
                      koahnig
                      wrote on 24 Jun 2015, 15:38 last edited by
                      #11

                      @SujaMM said:

                         screenshot = screen->grabWindow(0);
                      

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

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SujaMM
                        wrote on 25 Jun 2015, 13:20 last edited by
                        #12

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

                        1 Reply Last reply
                        0

                        11/12

                        24 Jun 2015, 15:38

                        • Login

                        • Login or register to search.
                        11 out of 12
                        • First post
                          11/12
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved