Skip to content

Commit 39f9724

Browse files
Merge pull request #10 from seequent/LF-34205-mouse-colour-selection-wrong-on-extended-monitor
LF-34205 Fix scaling error in QColorDialog colour picker
2 parents aefd71c + 664eb36 commit 39f9724

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/widgets/dialogs/qcolordialog.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class QColorDialogPrivate : public QDialogPrivate
120120
void setCurrentQColor(const QColor &color);
121121
bool selectColor(const QColor &color);
122122
QColor grabScreenColor(const QPoint &p);
123+
QColor grabScreenColor(const QPoint &p, QScreen *screen);
123124

124125
int currentAlpha() const;
125126
void setCurrentAlpha(int a);
@@ -1571,8 +1572,30 @@ bool QColorDialogPrivate::selectColor(const QColor &col)
15711572

15721573
QColor QColorDialogPrivate::grabScreenColor(const QPoint &p)
15731574
{
1574-
const QDesktopWidget *desktop = QApplication::desktop();
1575-
const QPixmap pixmap = QGuiApplication::primaryScreen()->grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
1575+
// Get the screen at p if it exists.
1576+
QScreen *screen = QGuiApplication::screenAt(p);
1577+
QScreen *primaryScreen = QGuiApplication::primaryScreen();
1578+
1579+
if (!screen || screen == primaryScreen)
1580+
// If no screen found then use primary. Origin is (0,0) so no need to shift.
1581+
return grabScreenColor(p, primaryScreen);
1582+
1583+
// Get the origin of the current screen
1584+
const QPoint origin = screen->geometry().topLeft();
1585+
1586+
// Convert the logical global point to a logical screen point.
1587+
const QPoint screenPos = p - origin;
1588+
1589+
// Use the shifted point to return the screen color
1590+
return grabScreenColor(screenPos, screen);
1591+
}
1592+
1593+
// Get the pixel color at the specified point p on the specified screen.
1594+
// Use screen coordinates rather than global to get around scaling issues.
1595+
QColor QColorDialogPrivate::grabScreenColor(const QPoint &p, QScreen *screen)
1596+
{
1597+
// Get a 1x1 pixmap from the screen, id=0 refers to the screen.
1598+
const QPixmap pixmap = screen->grabWindow(0, p.x(), p.y(), 1, 1);
15761599
QImage i = pixmap.toImage();
15771600
return i.pixel(0, 0);
15781601
}

0 commit comments

Comments
 (0)