소스 검색

修改按钮移动方式,可实现移动拖拽

main
email 5 일 전
부모
커밋
6ffd08b804
5개의 변경된 파일180개의 추가작업 그리고 29개의 파일을 삭제
  1. +5
    -5
      IntegratedPlatform/IntegratedPlatform.pro.user
  2. +90
    -0
      IntegratedPlatform/hmiwidget.cpp
  3. +46
    -0
      IntegratedPlatform/hmiwidget.h
  4. +37
    -22
      IntegratedPlatform/mainwindow.cpp
  5. +2
    -2
      IntegratedPlatform/mainwindow.ui

+ 5
- 5
IntegratedPlatform/IntegratedPlatform.pro.user 파일 보기

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2025-08-05T09:23:15. -->
<!-- Written by QtCreator 4.11.1, 2025-08-05T21:07:14. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -287,19 +287,19 @@
</valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.CustomExecutableRunConfiguration.Executable"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/admin/Desktop/two/IntegratedPlatform/IntegratedPlatform.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/admin/Desktop/two/IntegratedPlatform/IntegratedPlatform.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value>
<value type="bool" key="RunConfiguration.Arguments.multi">false</value>
<value type="QString" key="RunConfiguration.OverrideDebuggerStartup"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseMultiProcess">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/admin/Desktop/build-IntegratedPlatform-Desktop_Qt_5_14_2_MinGW_32_bit-Debug</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>


+ 90
- 0
IntegratedPlatform/hmiwidget.cpp 파일 보기

@@ -0,0 +1,90 @@
#include "hmiwidget.h"
#include <QGraphicsSceneMouseEvent>
#include <QCursor>

ResizableRectangle::ResizableRectangle(qreal x, qreal y, qreal w, qreal h)
: QGraphicsRectItem(x, y, w, h), resizing(false)
{
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setCursor(Qt::ArrowCursor);
}

void ResizableRectangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// 检查是否在调整区域
if (rect().bottomRight().x() - 10 <= event->pos().x() &&
rect().bottomRight().y() - 10 <= event->pos().y()) {
resizing = true;
setCursor(Qt::SizeFDiagCursor);
} else {
// 传递事件给基类处理移动
resizing = false;
setCursor(Qt::SizeAllCursor);
QGraphicsRectItem::mousePressEvent(event);
}
}

void ResizableRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (resizing) {
// 调整大小逻辑
QRectF newRect = rect();
newRect.setBottomRight(event->pos());
setRect(newRect);
} else {
// 传递事件给基类处理移动
QGraphicsRectItem::mouseMoveEvent(event);
}
}

void ResizableRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
resizing = false;
setCursor(Qt::ArrowCursor);
QGraphicsRectItem::mouseReleaseEvent(event);
}

// -------------------- ResizableEllipse --------------------

ResizableEllipse::ResizableEllipse(qreal x, qreal y, qreal w, qreal h)
: QGraphicsEllipseItem(x, y, w, h), resizing2(false)
{
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setCursor(Qt::ArrowCursor);
}

void ResizableEllipse::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QRectF ellipseRect = rect();
if (ellipseRect.bottomRight().x() - 10 <= event->pos().x() &&
ellipseRect.bottomRight().y() - 10 <= event->pos().y()) {
resizing2 = true;
setCursor(Qt::SizeFDiagCursor);
} else {
// 传递事件给基类处理移动
resizing2 = false;
setCursor(Qt::SizeAllCursor);
QGraphicsEllipseItem::mousePressEvent(event);
}
}

void ResizableEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (resizing2) {
QRectF newRect = rect();
newRect.setBottomRight(event->pos());
setRect(newRect);
} else {
// 传递事件给基类处理移动
QGraphicsEllipseItem::mouseMoveEvent(event);
}
}

void ResizableEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
resizing2 = false;
setCursor(Qt::ArrowCursor);
QGraphicsEllipseItem::mouseReleaseEvent(event);
}

+ 46
- 0
IntegratedPlatform/hmiwidget.h 파일 보기

@@ -1,4 +1,50 @@
#ifndef HMIELEMENT_H
#define HMIELEMENT_H
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsSceneMouseEvent>
#include <QPen>
#include <QBrush>

// 自定义矩形类,支持大小调整
class ResizableRectangle : public QGraphicsRectItem
{
public:
// 构造函数
ResizableRectangle(qreal x, qreal y, qreal w, qreal h);

protected:
// 鼠标按下事件
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

// 鼠标移动事件
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

// 鼠标释放事件
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

private:
bool resizing; //是否正在调整大小
};

