news 2026/9/19 14:22:27

QT 学习:协同开发的程序如何汇总到主程序

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
QT 学习:协同开发的程序如何汇总到主程序

有时候任务是分不同人开发的,如何把结果汇总到一个界面呢?
或者有些好的类是自己封装后,可以无限复制使用,怎么挪到自己的主程序呢?
以下举个小例子记录一下,我也备份一下
说明:
我有一个派生类,继承于QWidget,功能是用来绘制折线图的(直接QT例子粘贴的)
还有一个主程序,MainWindow,里面有一个QWidget占位,用来绘制折线图
如何把折线图绘制的QWidget放置到主程序里面的QWidget呢?
继承类:

/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/#ifndefMAINWIDGET_H#defineMAINWIDGET_H#include<QtCharts/QChartGlobal>#include<QtCharts/QChart>#include<QtCharts/QChartView>#include<QtWidgets/QWidget>#include<QtWidgets/QGraphicsWidget>#include<QtWidgets/QGridLayout>#include<QtWidgets/QGraphicsGridLayout>#include<QtWidgets/QDoubleSpinBox>#include<QtWidgets/QGroupBox>#include<QtCharts/QLineSeries>QT_CHARTS_USE_NAMESPACEclassMainWidget:publicQWidget{Q_OBJECTpublic:explicitMainWidget(QWidget*parent=0);voidaddSeries();voidconnectMarkers();publicslots:voidremoveSeries();voiddisconnectMarkers();voidhandleMarkerClicked();private:QChart*m_chart;QList<QLineSeries*>m_series;QChartView*m_chartView;QGridLayout*m_mainLayout;QGridLayout*m_fontLayout;};#endif// MAINWIDGET_H
/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/#include"mainwidget.h"#include<QtCharts/QChart>#include<QtCharts/QChartView>#include<QtWidgets/QPushButton>#include<QtWidgets/QLabel>#include<QtCore/QDebug>#include<QtCharts/QLegend>#include<QtWidgets/QFormLayout>#include<QtCharts/QLegendMarker>#include<QtCharts/QLineSeries>#include<QtCharts/QXYLegendMarker>#include<QtCore/QtMath>QT_CHARTS_USE_NAMESPACEMainWidget::MainWidget(QWidget*parent):QWidget(parent){// Create chart view with the chartm_chart=newQChart();m_chartView=newQChartView(m_chart,this);// Create layout for grid and detached legendm_mainLayout=newQGridLayout();m_mainLayout->addWidget(m_chartView,0,1,3,1);setLayout(m_mainLayout);// Add few seriesaddSeries();addSeries();addSeries();addSeries();connectMarkers();// Set the title and show legendm_chart->setTitle("Legendmarker example (click on legend)");m_chart->legend()->setVisible(true);m_chart->legend()->setAlignment(Qt::AlignBottom);m_chartView->setRenderHint(QPainter::Antialiasing);}voidMainWidget::addSeries(){QLineSeries*series=newQLineSeries();m_series.append(series);series->setName(QString("line "+QString::number(m_series.count())));// Make some sine wave for dataQList<QPointF>data;intoffset=m_chart->series().count();for(inti=0;i<360;i++){qreal x=offset*20+i;data.append(QPointF(i,qSin(qDegreesToRadians(x))));}series->append(data);m_chart->addSeries(series);if(m_series.count()==1)m_chart->createDefaultAxes();}voidMainWidget::removeSeries(){// Remove last series from chartif(m_series.count()>0){QLineSeries*series=m_series.last();m_chart->removeSeries(series);m_series.removeLast();deleteseries;}}voidMainWidget::connectMarkers(){//![1]// Connect all markers to handlerconstautomarkers=m_chart->legend()->markers();for(QLegendMarker*marker:markers){// Disconnect possible existing connection to avoid multiple connectionsQObject::disconnect(marker,&QLegendMarker::clicked,this,&MainWidget::handleMarkerClicked);QObject::connect(marker,&QLegendMarker::clicked,this,&MainWidget::handleMarkerClicked);}//![1]}voidMainWidget::disconnectMarkers(){//![2]constautomarkers=m_chart->legend()->markers();for(QLegendMarker*marker:markers){QObject::disconnect(marker,&QLegendMarker::clicked,this,&MainWidget::handleMarkerClicked);}//![2]}voidMainWidget::handleMarkerClicked(){//![3]QLegendMarker*marker=qobject_cast<QLegendMarker*>(sender());Q_ASSERT(marker);//![3]//![4]switch(marker->type())//![4]{caseQLegendMarker::LegendMarkerTypeXY:{//![5]// Toggle visibility of seriesmarker->series()->setVisible(!marker->series()->isVisible());// Turn legend marker back to visible, since hiding series also hides the marker// and we don't want it to happen now.marker->setVisible(true);//![5]//![6]// Dim the marker, if series is not visibleqreal alpha=1.0;if(!marker->series()->isVisible())alpha=0.5;QColor color;QBrush brush=marker->labelBrush();color=brush.color();color.setAlphaF(alpha);brush.setColor(color);marker->setLabelBrush(brush);brush=marker->brush();color=brush.color();color.setAlphaF(alpha);brush.setColor(color);marker->setBrush(brush);QPen pen=marker->pen();color=pen.color();color.setAlphaF(alpha);pen.setColor(color);marker->setPen(pen);//![6]break;}default:{qDebug()<<"Unknown marker type";break;}}}

