qt坐标轴实现修改颜色,坐标轴可拖拽、缩放

This commit is contained in:
dengjinlai 2024-09-11 16:53:11 +08:00
parent e1dd28762d
commit eaa879d463
5 changed files with 142 additions and 65 deletions

View File

@ -12,7 +12,12 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this);
customPlot = new QCustomPlot(this);
settingsDialog = new SettingsDialog(this);
plotGraph();
settingsDialog = new SettingsDialog(curves.size(), this);
QVector<QColor> curves_color = settingsDialog->getCurveColors();
for(int i=0; i< curves.size(); ++i){
curves[i]->setPen(QPen(curves_color[i]));
}
QPushButton *saveButton = new QPushButton("Save Graph", this);
connect(saveButton, &QPushButton::clicked, this, &MainWindow::saveGraph);
@ -22,7 +27,6 @@ MainWindow::MainWindow(QWidget *parent)
layout->addWidget(saveButton);
ui->centralwidget->setLayout(layout);
plotGraph();
// 连接customPlot的双击信号到槽函数
connect(customPlot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::openSettingsDialog);
@ -37,6 +41,8 @@ MainWindow::~MainWindow()
void MainWindow::plotGraph()
{
curves.clear();
QVector<double> I_values, t_values;
QVector<QCPCurveData> curve0, curve1, curve2, curve3, curve4, curve5;
@ -63,33 +69,36 @@ void MainWindow::plotGraph()
// 创建曲线并添加到图表中
QCPCurve *curveFunction = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curveFunction);
QCPCurve *curve0Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curve0Plot);
QCPCurve *curve1Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curve1Plot);
QCPCurve *curve2Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curve2Plot);
QCPCurve *curve3Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curve3Plot);
QCPCurve *curve4Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curve4Plot);
QCPCurve *curve5Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
curves.append(curve5Plot);
curveFunction->setData(I_values, t_values);
curveFunction->setPen(QPen(Qt::black, 1, Qt::DashLine));
curveFunction->setPen(QPen(Qt::DashLine));
// curveFunction->setPen(QPen(Qt::black, 1, Qt::DashLine));
curve0Plot->data()->set(curve0, true);
curve0Plot->setPen(QPen(Qt::blue));
// curve0Plot->setPen(QPen(Qt::blue));
curve1Plot->data()->set(curve1, true);
curve1Plot->setPen(QPen(Qt::green));
// curve1Plot->setPen(QPen(Qt::green));
curve2Plot->data()->set(curve2, true);
curve2Plot->setPen(QPen(QColorConstants::Svg::purple));
// curve2Plot->setPen(QPen(QColorConstants::Svg::purple));
curve3Plot->data()->set(curve3, true);
curve3Plot->setPen(QPen(QColorConstants::Svg::orange));
// curve3Plot->setPen(QPen(QColorConstants::Svg::orange));
curve4Plot->data()->set(curve4, true);
curve4Plot->setPen(QPen(Qt::red));
// curve4Plot->setPen(QPen(Qt::red));
curve5Plot->data()->set(curve5, true);
curve5Plot->setPen(QPen(QColorConstants::Svg::brown));
// curve5Plot->setPen(QPen(QColorConstants::Svg::brown));
// 设置对数坐标轴
customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
@ -101,6 +110,16 @@ void MainWindow::plotGraph()
customPlot->xAxis->setRange(5, 10000);
customPlot->yAxis->setRange(0.01, 1000);
// 启用缩放拖动
customPlot->setInteraction(QCP::iRangeZoom, true); // 缩放
customPlot->setInteraction(QCP::iRangeDrag, true); // 拖动
// 设置可以通过滚轮缩放的轴
customPlot->axisRect()->setRangeZoom(Qt::Horizontal | Qt::Vertical); // 水平和垂直方向都可以缩放
customPlot->axisRect()->setRangeZoomAxes(customPlot->xAxis, customPlot->yAxis); // 指定X和Y轴
// 设置缩放的缩放速度
customPlot->axisRect()->setRangeZoomFactor(0.9);
// 显示网格
customPlot->xAxis->grid()->setSubGridVisible(true);
customPlot->yAxis->grid()->setSubGridVisible(true);
@ -132,12 +151,18 @@ void MainWindow::openSettingsDialog()
customPlot->yAxis->setRange(yMin, yMax);
// 设置曲线颜色
QColor curveColor1 = settingsDialog->getCurveColor(0);
QColor curveColor2 = settingsDialog->getCurveColor(1);
// QColor curveColor1 = settingsDialog->getCurveColor(0);
// QColor curveColor2 = settingsDialog->getCurveColor(1);
// customPlot->graph(0)->setPen(QPen(curveColor1));
// customPlot->graph(1)->setPen(QPen(curveColor2));
QVector<QColor> curves_color = settingsDialog->getCurveColors();
for(int i=0; i< curves.size(); ++i){
curves[i]->setPen(QPen(curves_color[i]));
}
// 设置背景颜色
QColor backgroundColor = settingsDialog->getBackgroundColor();
customPlot->setBackground(backgroundColor);

