java - 获取 OnClick Pixmap 坐标 Java/QT/QT Jambi

标签 java qt qtgui qpixmap qt-jambi

我是一名新的 java/qt 程序员,我正在尝试使用 java 和 qt jambi 构建一个 gui。

我在标签内放置了一个像素图,效果很好,但是,现在我需要在单击标签内的像素图时获取图像坐标。我不需要 QWidget 坐标。我只需要像素图坐标来处理单击点上的图像。

我想我应该覆盖 mousePressEvent 事件,但我不知道如何将它附加到“pixmap onclick”。

你能给我一个简单的例子吗?

我希望我已经以正确的方式解释了我的问题,我的英语不是很好,抱歉!

最佳答案

i think i should override the mousePressEvent event, but i don't know how to attach it to "pixmap onclick".

您似乎将事件和信号混淆在一起。前者是一个QEvent,后者是一个信号。你需要的是前者,不需要后者。

在编写 C++(不是 Java 中的 Qt Jambi,但您可以将其作为伪代码)时,您需要子类化 QLabel 类并编写类似的内容。

#include <QLabel>
#include <QPoint>
#include <QMouseEvent>

class MyLabel : public QLabel
{
    Q_OBJECT
    public:
        MyLabel(QObject *parent) : QLabel(parent)
    
     ...

     protected:
         void QWidget::mousePressEvent(QMouseEvent * event)
         {
             ...
             QPoint localPosition = event.pos();
             // Work on the desired point
             ...
         }
};

因此,根据您的用例,您需要查看这些方法的文档:

void QWidget::mousePressEvent(QMouseEvent * event) [virtual protected]

This event handler, for event event, can be reimplemented in a subclass to receive mouse press events for the widget.

If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

然后这是获取本地位置的方法,它也适用于 Qt 4。如果您需要更精确的浮点等,您可能希望考虑 localPos() 或 Qt 5 中引入的其他方法。

QPoint QMouseEvent::pos() const

Returns the position of the mouse cursor, relative to the widget that received the event.

If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.

关于java - 获取 OnClick Pixmap 坐标 Java/QT/QT Jambi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21065528/

相关文章:

java - 从另一个文件夹运行 Java

java - 基于键的Map排序

c++ - 链接器错误 LNK2038 : mismatch detected for 'RuntimeLibrary'

c++ - 问题子类化 Qlabel C++

python - QStackedLayout 中的 QScroll 区域

java - Oracle 序列不会从 Java/Hibernate 递增

c++ - 你怎么知道 QMutex 是否被锁定?

qt - 使用 QtFFmpegWrapper+QLabel+QTimer 是在 Qt 应用程序中播放视频的好方法吗?

python - PyQt4 中 GridLayout 中的对齐

java服务与资源共享