Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Taking screenshot of a specific/focused window

    General and Desktop
    3
    8
    3335
    Loading More Posts
    • 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
      saber last edited by

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 4
        • S
          saber @Chris Kawa last edited by

          @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 Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            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 Reply Quote 0
            • S
              saber @Chris Kawa last edited by 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

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @saber last edited by 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 1 Reply Last reply Reply Quote 1
                • S
                  saber @mrjj last edited by

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

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @saber last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post