1

我在 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);
 }

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

谢谢。

阿什什

4

1 回答 1

1

这是我制作的一个快速示例,您可以在其中QRubberBand使用鼠标事件移动 a:

主窗口.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

主窗口.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;
}
于 2014-06-26T11:50:40.060 回答