可移动的QRubberband从一个点到另一个点

时间:2014-06-25 09:32:44

标签: qt rubber-band qmouseevent

我在QLabel上画了一个QRubberband。我可以使用QSizeGrip调整大小。现在我想使用QMouseevents将它从一个点移动到另一个点。有没有人可以帮助我。

void CropImage::mousePressEvent(QMouseEvent *event)
{
    QLabel::mousePressEvent(event);
    lastPoint = event->pos();
    rubberband = new QRubberBand(QRubberBand::Rectangle,this);
    rubberband->setGeometry(QRect(lastPoint, QSize()));
    rubberband->show();

}

void CropImage::mouseReleaseEvent(QMouseEvent *event)
{
    newPoint = event->pos();
}

这是我的子类部分,用于鼠标事件。代码如下:

Resizable_rubber_band::Resizable_rubber_band(QWidget *parent) : QWidget(parent)
{
 //tell QSizeGrip to resize this widget instead of top-level window
 setWindowFlags(Qt::SubWindow);
 QHBoxLayout* layout = new QHBoxLayout(this);
 layout->setContentsMargins(0, 0, 0, 0);
 QSizeGrip* grip1 = new QSizeGrip(this);
 QSizeGrip* grip2 = new QSizeGrip(this);
 layout->addWidget(grip1, 0, Qt::AlignLeft | Qt::AlignTop);
 layout->addWidget(grip2, 0, Qt::AlignRight | Qt::AlignBottom);
 rubberband = new QRubberBand(QRubberBand::Rectangle, this);
 rubberband->move(0, 0);
 rubberband->show();
}

 void Resizable_rubber_band::resizeEvent(QResizeEvent *)
 {
    rubberband->resize(size());
 }

 void Resizable_rubber_band::mousePressEvent(QMouseEvent *event)
 {
    lastPoint = event->pos();
    rubberband->childAt(lastPoint);
 }

 void Resizable_rubber_band::mouseReleaseEvent(QMouseEvent *event)
 {
   newpoint = event->pos();
   int dragx=newpoint.x()-lastPoint.x();
   int dragy=newpoint.y()-lastPoint.y();
   band->move(0+dragx,0+dragy);
 }

在这段代码中,我的问题是我在拖动后没有得到确切的坐标

感谢。

Ashish

1 个答案:

答案 0 :(得分:1)

以下是我使用鼠标事件移动QRubberBand的快速示例:

<强> mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QRubberBand>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);

private:
    Ui::MainWindow *ui;
    QRubberBand *rubberBand;
    bool move_rubberband;
    QPoint rubberband_offset;
};

#endif // MAINWINDOW_H

<强> mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    move_rubberband = false;
    rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    rubberBand->setGeometry(0,0,50,50);
    rubberBand->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(rubberBand->geometry().contains(e->pos()))
    {
        rubberband_offset = e->pos() - rubberBand->pos();
        move_rubberband = true;
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    if(move_rubberband)
    {
        rubberBand->move(e->pos() - rubberband_offset);
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    move_rubberband = false;
}