#include "mainwindownew.h" #include "ui_mainwindownew.h" #include #include #include // #include "formhead.h" #include "formtitle.h" #include "formdraw.h" #include "formline.h" #include "mycustomplot.h" #include "CallManage.h" #include "qtcenterwidgets.h" #include "qtcommonclass.h" //以下参数从配置文件读取 extern int g_iIndex; extern int g_iNum; extern int g_iOneWidth; //道宽 extern int g_iHeadHigh; //道头高度 extern int g_iTitleHigh; //道对象高度 extern int g_iCurveHigh;//曲线高度 extern int g_iMove; //道头偏移 extern int g_iPointNum; // number of points in graph extern int g_iLineNum; // number of Line extern int g_iWidth; //道宽 // extern int g_iX1; extern int g_iX2; extern int g_iY1; extern int g_iY2; extern int g_iCanZoom ; MainWindowNew::MainWindowNew(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowNew) { ui->setupUi(this); // 设置工具栏的位置,此处设置为在左侧 addToolBar(Qt::LeftToolBarArea, ui->toolBar); //初始化工具栏 initMainToolBar(); initToolBar(); //参数从配置文件读取 ReadConfig(); //加载样式 //loadStyle(":/qrc/qss/lightblue.css"); //loadStyle(":/qrc/qss/blacksoft.css"); loadStyle(":/qrc/qss/flatgray.css"); //setWindowState(Qt::WindowMaximized);//最大化 //停靠 dockLayout(); //QWidget *centralWidget = ui->centralWidget; centralWidget = ui->centralWidget; //---------------------- graphicsView = new QGraphicsView(this); scene = new QGraphicsScene(this); graphicsView->setScene(scene); // auto *layout = new QVBoxLayout(centralWidget); layout->setContentsMargins(0,0,0,0); layout->addWidget(graphicsView); //---------------------- // 添加多个按钮或其他小部件来模拟内容过多的情况 QString strName = ""; for (int i = 0; i < g_iIndex; ++i) { //道头 strName = "道" + QString::number(i+1); FormHead *formHead = new FormHead(graphicsView, i+1); formHead->Init(); formHead->Add(strName, QColor(Qt::white)); formHead->setGeometry(g_iMove+g_iOneWidth*i, 0, g_iOneWidth, g_iHeadHigh); // 设置标签的位置和大小 //对象 FormTitle *formTitle = new FormTitle(graphicsView, i+1); formTitle->setGeometry(g_iMove+g_iOneWidth*i, g_iHeadHigh, g_iOneWidth, g_iTitleHigh); // 设置标签的位置和大小 // MyCustomPlot *curv = new MyCustomPlot(graphicsView, i+1); //背景设置成透明色 curv->setBackground(Qt::transparent); curv->setStyleSheet("background: transparent;"); curv->setGeometry(0.5+g_iOneWidth*i, g_iHeadHigh+g_iTitleHigh-15, g_iOneWidth+29, g_iCurveHigh); // 设置标签的位置和大小 initForm(curv, "", g_iLineNum, formTitle); // scene->addWidget(formHead); scene->addWidget(formTitle); scene->addWidget(curv); // 连接信号和槽 connect(formTitle, &FormTitle::sig_LineClicked, curv, &MyCustomPlot::s_LineClicked); connect(formTitle, &FormTitle::removeSelectedGraphByTitle, curv, &MyCustomPlot::removeSelectedGraphByTitle); } if(g_iMove+g_iOneWidth*g_iNum < g_iWidth) { centralWidget->setGeometry(0, 0, g_iWidth, g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } else { centralWidget->setGeometry(0, 0, g_iMove+g_iOneWidth*g_iNum, g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } //滚动条 //QScrollArea *scrollArea = new QScrollArea(); scrollArea = new QScrollArea(); // widget是你想要放置内容的QWidget scrollArea->setWidget(centralWidget); // 设置滚动条一直显示 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // 设置滚动区域为主窗口的中心部件 setCentralWidget(scrollArea); //关联信号槽 connect(CallManage::getInstance(), SIGNAL(sig_Open()), this, SLOT(s_open())); connect(CallManage::getInstance(), SIGNAL(sig_Risize()), this, SLOT(s_risize())); connect(CallManage::getInstance(), SIGNAL(sig_AddOne()), this, SLOT(s_addOne())); connect(CallManage::getInstance(), SIGNAL(sig_AddLine(int)), this, SLOT(s_addLine(int))); connect(CallManage::getInstance(), SIGNAL(sig_DelOne(int)), this, SLOT(s_delOne(int))); connect(CallManage::getInstance(), SIGNAL(sig_SaveImg()), this, SLOT(s_SaveImg())); connect(CallManage::getInstance(), SIGNAL(sig_DrawImg()), this, SLOT(s_DrawImg())); connect(CallManage::getInstance(), SIGNAL(sig_DrawLine()), this, SLOT(s_DrawLine())); } MainWindowNew::~MainWindowNew() { delete ui; } //初始化工具栏 void MainWindowNew::initMainToolBar() { QSize toolIconSize(18, 18); ui->mainToolBar->setIconSize(toolIconSize); //设置工具栏图标大小 QIcon newFileIcon(":/image/new.png"); QIcon openFileIcon(":/image/open.png"); QIcon compileIcon(":/image/compile.png"); QIcon runIcon(":/image/capacity.png"); QIcon debugIcon(":/image/anaysis.png"); QIcon grepIcon(":/image/grab.png"); QIcon loadIcon(":/image/export.png"); m_newAc = new QAction(newFileIcon, "控制", this); m_openAc = new QAction(openFileIcon, "竖绘", this); m_grepAc = new QAction(grepIcon, "锁头", this); m_compileAc = new QAction(compileIcon, "加载图文件", this); m_runAc = new QAction(runIcon, "设置井", this); m_debugAc = new QAction(debugIcon, "撤销", this); m_loadAc = new QAction(loadIcon, "重做", this); ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); //此种方式为文字显示在图标右侧 //add QAction to Widget. ui->mainToolBar->addAction(m_newAc); ui->mainToolBar->addAction(m_openAc); ui->mainToolBar->addAction(m_grepAc); ui->mainToolBar->addAction(m_compileAc); ui->mainToolBar->addAction(m_runAc); ui->mainToolBar->addAction(m_debugAc); ui->mainToolBar->addAction(m_loadAc); // connect(m_openAc, &QAction::triggered, this, &MainWindow::s_Open); // connect(m_grepAc, &QAction::triggered, this, &MainWindow::s_Risize); // connect(m_compileAc, &QAction::triggered, this, &MainWindow::s_AddOne); // connect(m_runAc, &QAction::triggered, this, &MainWindow::s_SaveImage); // connect(m_debugAc, &QAction::triggered, this, &MainWindow::s_DrawImg); // connect(m_loadAc, &QAction::triggered, this, &MainWindow::s_DrawLine); } //初始化工具栏 void MainWindowNew::initToolBar() { QSize toolIconSize(18, 18); ui->toolBar->setIconSize(toolIconSize); //设置工具栏图标大小 QIcon newFileIcon(":/image/new.png"); QIcon openFileIcon(":/image/open.png"); QIcon compileIcon(":/image/compile.png"); QIcon runIcon(":/image/capacity.png"); QIcon debugIcon(":/image/anaysis.png"); QIcon grepIcon(":/image/grab.png"); QIcon loadIcon(":/image/export.png"); m_newAc2 = new QAction(newFileIcon, "", this); m_openAc2 = new QAction(openFileIcon, "", this); m_grepAc2 = new QAction(grepIcon, "", this); m_compileAc2 = new QAction(compileIcon, "", this); m_runAc2 = new QAction(runIcon, "", this); m_debugAc2 = new QAction(debugIcon, "", this); m_loadAc2 = new QAction(loadIcon, "", this); m_newAc2->setToolTip("道"); m_openAc2->setToolTip("深度"); m_grepAc2->setToolTip("曲线"); m_compileAc2->setToolTip("波列"); m_runAc2->setToolTip("解释结论道"); m_debugAc2->setToolTip("固井结论道"); m_loadAc2->setToolTip("井壁取心"); ui->toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly); //此种方式为文字显示在图标右侧 //add QAction to Widget. ui->toolBar->addAction(m_newAc2); ui->toolBar->addAction(m_openAc2); ui->toolBar->addAction(m_grepAc2); ui->toolBar->addAction(m_compileAc2); ui->toolBar->addAction(m_runAc2); ui->toolBar->addAction(m_debugAc2); ui->toolBar->addAction(m_loadAc2); // connect(m_openAc, &QAction::triggered, this, &MainWindow::s_Open); // connect(m_grepAc, &QAction::triggered, this, &MainWindow::s_Risize); // connect(m_compileAc, &QAction::triggered, this, &MainWindow::s_AddOne); // connect(m_runAc, &QAction::triggered, this, &MainWindow::s_SaveImage); // connect(m_debugAc, &QAction::triggered, this, &MainWindow::s_DrawImg); // connect(m_loadAc, &QAction::triggered, this, &MainWindow::s_DrawLine); } void MainWindowNew::ReadConfig() { int iIndex; int iNum; int iOneWidth; int iHeadHigh; int iTitleHigh; int iCurveHigh; int iMove; int iPointNum; int iLineNum; // 1.获取当前运行程序的目录路径 QString applicationDirPath = QCoreApplication::applicationDirPath(); QString configPath; configPath = applicationDirPath + "/config/config.ini"; //从配置文件读取 QtCommonClass *qtCommon = new QtCommonClass(this); qtCommon->readConfigSize(configPath, iIndex, iNum, iOneWidth, iHeadHigh, iTitleHigh, iCurveHigh, iMove, iPointNum, iLineNum); g_iIndex = iIndex; g_iNum = iNum; g_iOneWidth = iOneWidth; //道宽 g_iHeadHigh = iHeadHigh; //道头高度 g_iTitleHigh = iTitleHigh; //道对象高度 g_iCurveHigh = iCurveHigh;//曲线高度 g_iMove = iMove; //道头偏移 g_iPointNum = iPointNum; // number of points in graph g_iLineNum = iLineNum; // number of Line // qtCommon->readXyRange(configPath, g_iX1, g_iX2, g_iY1, g_iY2); } void MainWindowNew::loadStyle(const QString &qssFile) { //加载样式表 QString qss; QFile file(qssFile); if (file.open(QFile::ReadOnly)) { //用QTextStream读取样式文件不用区分文件编码 带bom也行 QStringList list; QTextStream in(&file); //in.setCodec("utf-8"); while (!in.atEnd()) { QString line; in >> line; list << line; } file.close(); qss = list.join("\n"); QString paletteColor = qss.mid(20, 7); this->setPalette(QPalette(paletteColor)); //用时主要在下面这句 this->setStyleSheet(qss); } } void MainWindowNew::addRandomGraph(MyCustomPlot *widget, QVector x, QVector y, FormTitle *formTitle, QString strLineName) { widget->addGraph(); if(strLineName=="") { strLineName = QString("曲线 %1").arg(widget->graphCount()); } widget->graph()->setName(strLineName); widget->graph()->setData(x, y); widget->graph()->setLineStyle((QCPGraph::LineStyle)(1));//曲线 //widget->graph()->setLineStyle((QCPGraph::LineStyle)(std::rand()%5+1)); //widget->graph()->setLineStyle((QCPGraph::LineStyle)(QCPGraph::lsLine));//曲线 //widget->graph()->setLineStyle((QCPGraph::LineStyle)(QCPGraph::lsImpulse));//杆状 // if (std::rand()%100 > 50) // widget->graph()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(std::rand()%14+1))); widget->graph()->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(1))); //widget->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5)); QPen graphPen; QColor lineColor = QColor(std::rand()%245+10, std::rand()%245+10, std::rand()%245+10); graphPen.setColor(lineColor); //graphPen.setWidthF(std::rand()/(double)RAND_MAX*2+1); graphPen.setWidthF(2); widget->graph()->setPen(graphPen); //widget->replot(); //道-对象 formTitle->Add(strLineName, lineColor); } void MainWindowNew::initForm(MyCustomPlot *widget, QString strName, int num, FormTitle *formTitle) { //道-对象清空 formTitle->Init(); // //widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | // QCP::iSelectLegend | QCP::iSelectPlottables); widget->setInteractions(QCP::iSelectLegend | QCP::iSelectPlottables); //拖动区域放大‌(即通过鼠标左键拖动绘制矩形框选区域进行放大)///////////////// if(g_iCanZoom==1) { widget->setInteraction(QCP::iRangeDrag, false); // 关闭拖动 widget->setSelectionRectMode(QCP::SelectionRectMode::srmZoom); // 启用框选放大 // widget->selectionRect()->setPen(QPen(Qt::black, 1, Qt::DashLine)); // 虚线边框 widget->selectionRect()->setBrush(QBrush(QColor(0,0,100,50))); // 半透明蓝色填充 // QCPSelectionRect *selectionRect = new QCPSelectionRect(widget); connect(selectionRect, &QCPSelectionRect::accepted, [=]() { // 当选择完成时,获取矩形范围并放大 QRectF rect = selectionRect->rect(); // 获取选择的矩形区域(像素坐标) // 转换为坐标轴范围 double x1 = widget->xAxis->pixelToCoord(rect.left()); double x2 = widget->xAxis->pixelToCoord(rect.right()); double y1 = widget->yAxis->pixelToCoord(rect.top()); double y2 = widget->yAxis->pixelToCoord(rect.bottom()); // 设置新的坐标轴范围 widget->xAxis->setRange(x1, x2); widget->yAxis->setRange(y1, y2); widget->replot(); }); } //拖动区域放大‌(即通过鼠标左键拖动绘制矩形框选区域进行放大)////////////// widget->m_iX1 = g_iX1; widget->m_iX2 = g_iX2; widget->m_iY1 = g_iY1; widget->m_iY2 = g_iY2; // widget->xAxis->setRange(g_iX1, g_iX2); widget->yAxis->setRange(g_iY1, g_iY2); widget->axisRect()->setupFullAxesBox(); widget->xAxis->ticker()->setTickCount(10);//x个主刻度 widget->yAxis->ticker()->setTickCount(60);//y个主刻度 // 设置刻度密度 // QSharedPointerMyTicker(new QCPAxisTickerFixed); // MyTicker->setTickStep(10); // MyTicker->setTickCount(300); // widget->xAxis->setTicker(MyTicker); // widget->plotLayout()->insertRow(0); // QCPTextElement *title = new QCPTextElement(widget, strName, QFont("sans", 17, QFont::Bold)); // widget->plotLayout()->addElement(0, 0, title); // widget->xAxis->setLabel("x"); // widget->yAxis->setLabel("y"); //对调XY轴,在最前面设置 QCPAxis *yAxis = widget->yAxis; QCPAxis *xAxis = widget->xAxis; widget->xAxis = yAxis; widget->yAxis = xAxis; // widget->legend->setVisible(true); // QFont legendFont = font(); // legendFont.setPointSize(10); // widget->legend->setFont(legendFont); // widget->legend->setSelectedFont(legendFont); // widget->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items QVector x(g_iPointNum), y(g_iPointNum); for(int k=0; kbounded(10); y[i] = (qSin(x[i]*r1*5)*qSin(qCos(x[i]*r2)*r4*3)+r3*qCos(qSin(x[i])*r4*2))*yScale + yOffset + randomNumber*0.01; } addRandomGraph(widget, x, y, formTitle, ""); } //widget->rescaleAxes(); widget->replot(); } void MainWindowNew::s_open() { // 1.获取当前运行程序的目录路径 QString applicationDirPath = QCoreApplication::applicationDirPath(); QString fileNames; fileNames = applicationDirPath + "/test.csv"; QList listDatas; QFile file(fileNames); if (!file.exists()) { QMessageBox::information(nullptr, "提示", "test.csv文件不存在!"); return; } // if (file.open(QIODevice::ReadOnly)) { int iIndex = 1; bool isHeader = true; while (!file.atEnd())//判断是否读到文件尾 { QString strData = QString::fromLocal8Bit(file.readLine().replace("\r\n", "")); //按行读,追加 //string strData = file.readLine().data();//QString::fromLocal8Bit(file.readLine()); //按行读,追加 if (isHeader) { isHeader = false; } else { QString strNewData = ""; strNewData = strData; //按行读,追加 //QString strNewData = QString::fromStdString(strData); QStringList array = strNewData.split(","); if (array.size() == 0) { continue; } listDatas.append(array); iIndex++; } } file.close(); } for(int iDataNum=0; iDataNum<4; iDataNum++) { if(g_iMove+g_iOneWidth*g_iNum < g_iWidth) { centralWidget->setGeometry(0, 0, g_iWidth, g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } else { centralWidget->setGeometry(0, 0, g_iMove+g_iOneWidth*(g_iNum+1), g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } //道头 QString strName = "道" + QString::number(g_iIndex+1); FormHead *formHead = new FormHead(graphicsView, g_iIndex+1); formHead->Init(); formHead->Add(strName, QColor(Qt::white)); formHead->setGeometry(g_iMove+g_iOneWidth*g_iNum, 0, g_iOneWidth, g_iHeadHigh); // 设置标签的位置和大小 formHead->setVisible(true); //对象 FormTitle *formTitle = new FormTitle(graphicsView, g_iIndex+1); formTitle->setGeometry(g_iMove+g_iOneWidth*g_iNum, g_iHeadHigh, g_iOneWidth, g_iTitleHigh); // 设置标签的位置和大小 formTitle->setVisible(true); // MyCustomPlot *curv = new MyCustomPlot(graphicsView, g_iIndex+1); //背景设置成透明色 curv->setBackground(Qt::transparent); curv->setStyleSheet("background: transparent;"); curv->setGeometry(0.5+g_iOneWidth*g_iNum, g_iHeadHigh+g_iTitleHigh-15, g_iOneWidth+29, g_iCurveHigh); // 设置标签的位置和大小 curv->setVisible(true); //initForm(curv, "", 0, formTitle); //------------- scene->addWidget(formHead); scene->addWidget(formTitle); scene->addWidget(curv); //道-对象清空 formTitle->Init(); // //curv->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | // QCP::iSelectLegend | QCP::iSelectPlottables); curv->setInteractions(QCP::iSelectLegend | QCP::iSelectPlottables); //拖动区域放大‌(即通过鼠标左键拖动绘制矩形框选区域进行放大)///////////////// if(g_iCanZoom==1) { curv->setInteraction(QCP::iRangeDrag, false); // 关闭拖动 curv->setSelectionRectMode(QCP::SelectionRectMode::srmZoom); // 启用框选放大 // curv->selectionRect()->setPen(QPen(Qt::black, 1, Qt::DashLine)); // 虚线边框 curv->selectionRect()->setBrush(QBrush(QColor(0,0,100,50))); // 半透明蓝色填充 // QCPSelectionRect *selectionRect = new QCPSelectionRect(curv); connect(selectionRect, &QCPSelectionRect::accepted, [=]() { // 当选择完成时,获取矩形范围并放大 QRectF rect = selectionRect->rect(); // 获取选择的矩形区域(像素坐标) // 转换为坐标轴范围 double x1 = curv->xAxis->pixelToCoord(rect.left()); double x2 = curv->xAxis->pixelToCoord(rect.right()); double y1 = curv->yAxis->pixelToCoord(rect.top()); double y2 = curv->yAxis->pixelToCoord(rect.bottom()); // 设置新的坐标轴范围 curv->xAxis->setRange(x1, x2); curv->yAxis->setRange(y1, y2); curv->replot(); }); } //拖动区域放大‌(即通过鼠标左键拖动绘制矩形框选区域进行放大)////////////// if(iDataNum==0) { curv->m_iX1 = -5000; curv->m_iX2 = 5000; curv->m_iY1 = -500; curv->m_iY2 = 0; // curv->xAxis->setRange(-5000, 5000); curv->yAxis->setRange(-500, 0); curv->axisRect()->setupFullAxesBox(); curv->xAxis->ticker()->setTickCount(10);//x个主刻度 curv->yAxis->ticker()->setTickCount(60);//y个主刻度 } else if(iDataNum==1 || iDataNum==2) { curv->m_iX1 = 0; curv->m_iX2 = 150; curv->m_iY1 = -500; curv->m_iY2 = 0; // curv->xAxis->setRange(0, 150); curv->yAxis->setRange(-500, 0); curv->axisRect()->setupFullAxesBox(); curv->xAxis->ticker()->setTickCount(10);//x个主刻度 curv->yAxis->ticker()->setTickCount(60);//y个主刻度 } else if(iDataNum==3) { curv->m_iX1 = 10; curv->m_iX2 = 20; curv->m_iY1 = -500; curv->m_iY2 = 0; // curv->xAxis->setRange(10, 20); curv->yAxis->setRange(-500, 0); curv->axisRect()->setupFullAxesBox(); curv->xAxis->ticker()->setTickCount(10);//x个主刻度 curv->yAxis->ticker()->setTickCount(60);//y个主刻度 } //对调XY轴,在最前面设置 QCPAxis *yAxis = curv->yAxis; QCPAxis *xAxis = curv->xAxis; curv->xAxis = yAxis; curv->yAxis = xAxis; // // 连接信号和槽 connect(formTitle, &FormTitle::sig_LineClicked, curv, &MyCustomPlot::s_LineClicked); connect(formTitle, &FormTitle::removeSelectedGraphByTitle, curv, &MyCustomPlot::removeSelectedGraphByTitle); // g_iIndex++; g_iNum++; //画线--------------- if(curv && formTitle) { int iSize = listDatas.size(); QVector x(iSize), y(iSize); for (int i=0; im_indexLine+1); curv->m_indexLine++; addRandomGraph(curv, x, y, formTitle, strLineName); //curv->rescaleAxes(); curv->replot(); } } } void MainWindowNew::s_risize() { g_iCanZoom = 1; } void MainWindowNew::s_addOne() { // QMessageBox::StandardButton reply; // reply = QMessageBox::question(this, "提示", "是否添加新道?", // QMessageBox::Yes | QMessageBox::No); // if (reply != QMessageBox::Yes) { // return; // 取消 // } //QWidget *centralWidget = ui->centralWidget; if(g_iMove+g_iOneWidth*g_iNum < g_iWidth) { centralWidget->setGeometry(0, 0, g_iWidth, g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } else { centralWidget->setGeometry(0, 0, g_iMove+g_iOneWidth*(g_iNum+1), g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } //道头 QString strName = "道" + QString::number(g_iIndex+1); FormHead *formHead = new FormHead(graphicsView, g_iIndex+1); formHead->Init(); formHead->Add(strName, QColor(Qt::white)); formHead->setGeometry(g_iMove+g_iOneWidth*g_iNum, 0, g_iOneWidth, g_iHeadHigh); // 设置标签的位置和大小 formHead->setVisible(true); //对象 FormTitle *formTitle = new FormTitle(graphicsView, g_iIndex+1); formTitle->setGeometry(g_iMove+g_iOneWidth*g_iNum, g_iHeadHigh, g_iOneWidth, g_iTitleHigh); // 设置标签的位置和大小 formTitle->setVisible(true); // MyCustomPlot *curv = new MyCustomPlot(graphicsView, g_iIndex+1); //背景设置成透明色 curv->setBackground(Qt::transparent); curv->setStyleSheet("background: transparent;"); //curv->setGeometry(g_iOneWidth, g_iHeadHigh+g_iTitleHigh, g_iOneWidth+27, g_iCurveHigh); // 设置标签的位置和大小 curv->setGeometry(0.5+g_iOneWidth*g_iNum, g_iHeadHigh+g_iTitleHigh-15, g_iOneWidth+29, g_iCurveHigh); // 设置标签的位置和大小 curv->setVisible(true); initForm(curv, "", g_iLineNum, formTitle); //------------------------ scene->addWidget(formHead); scene->addWidget(formTitle); scene->addWidget(curv); // 连接信号和槽 connect(formTitle, &FormTitle::sig_LineClicked, curv, &MyCustomPlot::s_LineClicked); connect(formTitle, &FormTitle::removeSelectedGraphByTitle, curv, &MyCustomPlot::removeSelectedGraphByTitle); // g_iIndex++; g_iNum++; } void MainWindowNew::s_addLine(int indexID) { qDebug() << "MainWindowNew s_addLine"; // QString strQuestion = "是否新建曲线?\n道编号:" + QString::number(indexID); // QMessageBox::StandardButton reply; // reply = QMessageBox::question(this, "提示", strQuestion, // QMessageBox::Yes | QMessageBox::No); // if (reply != QMessageBox::Yes) { // return; // 取消 // } MyCustomPlot *widget = nullptr; FormTitle *formTitle =nullptr; // 获取当前widget的所有子控件 const QObjectList &children = centralWidget->children(); // 遍历子控件列表 for (QObject *child : children) { // 判断子控件是否为QWidget类型 if (QWidget *childWidget = qobject_cast(child)) { // 打印子控件的信息,使用缩进表示层级关系 qDebug() << QString("%1").arg(childWidget->objectName()); QString strObjName = childWidget->objectName(); if(strObjName=="FormTitle") { FormTitle *form = (FormTitle*)childWidget; if(form->m_indexID == indexID) { formTitle = form; //break; } } else if(strObjName=="MyCustomPlot") { MyCustomPlot *form = (MyCustomPlot*)childWidget; if(form->m_indexID == indexID) { widget = form; //break; } } } } if(widget && formTitle) { QVector x(g_iPointNum), y(g_iPointNum); double xScale = (std::rand()/(double)RAND_MAX + 0.5)*2; double yScale = (std::rand()/(double)RAND_MAX + 0.5)*2; double xOffset = (std::rand()/(double)RAND_MAX - 0.5)*4; double yOffset = (std::rand()/(double)RAND_MAX - 0.5)*10; double r1 = (std::rand()/(double)RAND_MAX - 0.5)*2; double r2 = (std::rand()/(double)RAND_MAX - 0.5)*2; double r3 = (std::rand()/(double)RAND_MAX - 0.5)*2; double r4 = (std::rand()/(double)RAND_MAX - 0.5)*2; // for (int i=0; ibounded(10); y[i] = (qSin(x[i]*r1*5)*qSin(qCos(x[i]*r2)*r4*3)+r3*qCos(qSin(x[i])*r4*2))*yScale + yOffset + randomNumber*0.01; } QString strLineName = QString("新增曲线 %1").arg(widget->m_indexLine+1); widget->m_indexLine++; addRandomGraph(widget, x, y, formTitle, strLineName); //widget->rescaleAxes(); widget->replot(); } } void MainWindowNew::s_delOne(int indexID) { QString strQuestion = "是否删除道?\n编号:" + QString::number(indexID); QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "提示", strQuestion, QMessageBox::Yes | QMessageBox::No); if (reply != QMessageBox::Yes) { return; // 取消 } // 获取当前widget的所有子控件 const QObjectList &children = centralWidget->children(); // 遍历子控件列表 for (QObject *child : children) { // 判断子控件是否为QWidget类型 if (QWidget *childWidget = qobject_cast(child)) { // 打印子控件的信息,使用缩进表示层级关系 qDebug() << QString("%1").arg(childWidget->objectName()); QString strObjName = childWidget->objectName(); if(strObjName=="FormHead") { FormHead *form = (FormHead*)childWidget; if(form->m_indexID == indexID) { childWidget->deleteLater(); // 安排控件的删除,稍后执行 //break; } } else if(strObjName=="FormTitle") { FormTitle *form = (FormTitle*)childWidget; if(form->m_indexID == indexID) { childWidget->deleteLater(); // 安排控件的删除,稍后执行 //break; } } else if(strObjName=="MyCustomPlot") { MyCustomPlot *form = (MyCustomPlot*)childWidget; if(form->m_indexID == indexID) { childWidget->deleteLater(); // 安排控件的删除,稍后执行 //break; } } } } //暂时不处理删除,坐标调整 //g_iNum--; //centralWidget->setGeometry(0, 0, g_iMove+g_iOneWidth*g_iNum, g_iHeadHigh+g_iTitleHigh+g_iCurveHigh); // 设置标签的位置和大小 } void MainWindowNew::s_SaveImg() { QString strQuestion = "是否保存图像?"; QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "提示", strQuestion, QMessageBox::Yes | QMessageBox::No); if (reply != QMessageBox::Yes) { return; // 取消 } // 1.获取当前运行程序的目录路径 QString applicationDirPath = QCoreApplication::applicationDirPath(); QString configPath; configPath = applicationDirPath + "/1.png"; //指标体系图 QPixmap pCenter = centralWidget->grab(QRect(0, 0, centralWidget->width(), centralWidget->height())); //pCenter.save(configPath, "png"); QPixmap pImageHead = m_ImageHeadTable->grab(QRect(0, 0, m_ImageHeadTable->width(), m_ImageHeadTable->height())); // 创建一个足够大的QPixmap作为画布 int width = std::max({pImageHead.width(), pCenter.width()});// 最大宽度 int height = pImageHead.height()+ pCenter.height(); // 总高度 QPixmap result(width, height); result.fill(Qt::transparent); // 填充背景为透明,或者使用其他颜色填充背景 // 使用QPainter绘制小QPixmap QPainter painter(&result); painter.drawPixmap(0, 0, pImageHead); // 在(0,0)位置绘制pixmap1 painter.drawPixmap(0, pImageHead.height(), pCenter); // 在pixmap1右侧绘制pixmap2 //painter.drawPixmap(pixmap1.width() + pixmap2.width(), 0, pixmap3); // 在右侧继续绘制pixmap3 result.save(configPath, "png"); } void MainWindowNew::s_DrawImg() { QString strQuestion = "是否画图?"; QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "提示", strQuestion, QMessageBox::Yes | QMessageBox::No); if (reply != QMessageBox::Yes) { return; // 取消 } FormDraw *formDraw = new FormDraw(centralWidget); //formDraw->setStyleSheet("QWidget { background-color: red; }"); formDraw->setGeometry(100, g_iHeadHigh+g_iTitleHigh+200, 200, 200); // 设置标签的位置和大小 formDraw->setVisible(true); } void MainWindowNew::s_DrawLine() { QString strQuestion = "是否画线?"; QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "提示", strQuestion, QMessageBox::Yes | QMessageBox::No); if (reply != QMessageBox::Yes) { return; // 取消 } FormLine *formLine = new FormLine(centralWidget); formLine->setGeometry(100, g_iHeadHigh+g_iTitleHigh+100, 200, 2); // 设置标签的位置和大小 formLine->setVisible(true); } //停靠 void MainWindowNew::dockLayout() { // QDockWidget *dockImageHead=new QDockWidget(tr(""),this); //停靠窗口4 dockImageHead->setFeatures(QDockWidget::DockWidgetClosable);//|QDockWidget::DockWidgetFloatable //dockImageHead->setAllowedAreas(Qt::TopDockWidgetArea); QWidget *dockWidgetContents = new QWidget(); //dockWidgetContents->setGeometry(0,0,800,400); if(!m_ImageHeadTable) { m_ImageHeadTable = new QTableWidget(dockWidgetContents); m_ImageHeadTable->setMinimumSize(1100,300); //清空 m_ImageHeadTable->clearContents(); // m_ImageHeadTable->verticalHeader()->hide(); m_ImageHeadTable->horizontalHeader()->hide(); //因为tableWidget需要提前规定好行数与列数 int rowcount = 3; //总行数 int columnCount = 5;//总列数 m_ImageHeadTable->setColumnCount(columnCount); m_ImageHeadTable->setRowCount(rowcount); //动态设置行数 for(int i=0; isetItem(i, j, item); } } // 设置右键菜单策略 m_ImageHeadTable->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_ImageHeadTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu(QPoint))); } //----- QVBoxLayout *layout = new QVBoxLayout(dockWidgetContents); // 将你的widget添加到布局中 layout->addWidget(m_ImageHeadTable, 1, Qt::AlignCenter); // 使用Qt::AlignCenter进行居中 layout->setAlignment(Qt::AlignCenter); // 设置整个布局的居中 dockImageHead->setWidget(dockWidgetContents); //----- //dockImageHead->setWidget(m_ImageHeadTable); addDockWidget(Qt::TopDockWidgetArea, dockImageHead); } void MainWindowNew::slotContextMenu(QPoint pos) { // 获取点击位置的 QTableWidgetItem QTableWidgetItem *item = m_ImageHeadTable->itemAt(pos); // 判断是否点击在有效数据区域 //if (item) { if(1) { QMenu menu(m_ImageHeadTable); QAction *mergeAction = menu.addAction("合并表格"); QAction *refreshAction = menu.addAction("拆分表格"); QAction *deleteAction = menu.addAction("删除行"); QAction *addAction = menu.addAction("添加行"); // 弹出菜单 QAction *selectedAction = menu.exec(m_ImageHeadTable->mapToGlobal(pos)); if (selectedAction == mergeAction) { qDebug() << "合并表格"; slotMerge(); } else if (selectedAction == deleteAction) { int row = m_ImageHeadTable->currentRow(); m_ImageHeadTable->removeRow(row); } else if (selectedAction == refreshAction) { qDebug() << "拆分表格"; slotSplit(); } } else { // 点击在无效区域,不弹出菜单 qDebug() << "点击在无效区域,不弹出菜单"; } } //合并 void MainWindowNew::slotMerge() { QModelIndexList list = m_ImageHeadTable->selectionModel()->selectedIndexes(); if (list.size() < 2) { QMessageBox::warning(this, "单元格合并", "所选中单元格中为单个单元格,无法合并", "确定"); return; } int topRow = 0; int leftCol = 0; int bottomRow = 0; int rightCol = 0; QList selectRanges = m_ImageHeadTable->selectedRanges(); if (selectRanges.size() > 0) { topRow = selectRanges[0].topRow(); leftCol = selectRanges[0].leftColumn(); bottomRow = selectRanges[0].bottomRow(); rightCol = selectRanges[0].rightColumn(); } for(auto range:selectRanges) { if(range.topRow() bottomRow) bottomRow=range.bottomRow(); if(range.rightColumn()>rightCol) rightCol=range.rightColumn(); } int rowSpan = (bottomRow - topRow) + 1; int colSpan = (rightCol - leftCol) + 1; m_ImageHeadTable->setSpan(topRow, leftCol, rowSpan, colSpan); } //拆分 void MainWindowNew::slotSplit() { int row,col; QList selectRanges = m_ImageHeadTable->selectedRanges(); if (selectRanges.size() < 2) { QMessageBox::warning(this, "拆分表格失败", "单元格已是最小单位,不能再进行拆分", "确定"); return; } QList selectItems = m_ImageHeadTable->selectedItems(); if(selectItems.size()==0) { QMessageBox::warning(this, "拆分表格失败", "请先为表格设置元素item", "确定"); return; } if(selectItems.size()>1) { QMessageBox::warning(this, "拆分表格失败", "非法选择", "确定"); return; } for(auto item:selectItems) { row= item->row(); col=item->column(); } m_ImageHeadTable->setSpan(row, col, 1, 1); // 设置跨度为1 }