嗨我需要在Qlabel上绘制一个矩形,作为我尝试获取鼠标事件的第一步。使用下面的代码调用事件,但即使我改变了鼠标位置,坐标总是相同的。
此外,我只需要在Qlabel上设置鼠标跟踪,除鼠标释放外,它的工作正常,当鼠标在Qlabel外部释放时,函数mouseReleaseEvent
会被调用。
另请参阅以下代码中的注释,
ImageEditer::ImageEditer(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImageEditer)
{
ui->setupUi(this);
ui->label_image->setMouseTracking(true); // need to set the mouse tracking over this label
}
void ImageEditer::mouseMoveEvent(QMouseEvent* event)
{
qDebug() << "Mouse move.." << pos() << "---> " << x() << "," << y(); // these printing same values
}
void ImageEditer::mousePressEvent(QMouseEvent* event)
{
qDebug() << "Mouse press.." << pos() << "---> " << x() << "," << y(); // these printing same values
}
// This getting called when release the mouse outside the label_image
void ImageEditer::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "Mouse release.." << pos() << "---> " << x() << "," << y(); // these printing same values
}
答案 0 :(得分:4)
您正在打印小部件的pos(),x(),y()。 只需将其更改为event-&gt; pos(),event-&gt; pos()。x(),event-&gt; pos()。y()