1.概要
2.代码
在Qt中,如果你想要创建一个矩形,并且仅允许通过拖拽其左侧边线来改变宽度,你可以通过重写QGraphicsRectItem
类来实现。以下是一个简单的例子,展示了如何创建一个自定义的ResizableRectItem
类,该类允许用户通过拖拽矩形的左侧边线来改变其宽度:
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QPointF>
#include <QSizeF>
#include <QRectF>
#include <QGraphicsSceneMouseEvent>
class ResizableRectItem : public QGraphicsRectItem {
public:
ResizableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
: QGraphicsRectItem(rect, parent),
dragging(false),
dragStartWidth(0.0),
dragStartPos(QPointF()) {}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
QRectF rect = this->rect();
QPointF pos = event->pos();
const int edgeSensitivity = 5; // Pixels
if (qAbs(pos.x() - rect.x()) < edgeSensitivity) {
// Left edge
dragging = true;
dragStartWidth = rect.width();
dragStartPos = pos;
}
}
//QGraphicsRectItem::mousePressEvent(event);
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
if (dragging) {
QPointF pos = event->pos();
qreal newWidth = dragStartWidth - (dragStartPos.x() - pos.x());
this->setRect(QRectF(pos.x(), this->rect().y(), newWidth, this->rect().height()));
}
//QGraphicsRectItem::mouseMoveEvent(event);
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
dragging = false;
QGraphicsRectItem::mouseReleaseEvent(event);
}
private:
bool dragging;
qreal dragStartWidth;
QPointF dragStartPos;
};
class MainWindow : public QGraphicsView {
public:
MainWindow(QWidget *parent = nullptr) : QGraphicsView(parent) {
QGraphicsScene *scene = new QGraphicsScene(this);
this->setScene(scene);
ResizableRectItem *rect = new ResizableRectItem(QRectF(10, 10, 200, 100));
scene->addItem(rect);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow window;
window.resize(400, 300);
window.show();
return app.exec();
}
在这个例子中,ResizableRectItem
类重写了mousePressEvent
、mouseMoveEvent
和mouseReleaseEvent
方法。当用户点击矩形的左侧边线时,mousePressEvent
会设置拖拽状态,并记录拖拽开始时的宽度和位置。当用户移动鼠标时,mouseMoveEvent
会根据鼠标的新位置更新矩形的宽度。最后,当用户释放鼠标按钮时,mouseReleaseEvent
会结束拖拽状态。
代码2
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
//https://blog.csdn.net/xie__jin__cheng/article/details/140438674?spm=1001.2014.3001.5501
//qt 创建一个矩形,矩形的边线可以拖拽
class DraggableRectItem : public QGraphicsRectItem {
public:
DraggableRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
: QGraphicsRectItem(rect, parent),
dragging(false) {}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
dragging = true;
lastPos = event->pos();
}
//QGraphicsRectItem::mousePressEvent(event);
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
if (dragging) {
QRectF rect = this->rect();
QPointF diff = event->pos() - lastPos;
rect.translate(diff);
this->setRect(rect);
lastPos = event->pos();
}
//QGraphicsRectItem::mouseMoveEvent(event);
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
dragging = false;
//QGraphicsRectItem::mouseReleaseEvent(event);
}
private:
bool dragging;
QPointF lastPos;
};
class MainWindow : public QMainWindow {
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
auto *scene = new QGraphicsScene(this);
auto *view = new QGraphicsView(scene, this);
setCentralWidget(view);
auto *rect = new DraggableRectItem(QRectF(10, 10, 200, 100));
scene->addItem(rect);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow window;
window.resize(400, 300);
window.show();
return app.exec();
}