C++Qt实现类似multisim中复制拖拽器件并且器件之间连接起来的功能的完整代码

时间: 2023-12-30 15:07:48 浏览: 23
以下是一个简单的示例程序,演示了如何使用Qt实现类似Multisim中的拖放器件和连接它们的功能。程序包含两个自定义小部件:ComponentWidget和ConnectionWidget。 ComponentWidget类表示电路元件,例如电阻器、电容器等,它可以被拖放到另一个ComponentWidget上。 ConnectionWidget类表示两个ComponentWidget之间的连接。 下面是程序的完整代码: mainwindow.h: ```c++ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class ComponentWidget; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; ComponentWidget *m_lastComponent; private slots: void onComponentClicked(ComponentWidget *component); }; #endif // MAINWINDOW_H ``` mainwindow.cpp: ```c++ #include "mainwindow.h" #include "ui_mainwindow.h" #include "componentwidget.h" #include "connectionwidget.h" #include <QMouseEvent> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_lastComponent(nullptr) { ui->setupUi(this); setCentralWidget(ui->scrollArea); // 添加一些初始的元件 ComponentWidget *resistor1 = new ComponentWidget("Resistor"); ui->gridLayout->addWidget(resistor1, 0, 0); ComponentWidget *resistor2 = new ComponentWidget("Resistor"); ui->gridLayout->addWidget(resistor2, 1, 0); ComponentWidget *capacitor1 = new ComponentWidget("Capacitor"); ui->gridLayout->addWidget(capacitor1, 2, 0); ComponentWidget *capacitor2 = new ComponentWidget("Capacitor"); ui->gridLayout->addWidget(capacitor2, 3, 0); } MainWindow::~MainWindow() { delete ui; } void MainWindow::onComponentClicked(ComponentWidget *component) { if (m_lastComponent == nullptr) { m_lastComponent = component; } else { // 创建一个新的连接 ConnectionWidget *connection = new ConnectionWidget(m_lastComponent, component); ui->scrollAreaWidgetContents->layout()->addWidget(connection); m_lastComponent = nullptr; } } ``` componentwidget.h: ```c++ #ifndef COMPONENTWIDGET_H #define COMPONENTWIDGET_H #include <QWidget> class ComponentWidget : public QWidget { Q_OBJECT public: explicit ComponentWidget(const QString &name, QWidget *parent = nullptr); protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); private: QString m_name; QPointF m_dragStartPosition; signals: void clicked(ComponentWidget *component); }; #endif // COMPONENTWIDGET_H ``` componentwidget.cpp: ```c++ #include "componentwidget.h" #include <QMouseEvent> #include <QDrag> #include <QMimeData> #include <QPainter> ComponentWidget::ComponentWidget(const QString &name, QWidget *parent) : QWidget(parent), m_name(name) { setFixedSize(100, 100); } void ComponentWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragStartPosition = event->pos(); } } void ComponentWidget::mouseMoveEvent(QMouseEvent *event) { if ((event->buttons() & Qt::LeftButton) && (event->pos() - m_dragStartPosition).manhattanLength() > QApplication::startDragDistance()) { QDrag *drag = new QDrag(this); QMimeData *mimeData = new QMimeData(); mimeData->setText(m_name); drag->setMimeData(mimeData); QPixmap pixmap(size()); pixmap.fill(Qt::white); QPainter painter(&pixmap); painter.drawPixmap(rect(), grab()); drag->setPixmap(pixmap); drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); } } ``` connectionwidget.h: ```c++ #ifndef CONNECTIONWIDGET_H #define CONNECTIONWIDGET_H #include <QWidget> class ComponentWidget; class ConnectionWidget : public QWidget { Q_OBJECT public: explicit ConnectionWidget(ComponentWidget *startComponent, ComponentWidget *endComponent, QWidget *parent = nullptr); protected: void paintEvent(QPaintEvent *event); private: ComponentWidget *m_startComponent; ComponentWidget *m_endComponent; }; #endif // CONNECTIONWIDGET_H ``` connectionwidget.cpp: ```c++ #include "connectionwidget.h" #include "componentwidget.h" #include <QPainter> ConnectionWidget::ConnectionWidget(ComponentWidget *startComponent, ComponentWidget *endComponent, QWidget *parent) : QWidget(parent), m_startComponent(startComponent), m_endComponent(endComponent) { setAttribute(Qt::WA_TransparentForMouseEvents); setFixedSize(10, 10); } void ConnectionWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.setPen(QPen(Qt::black, 2)); painter.drawLine(m_startComponent->mapToGlobal(m_startComponent->rect().center()) - mapToGlobal(rect().center()), m_endComponent->mapToGlobal(m_endComponent->rect().center()) - mapToGlobal(rect().center())); } ``` 在这个程序中,我们使用一个QGridLayout来布置一些初始的ComponentWidget。当用户单击一个ComponentWidget时,我们会记录下来,并等待下一个ComponentWidget的单击。当用户单击另一个ComponentWidget时,我们就创建一个新的ConnectionWidget,并将其添加到布局中。 当用户从一个ComponentWidget中拖动时,我们会创建一个QMimeData对象并设置它的文本为该元件的名称。然后,我们创建一个QDrag对象,并将该QMimeData对象设置为其Mime数据。我们还创建了一个QPixmap对象,并将其绘制成当前元件的图像。最后,我们调用QDrag::exec()方法来启动拖放操作。 ConnectionWidget类用于表示两个ComponentWidget之间的连接。在paintEvent()函数中,我们使用QPainter绘制一条连接线,从起始元件的中心到终止元件的中心。由于我们将其设置为不接收鼠标事件,因此用户不能单击它或拖动它。

相关推荐

最新推荐

recommend-type

元器件应用中的门电路延迟时间的Multisim仿真测试方案

摘 要:介绍了用Multisim 仿真软件测试门电路延迟时间的方法,提出了三种测试方案,即将奇数个门首尾相接构成环形振荡电路,用虚拟示波器测试所产生振荡信号的周期,计算门的传输延迟时间;奇数个门首尾相接构成...
recommend-type

元器件应用中的电容的电路符号及图片识别

耦合:作为两个电路之间的连接,允许交流信号通过并传输到下一级电路  4.滤波:这个对DIY而言很重要,显卡上的电容基本都是这个作用。  5.温度补偿:针对其它组件对温度的适应性不够带来的影响,而进行补偿,...
recommend-type

multisim中导入元件的方法.doc

介绍了Multisim导入元件的方法(以TI器件导入为例,亲测可用,如果使用有什么问题可私戳)
recommend-type

仿真软件Multisim与PSpice在电路设计中的功能比较

随着计算机技术的迅速发展,计算机辅助设计技术(CAD)已渗透到电子...目前国际上比较流行两个仿真软件:Multisim (EWB的版本)和PSpice。通过对两个软件的认真学习和反复比较,发现二者存在很多差异,下面进行一一说明。
recommend-type

Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar

Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rarJava开发案例-springboot-19-校验表单重复提交-源代码+文档.rar Java开发案例-springboot-19-校验表单重复提交-源代码+文档.rar
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。