简体   繁体   English

彩色鼠标移动

[英]Color mouse movement

I am currently working on a GUI Project with Qt C++.我目前正在使用 Qt C++ 进行 GUI 项目。 A part of my work consists of tracking the mouse movement and marking its position with a green pixel.我的一部分工作包括跟踪鼠标移动并用绿色像素标记它的 position。 The tracking should occur only on a certain image, which is stored in a label.跟踪应该只发生在某个图像上,该图像存储在 label 中。

So far, the tracking of the mouse has worked fine and I have been able to extract the coordinates from the QMouseEvent.到目前为止,鼠标的跟踪工作正常,我已经能够从 QMouseEvent 中提取坐标。 However, I seem to have difficulties coloring the pixel the mouse is on.但是,我似乎很难为鼠标所在的像素着色。 What I want is just a colored pixel which marks continuously where the mouse is, but I'm getting all the pixels colored on which the mouse is, and the image doesn't seem to update too.我想要的只是一个彩色像素,它连续标记鼠标所在的位置,但我得到了鼠标所在的所有像素,而且图像似乎也没有更新。 When I walk over, all I get is a black label (the image is wiped out) and ALL the green pixels where I have been with my mouse.当我走过去时,我得到的只是一个黑色的 label(图像被清除)和我用鼠标所在的所有绿色像素。 However it should color ONLY the current pixel ON the existing image, and then it should refresh to the next position.但是它应该只对现有图像上的当前像素着色,然后它应该刷新到下一个 position。 Here is the function I have implemented for this:这是我为此实现的 function:

   QPoint current = event->pos(); 
   QPoint localPos = ui->label_image_2->mapFromParent(current);
   if(ui->label_image_2->rect().contains((localPos))){
       image.setPixel(localPos.x(),localPos.y(), qRgb(0,255,0));
       ui->label_image_2->setPixmap(QPixmap::fromImage(image)); 

When I walk through the image on label_2, the current position ON the image should be marked green.当我浏览 label_2 上的图像时,图像上的当前 position 应标记为绿色。 Am I missing something, or is my general approach wrong?我是否遗漏了什么,或者我的一般方法是错误的?

Where does your image variable come from?您的image变量来自哪里? It doesn't have the correct contents.它没有正确的内容。 You were probably setting some image on the label, but instead you should set that image on the image member variable, and use that to set on the label.您可能在 label 上设置了一些图像,但您应该在image成员变量上设置该图像,并使用在 label 上设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM