【问题标题】:Line drawing keeps recreating on eachmousemove线条图在每次鼠标移动时不断重新创建
【发布时间】:2022-01-09 19:33:27
【问题描述】:

我正在尝试在 qt 中实现线条绘制。我正在使用两个事件 mouseClickEventmouseMoveEvent,但是我得到了这种不需要的效果,每当我画一条线并移动鼠标时,每次鼠标移动都会重新创建线。有什么办法可以避免这种情况吗?这可以通过操纵图像来完成吗?不允许使用 Qt 画线函数。

void MyWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        x0 = event->x();
        y0 = event->y();
 
    }
    update();
}
void MyWindow::mouseMoveEvent(QMouseEvent *event)
{
    x1 = event->x();
    y1 = event->y();

    unsigned char *image;
    image = img->bits();
    
    ...line drawing algorithm
}
std::vector<std::pair<int, int>> myPair;

void MyWindow::drawPoints(unsigned char *image, int x, int y)
{
    myPair.push_back({x, y});
//Trying to clear previous line
    image[width*4*myPair[b].second + 4*myPair[b].first] = 0;
    image[width*4*myPair[b].second + 4*myPair[b].first + 1] = 0;
    image[width*4*myPair[b].second + 4*myPair[b].first + 2] = 0;
//Below draws white line
    image[width*4*y + 4*x] = 255;
    image[width*4*y + 4*x + 1] = 255;
    image[width*4*y + 4*x + 2] = 255;
}

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    您应该将要绘制的所有对象存储在矢量中。然后,专用的绘图功能可以清除背景并重新绘制您需要/想要的一切。然后所有其他函数将只是添加到向量中或从向量中删除

    【讨论】:

    • 你能检查我编辑的帖子吗?我添加了一个利用 x,y 的函数。但是,在将所有 x,y 存储在向量中后,我不知道如何从那里继续? @infinitezero
    • 它只在我的屏幕上绘制一个点,并且该点使用我上面提供的代码跟随鼠标。
    • @t0xicf 你检查过这个例子了吗? Qt Scribble
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2022-01-18
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多