Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 从一点到另一点的可移动QRubberband_Qt_Rubber Band_Qmouseevent - Fatal编程技术网

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

Qt 从一点到另一点的可移动QRubberband,qt,rubber-band,qmouseevent,Qt,Rubber Band,Qmouseevent,我在QLabel上画了一个QRubberband。我可以使用QSizeGrip调整它的大小。现在我想使用QMouseevents将它从一个点移动到另一个点。有人能帮我吗 void CropImage::mousePressEvent(QMouseEvent *event) { QLabel::mousePressEvent(event); lastPoint = event->pos(); rubberband = new QRubberBand(QRubberBa

我在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

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

main window.h

\ifndef主窗口
#定义主窗口
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
受保护的:
无效鼠标压力事件(QMouseEvent*e);
void mouseMoveEvent(QMouseEvent*e);
无效鼠标事件(QMouseEvent*e);
私人:
Ui::MainWindow*Ui;
QRubberBand*橡胶带;
布尔移动_橡胶带;
QPoint rubberband_偏移;
};
#endif//main窗口
main window.cpp

#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
move_rubberband=false;
rubberBand=新的QRubberBand(QRubberBand::Rectangle,this);
橡胶垫->设置几何体(0,0,50,50);
橡皮键->显示();
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void主窗口::MousePresseEvent(QMouseEvent*e)
{
如果(橡胶垫->几何体()。包含(e->位置())
{
橡胶带偏移量=e->pos()-橡胶带->pos();
move_rubberband=真;
}
}
void主窗口::mouseMoveEvent(QMouseEvent*e)
{
如果(移动橡胶带)
{
橡胶垫->移动(e->位置()-橡胶垫\U偏移);
}
}
void主窗口::mouseReleaseEvent(QMouseEvent*e)
{
move_rubberband=false;
}

详细描述问题所在会有很大帮助。我想用mousepressevent移动橡胶带。我怎么做?我怎么知道MouseeEvent在rubberband上或应用程序的其他地方?请详细说明。谢谢Thuga,我已经试过了。它工作正常,但当我尝试用鼠标拖动橡皮筋时,它会删除我的橡皮筋。我不能拖动我的橡皮筋。嘿,图加,我已经编辑了我的查询。现在,您可以检查我的代码,并告诉我如何获得准确的Cordinate。但是,我无法使用我使用QSizeGrips的上部代码正确调整其大小。@Ashish“无法正确调整其大小”是什么意思?发生了什么?当我试图将橡皮筋从一点拖到另一点时,只有橡皮筋在移动,但QSizeGrip没有移动(固定)。@Ashish Well当然。如果你不移动它们,为什么你希望它们移动?可以单独移动它们,也可以创建包含橡皮筋和大小夹点的小部件。
#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
#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;
}