实现:
1.我原来不考虑主程序代码量、不考虑代码美观,我都是直接写到主程序里面了,但是这样代码太多了,不方便管理,所以要剥离出来,单独封装
2.在代码里面直接创建实例即可,这样的话,所有图标操作全部放到chart 实例中封装对应接口就行

if(!ui->widget->layout()){ui->widget->setLayout(newQVBoxLayout());}chart=newMainWidget(this);// 添加到布局ui->widget->layout()->addWidget(chart);// 可选:设置布局边距ui->widget->layout()->setContentsMargins(0,0,0,0);//调用的时候chart->addSeries();chart->connectMarkers();

3.或者更简单,在.ui中,找到占位的QWidget 控件,右键提升,写类名和头文件即可,后续调用就不需要代码了,只需要ui->widget占位的控件调用派生类的接口函数就行

ui->widget->addSeries();ui->widget->connectMarkers();


可以了

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/12 22:56:58

Balena Etcher终极指南:快速免费镜像烧录神器

Balena Etcher终极指南&#xff1a;快速免费镜像烧录神器 【免费下载链接】etcher Flash OS images to SD cards & USB drives, safely and easily. 项目地址: https://gitcode.com/GitHub_Trending/et/etcher 还在为复杂的镜像烧录工具头疼吗&#xff1f;&#x1f…

作者头像 李华
网站建设 2026/9/19 3:41:50

戴森球计划工厂蓝图完全指南:高效自动化系统构建策略

戴森球计划工厂蓝图完全指南&#xff1a;高效自动化系统构建策略 【免费下载链接】FactoryBluePrints 游戏戴森球计划的**工厂**蓝图仓库 项目地址: https://gitcode.com/GitHub_Trending/fa/FactoryBluePrints 在戴森球计划中构建高效工厂系统往往面临诸多挑战&#xf…

作者头像 李华
网站建设 2026/9/2 18:26:00

SAM3实战案例:服装电商的虚拟试衣系统

SAM3实战案例&#xff1a;服装电商的虚拟试衣系统 1. 技术背景与应用场景 随着AI技术在电商领域的深入应用&#xff0c;虚拟试衣系统正成为提升用户体验和转化率的关键工具。传统试衣方案依赖3D建模或AR叠加&#xff0c;开发成本高、适配复杂。而基于SAM3&#xff08;Segment…

作者头像 李华
网站建设 2026/9/2 11:33:41

Qwen2.5资源占用高?轻量化部署优化实战

Qwen2.5资源占用高&#xff1f;轻量化部署优化实战 1. 背景与挑战&#xff1a;Qwen2.5-0.5B-Instruct的部署痛点 1.1 模型能力升级带来的资源压力 Qwen2.5 是最新的 Qwen 大型语言模型系列&#xff0c;涵盖从 0.5B 到 720B 参数规模的多个版本。其中 Qwen2.5-0.5B-Instruct …

作者头像 李华
网站建设 2026/9/19 1:37:18

实测通义千问2.5-7B-Instruct:vLLM推理加速效果超预期

实测通义千问2.5-7B-Instruct&#xff1a;vLLM推理加速效果超预期 随着大语言模型在实际业务场景中的广泛应用&#xff0c;如何高效部署并提升推理性能成为工程落地的关键挑战。本文基于 通义千问2.5-7B-Instruct 模型&#xff0c;结合 vLLM 推理框架与 Open WebUI 可视化界面…

作者头像 李华
网站建设 2026/9/14 6:42:05

5个实用技巧帮你轻松下载QQ音乐资源,告别会员限制

5个实用技巧帮你轻松下载QQ音乐资源&#xff0c;告别会员限制 【免费下载链接】res-downloader 资源下载器、网络资源嗅探&#xff0c;支持微信视频号下载、网页抖音无水印下载、网页快手无水印视频下载、酷狗音乐下载等网络资源拦截下载! 项目地址: https://gitcode.com/Git…

作者头像 李华