View File

@ -19,13 +19,14 @@ public:
private slots:
void saveGraph();
void openSettingsDialog(); // 新增槽函数
void openSettingsDialog();
private:
Ui::MainWindow *ui;
QCustomPlot *customPlot;
SettingsDialog *settingsDialog;
QVector<QCPCurve*> curves;
void plotGraph();
};

View File

@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>曲线编辑</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
@ -20,7 +20,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>27</height>
<height>23</height>
</rect>
</property>
</widget>

View File

@ -1,29 +1,28 @@
#include "settingsdialog.h"
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent)
SettingsDialog::SettingsDialog(int curveCount, QWidget *parent) :
QDialog(parent),
// curveColors{Qt::blue, Qt::green},
backgroundColor(Qt::white)
{
initializeCurveColors(100, 123);
// 创建UI元素
QLabel *xMinLabel = new QLabel("X Axis Min:", this);
xMinLineEdit = new QLineEdit(this);
xMinLineEdit->setText("5");
QLabel *xMaxLabel = new QLabel("X Axis Max:", this);
xMaxLineEdit = new QLineEdit(this);
xMaxLineEdit->setText("10000");
QLabel *yMinLabel = new QLabel("Y Axis Min:", this);
yMinLineEdit = new QLineEdit(this);
yMinLineEdit->setText("0.01");
QLabel *yMaxLabel = new QLabel("Y Axis Max:", this);
yMaxLineEdit = new QLineEdit(this);
QLabel *curveColor1Label = new QLabel("Curve 1 Color:", this);
curveColor1Button = new QPushButton(this);
curveColor1Button->setStyleSheet("background-color: blue");
QLabel *curveColor2Label = new QLabel("Curve 2 Color:", this);
curveColor2Button = new QPushButton(this);
curveColor2Button->setStyleSheet("background-color: green");
yMaxLineEdit->setText("1000");
QLabel *backgroundColorLabel = new QLabel("Background Color:", this);
backgroundColorButton = new QPushButton(this);
backgroundColorButton->setStyleSheet("background-color: white");
updateColorButton(backgroundColorButton, backgroundColor);
gridCheckBox = new QCheckBox("Show Grid", this);
gridCheckBox->setChecked(true);
@ -39,14 +38,13 @@ SettingsDialog::SettingsDialog(QWidget *parent) :
layout->addWidget(yMaxLabel, 3, 0);
layout->addWidget(yMaxLineEdit, 3, 1);
layout->addWidget(curveColor1Label, 4, 0);
layout->addWidget(curveColor1Button, 4, 1);
layout->addWidget(curveColor2Label, 5, 0);
layout->addWidget(curveColor2Button, 5, 1);
layout->addWidget(backgroundColorLabel, 6, 0);
layout->addWidget(backgroundColorButton, 6, 1);
// 添加曲线颜色按钮
setupCurveColorButtons(curveCount);
layout->addWidget(gridCheckBox, 7, 0, 1, 2);
layout->addWidget(backgroundColorLabel, 4 + curveCount, 0);
layout->addWidget(backgroundColorButton, 4 + curveCount, 1);
layout->addWidget(gridCheckBox, 5 + curveCount, 0, 1, 2);
// 确认和取消按钮
QPushButton *okButton = new QPushButton("OK", this);
@ -56,34 +54,30 @@ SettingsDialog::SettingsDialog(QWidget *parent) :
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
// 将按钮布局添加到主布局
layout->addLayout(buttonLayout, 8, 0, 1, 2);
layout->addLayout(buttonLayout, 6 + curveCount, 0, 1, 2);
// 设置对话框的主要布局
setLayout(layout);
// 连接按钮信号到槽
connect(curveColor1Button, &QPushButton::clicked, this, [=](){
QColor color = QColorDialog::getColor(Qt::blue, this);
if (color.isValid()) {
curveColor1Button->setStyleSheet(QString("background-color: %1").arg(color.name()));
}
});
connect(curveColor2Button, &QPushButton::clicked, this, [=](){
QColor color = QColorDialog::getColor(Qt::green, this);
if (color.isValid()) {
curveColor2Button->setStyleSheet(QString("background-color: %1").arg(color.name()));
}
});
connect(backgroundColorButton, &QPushButton::clicked, this, [=](){
QColor color = QColorDialog::getColor(Qt::white, this);
QColor color = QColorDialog::getColor(backgroundColor, this);
if (color.isValid()) {
backgroundColorButton->setStyleSheet(QString("background-color: %1").arg(color.name()));
backgroundColor = color;
updateColorButton(backgroundColorButton, backgroundColor);
}
});
for (int i = 0; i < curveColorButtons.size(); ++i) {
connect(curveColorButtons[i], &QPushButton::clicked, this, [=]() {
QColor color = QColorDialog::getColor(curveColors[i], this);
if (color.isValid()) {
curveColors[i] = color;
updateColorButton(curveColorButtons[i], color);
}
});
}
connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
}
@ -96,17 +90,65 @@ double SettingsDialog::getYMin() const { return yMinLineEdit->text().toDouble();
double SettingsDialog::getYMax() const { return yMaxLineEdit->text().toDouble(); }
QColor SettingsDialog::getCurveColor(int index) const {
switch (index) {
case 0: return QColor(curveColor1Button->styleSheet().mid(17, 7));
case 1: return QColor(curveColor2Button->styleSheet().mid(17, 7));
default: return QColor(Qt::black);
if (index >= 0 && index < curveColors.size()) {
return curveColors[index];
}
return QColor(Qt::black);
}
QColor SettingsDialog::getBackgroundColor() const {
return QColor(backgroundColorButton->styleSheet().mid(17, 7));
return backgroundColor;
}
QVector<QColor> SettingsDialog::getCurveColors() const {
return curveColors;
}
bool SettingsDialog::isGridVisible() const {
return gridCheckBox->isChecked();
}
void SettingsDialog::setupCurveColorButtons(int curveCount) {
QStringList labels;
for (int i = 0; i < curveCount; ++i) {
labels << QString("Curve %1 Color:").arg(i+1);
}
QGridLayout *layout = qobject_cast<QGridLayout*>(this->layout());
qDeleteAll(curveColorButtons);
curveColorButtons.clear();
for (int i = 0; i < labels.size(); ++i) {
QLabel *label = new QLabel(labels[i], this);
QPushButton *button = new QPushButton(this);
// 这里假设curveColors有足够的元素如果不确定填充默认颜色
if (i < curveColors.size()) {
updateColorButton(button, curveColors[i]);
} else {
updateColorButton(button, Qt::white);
}
layout->addWidget(label, 4 + i, 0);
layout->addWidget(button, 4 + i, 1);
curveColorButtons.append(button);
}
}
void SettingsDialog::initializeCurveColors(int size, quint32 seed){
curveColors.resize(size);
QRandomGenerator random(seed);
// 生成随机颜色并填充 QVector
for (int i = 0; i < curveColors.size(); ++i) {
int red = random.bounded(256);
int green = random.bounded(256);
int blue = random.bounded(256);
curveColors[i] = QColor(red, green, blue);
}
}
void SettingsDialog::updateColorButton(QPushButton *button, const QColor &color) {
button->setStyleSheet(QString("background-color: %1").arg(color.name()));
}

View File

@ -10,13 +10,15 @@
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QVector>
#include <QRandomGenerator>
class SettingsDialog : public QDialog
{
Q_OBJECT
public:
explicit SettingsDialog(QWidget *parent = nullptr);
explicit SettingsDialog(int curveCount, QWidget *parent = nullptr);
~SettingsDialog();
double getXMin() const;
@ -26,6 +28,7 @@ public:
QColor getCurveColor(int index) const;
QColor getBackgroundColor() const;
QVector<QColor> getCurveColors() const;
bool isGridVisible() const;
@ -35,11 +38,17 @@ private:
QLineEdit *yMinLineEdit;
QLineEdit *yMaxLineEdit;
QPushButton *curveColor1Button;
QPushButton *curveColor2Button;
QVector<QPushButton*> curveColorButtons;
QPushButton *backgroundColorButton;
QVector<QColor> curveColors;
QColor backgroundColor;
QCheckBox *gridCheckBox;
void setupCurveColorButtons(int curveCount);
void initializeCurveColors(int size, quint32 seed);
void updateColorButton(QPushButton *button, const QColor &color);
};
#endif // SETTINGSDIALOG__H