// 自定义圆形类,支持大小调整
class ResizableEllipse : public QGraphicsEllipseItem
{
public:
// 构造函数
ResizableEllipse(qreal x, qreal y, qreal w, qreal h);

protected:
// 鼠标按下事件
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

// 鼠标移动事件
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

// 鼠标释放事件
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

private:
bool resizing2; // 是否正在调整大小
};
#endif // HMIELEMENT_H

+ 37
- 22
IntegratedPlatform/mainwindow.cpp 파일 보기

@@ -1,5 +1,6 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"hmiwidget.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@@ -10,14 +11,13 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView_2->setScene(scene);

connect(ui->treeWidget_2, &QTreeWidget::itemClicked, this, &MainWindow::onHmiItemClicked);
ui-> graphicsView_2->viewport()->installEventFilter(this);
addDockWidget(Qt::BottomDockWidgetArea, ui->dockWidget_3);//将dockWidget_3添加到主窗口的下方

QList<QTreeWidgetItem *> items = ui->treeWidget_2->findItems("指示灯", Qt::MatchExactly);
for (QTreeWidgetItem *item : items) {
item->setIcon(0, QIcon("C:/Users/admin/Desktop/T-常开触点-01.png"));
}
addDockWidget(Qt::BottomDockWidgetArea, ui->dockWidget_3);//将dockWidget_3添加到主窗口的下方
ui->treeWidget_2->topLevelItem(0)->setIcon(0, QIcon("C:/Users/admin/Desktop/灯泡.png"));
ui->treeWidget_2->topLevelItem(1)->setIcon(0, QIcon("C:/Users/admin/Desktop/按钮.png"));
}

MainWindow::~MainWindow()
@@ -25,7 +25,8 @@ MainWindow::~MainWindow()
delete ui;
}

void MainWindow::onHmiItemClicked(QTreeWidgetItem *item){
void MainWindow::onHmiItemClicked(QTreeWidgetItem *item)
{
if (item->text(0) == "指示灯")
{
canDrawCircle = true;
@@ -43,29 +44,43 @@ void MainWindow::onHmiItemClicked(QTreeWidgetItem *item){
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->graphicsView_2->viewport() && event->type() == QEvent::MouseButtonPress) {
// 修正1:使用一致的视图对象名(graphicsView_2)
if (obj != ui->graphicsView_2->viewport()) {
return false; // 不是我们关心的对象,不处理
}
// 只处理鼠标按下事件
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::LeftButton) {
// 获取点击的场景坐标
if (canDrawCircle)
{
QPointF scenePos = ui->graphicsView_2->mapToScene(mouseEvent->pos());// 在点击位置绘制一个指示灯
scene->addEllipse(scenePos.x(), scenePos.y(), 50, 50, QPen(Qt::black), QBrush(Qt::blue));
}
else if (canDrawRectangle)
{
QPointF scenePos = ui->graphicsView_2->mapToScene(mouseEvent->pos());//在点击位置绘制一个按钮
scene->addRect(scenePos.x(), scenePos.y(), 50, 50, QPen(Qt::black), QBrush(Qt::blue));
QPointF scenePos = ui->graphicsView_2->mapToScene(mouseEvent->pos());

// 根据标志绘制元素
if (canDrawCircle) {
ResizableEllipse *circle = new ResizableEllipse(
scenePos.x(), scenePos.y(), 50, 50);
circle->setBrush(QBrush(Qt::red)); // 设置指示灯颜色
scene->addItem(circle);
}
QTreeWidgetItem *selectedItem = ui->treeWidget_2->currentItem();
if (selectedItem)
{
selectedItem->setSelected(false);//取消选中当前项目
else if (canDrawRectangle) {
ResizableRectangle *rectangle = new ResizableRectangle(
scenePos.x(), scenePos.y(), 100, 50);
rectangle->setBrush(QBrush(Qt::yellow)); // 设置按钮颜色
scene->addItem(rectangle);
}

// 重置绘制标志
canDrawCircle = false;
canDrawRectangle = false;
return true;

// 修正2:使用一致的树控件对象名(treeWidget_2)
if (ui->treeWidget_2->currentItem()) {
ui->treeWidget_2->currentItem()->setSelected(false);
}

return true; // 事件已处理
}
return QMainWindow::eventFilter(obj, event);
}
// 修正3:正确处理未过滤的事件
return QMainWindow::eventFilter(obj, event);
}

+ 2
- 2
IntegratedPlatform/mainwindow.ui 파일 보기

@@ -52,7 +52,7 @@
<property name="font">
<font>
<family>宋体</family>
<pointsize>12</pointsize>
<pointsize>16</pointsize>
</font>
</property>
<column>
@@ -110,7 +110,7 @@
<property name="font">
<font>
<family>宋体</family>
<pointsize>18</pointsize>
<pointsize>16</pointsize>
</font>
</property>
<property name="selectionMode">


불러오는 중...
취소
저장