我创建了一个标签,并使用setPixmap将.png图像附加到标签上。我还设置了WindowsFlags来禁用标题栏并创建一个无框窗口。因为我已经禁用了它们,它还禁用了拖动任何东西的能力,因此我想创建mouseevents(除非有更好的方法)将我的标签放在屏幕上的任何位置,就像拖动窗口的框架一样。我该怎么办?非常感谢一个例子和简要说明。
答案 0 :(得分:0)
重新实现您需要的QMouseEvent
....比如
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)
}
}
这只是一个非常基本的实现,但如果你计算出正确的运动,它应该会很好:)
答案 1 :(得分: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;
};