大概就是像这个软件这个,尝试过从QWidget::mouseMoveEvent入手,但好像只能在窗体内移动才会触发。
尝试一下的思路,但不太行。
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
int x=event->globalX();
int y=event->globalY();
ui->locationLabel->setText(QString::number(x)+','+QString::number(y));
this->screenPixmap= QPixmap::grabWindow(QApplication::desktop()->winId(),x,y,1,1); //这能获得鼠标当前位置的像素值吗?
ui->label_2->setPixmap(this->screenPixmap);
QColor color=QColor(screenPixmap.toImage().pixel(QPoint(x,y)));
ui->rgbLabel->setText('('+QString::number(color.red())+','+QString::number(color.green())+','+QString::number(color.blue())+')');
}
反应比较迟钝。
当 this->screenPixmap= QPixmap::grabWindow(QApplication::desktop()->winId(),x,y,1,1);
会警告超出范围:QImage::pixel: coordinate (1145,150) out of range。
而 this->screenPixmap= QPixmap::grabWindow(QApplication::desktop()->winId(),x,y,-1,-1);//默认宽度和高度
则不会提示,为什么会这样?