【问题标题】:QRubberBand doesn't workQRubberBand 不起作用
【发布时间】:2015-01-28 21:07:30
【问题描述】:

我正在使用 QRubberBand 在我的 QWidget 上绘制一个显示 QImage 的选择框。

我使用的代码与文档中显示的完全相同,但它不起作用。我没有收到任何错误,但它确实表现得很奇怪。当我按住左键并将其拖动到用 QImage 绘制的 QWidget 时,它不会显示选择框,它只会使我的界面上的一个按钮消失并根据我的左键单击重新出现。我还应该补充一点,消失的按钮不是 QWidget 的一部分,甚至不是创建 QWidget 的父 QObject 的一部分。

void Widget::mousePressEvent(QMouseEvent *event)
{
    origin = event->pos();
    if (!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    rubberBand->setGeometry(QRect(origin, QSize()));
    rubberBand->show();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    rubberBand->hide();
    // determine selection, for example using QRect::intersects()
    // and QRect::contains().
}

感谢任何帮助。

谢谢

【问题讨论】:

  • 您能否调试应用程序以确保所有事件都被触发?
  • 在您的情况下,使用 event->pos() 或 event->localPos() 是否正确?

标签: qt


【解决方案1】:

这完全是我的错。我未能在标题中将指针设置为 NULL。

而不是这个:

QRubberBand *rubberBand {rubberBand = NULL};

我刚刚做了:

QRubberBand *rubberBand;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多