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. How to grab current qt application Window
Forum Updated to NodeBB v4.3 + New Features

How to grab current qt application Window

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 5.7k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jhovarie
    wrote on last edited by
    #1

    Hello guys I am trying to screenshot my own qt application window.
    I tried this code but I know there are some better way.

    void Test::capture(const QString &path) const
    {
        QPixmap originalPixmap;
        QScreen *screen = QGuiApplication::primaryScreen();
        int y = 100;
        int w = 1600;
        int h = 450;
        originalPixmap = screen->grabWindow(0,x,y,w,h);
        originalPixmap.save(path);
    }
    

    is there another way to screenshot or grab current qt application?

    jsulmJ 1 Reply Last reply
    0
    • J jhovarie

      Hello guys I am trying to screenshot my own qt application window.
      I tried this code but I know there are some better way.

      void Test::capture(const QString &path) const
      {
          QPixmap originalPixmap;
          QScreen *screen = QGuiApplication::primaryScreen();
          int y = 100;
          int w = 1600;
          int h = 450;
          originalPixmap = screen->grabWindow(0,x,y,w,h);
          originalPixmap.save(path);
      }
      

      is there another way to screenshot or grab current qt application?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @jhovarie Use http://doc.qt.io/qt-5/qwidget.html#winId to get WId and don't pass x, y, w, h to grab the whole window.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jhovarie
        wrote on last edited by
        #3

        How to use it? sorry I am a beginner

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          void Test::capture(const QString &path) const
          {
          QWidget* parentWid = this; // Current widget
          while(parentWid->parent()) parentWid = parentWid->parent(); // climb up utill you reach the widget that's the parent of everything
              parentWid->grab().save(path); // save it
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          JonBJ D 2 Replies Last reply
          1
          • VRoninV VRonin
            void Test::capture(const QString &path) const
            {
            QWidget* parentWid = this; // Current widget
            while(parentWid->parent()) parentWid = parentWid->parent(); // climb up utill you reach the widget that's the parent of everything
                parentWid->grab().save(path); // save it
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @VRonin
            Your method relies on starting from a widget. To find the app's main window I use this standalone approach:

            def findMainWindow() -> typing.Union[QMainWindow, None]:
                # Global function to find the (open) QMainWindow in application
                app = QApplication.instance()
                for widget in app.topLevelWidgets():
                    if isinstance(widget, QMainWindow):
                        return widget
                return None
            

            As I respect your expertise, if you have any comment on this it would be welcome?

            1 Reply Last reply
            2
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              As @JonB suggested, (i'm just translating in C++ are removing the QMainWindow assumption), if Test is not a widget inside the area you want to grab

              const QWidgetList topWidg = QApplication::topLevelWidgets();
              for(int i=0;i<topWidg.size();++i){
              widget->grab().save("GrabImage" + QString::number(i+1) + ".png");
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • VRoninV VRonin
                void Test::capture(const QString &path) const
                {
                QWidget* parentWid = this; // Current widget
                while(parentWid->parent()) parentWid = parentWid->parent(); // climb up utill you reach the widget that's the parent of everything
                    parentWid->grab().save(path); // save it
                }
                
                D Offline
                D Offline
                DungeonLords
                wrote on last edited by DungeonLords
                #7

                @VRonin said in How to grab current qt application Window:

                parentWid

                Your code doesn't work on Qt 5.15 (but thanks for your job). I fix it.

                QWidget* parentWid = this; // Current widget
                while(parentWid->parent()) parentWid = parentWid->parentWidget(); // climb up utill you reach the widget that's the parent of everything
                    parentWid->grab().save("path.png"); // save it
                
                1 Reply Last reply
                0

                • Login

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