Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-30284

QPixmap::grabWindow does not work on anything other than the primary screen

XMLWordPrintable

      The following snippet creates small window painted with the pixel color under the mouse. Click and drag in the widget and then position the mouse over any item on the desktop.

      On a Windows 7 system with two monitors, this works on both the primary and secondary monitor.

      On Mac OSX 10.8, it only works on the primary monitor. On the secondary display, the color returned is black.

      #include <QtGui>
      
      class Widget : public QWidget
      {
      public:
          Widget(QWidget *parent = 0) : QWidget(parent)
          {
              resize(100, 100);
          }
      
          void paintEvent(QPaintEvent *)
          {
              QPainter p(this);
      
              p.fillRect(rect(), m_color);
          }
      
          void mouseMoveEvent(QMouseEvent *evt)
          {
              m_color = ColorUnderPos(QCursor::pos());
      
              update();
          }
      
          QColor ColorUnderPos(const QPoint &globalPt)
          {
              QColor color;
      
              const int screenNo = qApp->desktop()->screenNumber(globalPt);
              QWidget *desktopWidget = qApp->desktop()->screen(screenNo);
      
              const QImage screen = QPixmap::grabWindow(
                  desktopWidget->winId(),
                  globalPt.x(),
                  globalPt.y(),
                  1,
                  1).toImage();
      
              const QRgb c = screen.pixel(QPoint(0, 0));
              color.setRgb(qRed(c), qGreen(c), qBlue(c));
      
              return color;
          }
      
          QColor m_color;
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          Widget w;
          w.show();
      
          return a.exec();
      }
      

        For Gerrit Dashboard: QTBUG-30284
        # Subject Branch Project Status CR V

            sorvig Morten Sørvig
            danny77uk Daniel
            Votes:
            2 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes