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. Taking screenshot of a specific/focused window
Qt 6.11 is out! See what's new in the release blog

Taking screenshot of a specific/focused window

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 6.4k Views 2 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.
  • S Offline
    S Offline
    saber
    wrote on last edited by
    #1

    i want get the screen shot of a widget (like listview in my program) and focused window
    i tried this

    ui->label->setPixmap(QPixmap::grabWindow(QApplication::desktop()->hasFocus()));
    
    

    but it did not worked.it garbed whole screen.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      but it did not work

      Because it's totally bonkers. QPixmap::grabWindow expects a window handle (WinId). QApplication::desktop() returns a widget and hasFocus() returns a bool. You're casting a bool to a window handle which makes absolutely no sense. It probably casts to a null handle, which on most platforms means "desktop" so that's what you're getting.

      Apart from that QPixmap::grabWindow is obsolete and you should not use it in new code.

      Here's how to do it correctly:

      auto active_window = qApp->activeWindow();
      if (active_window) //could be null if your app doesn't have focus
      {
          QPixmap pixmap(active_window->size());
          active_window->render(&pixmap);
          ui->label->setPixmap(pixmap);
      }
      
      S 1 Reply Last reply
      4
      • Chris KawaC Chris Kawa

        but it did not work

        Because it's totally bonkers. QPixmap::grabWindow expects a window handle (WinId). QApplication::desktop() returns a widget and hasFocus() returns a bool. You're casting a bool to a window handle which makes absolutely no sense. It probably casts to a null handle, which on most platforms means "desktop" so that's what you're getting.

        Apart from that QPixmap::grabWindow is obsolete and you should not use it in new code.

        Here's how to do it correctly:

        auto active_window = qApp->activeWindow();
        if (active_window) //could be null if your app doesn't have focus
        {
            QPixmap pixmap(active_window->size());
            active_window->render(&pixmap);
            ui->label->setPixmap(pixmap);
        }
        
        S Offline
        S Offline
        saber
        wrote on last edited by
        #3

        @Chris-Kawa
        thanks.it works.
        now it takes screenshot of it's own .
        what about get other focused app's screenshot?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Nothing in Qt for that. It's very platform specific so you'll need to go for the native APIs.

          S 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            Nothing in Qt for that. It's very platform specific so you'll need to go for the native APIs.

            S Offline
            S Offline
            saber
            wrote on last edited by saber
            #5

            @Chris-Kawa
            i am in linux
            i found this .but could not figure it out ,how to get other focused app's screen-shot.
            here

            mrjjM 1 Reply Last reply
            0
            • S saber

              @Chris-Kawa
              i am in linux
              i found this .but could not figure it out ,how to get other focused app's screen-shot.
              here

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @saber
              Hi
              That is for windows.
              If you dont want to get into X 11 programming and the millions windows managers on linux, you
              could just include a cmdline screenshot utility and call it with QProcess
              https://askubuntu.com/questions/194427/what-is-the-terminal-command-to-take-a-screenshot
              shutter is pretty neat.

              If you dont want that, you can look at wmctrl source as it enum windows on various linux distros.
              http://tripie.sweb.cz/utils/wmctrl/#help

              S 1 Reply Last reply
              1
              • mrjjM mrjj

                @saber
                Hi
                That is for windows.
                If you dont want to get into X 11 programming and the millions windows managers on linux, you
                could just include a cmdline screenshot utility and call it with QProcess
                https://askubuntu.com/questions/194427/what-is-the-terminal-command-to-take-a-screenshot
                shutter is pretty neat.

                If you dont want that, you can look at wmctrl source as it enum windows on various linux distros.
                http://tripie.sweb.cz/utils/wmctrl/#help

                S Offline
                S Offline
                saber
                wrote on last edited by
                #7

                @mrjj
                thanks for the suggestion.
                i won't do this.
                i don't want to add a dependencies for that option in my app.

                mrjjM 1 Reply Last reply
                0
                • S saber

                  @mrjj
                  thanks for the suggestion.
                  i won't do this.
                  i don't want to add a dependencies for that option in my app.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @saber
                  ok. fair enough.
                  User can always take shots themselfs.

                  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