我创建了一个标签并使用 setPixmap 将 .png 图像附加到标签上。我还设置了 WindowFlags 来禁用标题栏并创建一个无框窗口。因为我已经禁用了这些,它也禁用了拖动任何东西的能力,所以我想创建鼠标事件(除非有更好的方法)来将我的标签定位在屏幕上的任何位置,就像拖动窗口的框架一样。我该怎么做?一个例子和简短的解释将不胜感激。
问问题
89 次
2 回答
0
重新实现QMouseEvent
你需要的 s .... 喜欢
void MyLabel::mousePressEvent(QMouseEvent* e)
{
m_moveDatWidget = true;
// so when the mousebutton got pressed, you set something to
// tell your code to move the widget ... consider maybe you
// want to move it only on right button pressed ...
}
void MyLabel::mouseReleaseEvent(QMouseEvent* e)
{
m_moveDatWidget = false;
// after releasing do not forget to reset the movement variable
}
void MyLabel::mouseMoveEvent(QMouseEvent* e)
{
// when in 'moving state' ...
if (m_moveDatWidget)
{
// move your widget around using QWidget::move(qreal,qreal)
}
}
这只是一个非常基本的实现,但如果您正确计算所需的运动,它应该会做得很好:)
于 2013-11-02T11:18:47.417 回答
0
我将通过以下方式实现鼠标拖动标签:
class Label : public QLabel
{
public:
// Implement the constructor(s)
protected:
void Label::mouseMoveEvent(QMouseEvent* event)
{
if (!m_offset.isNull()) {
move(event->globalPos() - m_offset);
}
QLabel::mouseMoveEvent(event);
}
void Label::mousePressEvent(QMouseEvent* event)
{
// Get the mouse offset in the label's coordinates system.
m_offset = event->globalPos() - pos();
QLabel::mousePressEvent(event);
}
void Notifier::mouseReleaseEvent(QMouseEvent* event)
{
m_offset = QPoint();
QLabel::mouseReleaseEvent(event);
}
private:
// The mouse pointer offset from the top left corner of the label.
QPoint m_offset;
};
于 2013-11-02T12:20:44.833 回答