'QCustomPlots updates the heatmap values but not the axis values
My goal is to make a program that can display a heatmap based on the values in a text file.
Upon first opening a text file, everything seems to work normally- the heatmap values, color scale label, and color scale bar values are displayed correctly.
When I try opening a different text file without recompiling(second, third, and so on..), the heatmap values show up normally but the values of the color scale bar (the one on the right) don't get updated + the color scale label duplicates and shifts into the left corner.
I don't know which leftover data causes this so any help would be appreciated.
Images for clarification:
The result after opening the first file:

The result after opening a second, different file (notice the same color scale bar values (on the right) and the label artifact in the upper left corner)

This is my cpp file:
void MainWindow::setupPlot()
{
ui->customPlot->clearPlottables();
ui->customPlot->axisRect()->setupFullAxesBox(true);
ui->customPlot->xAxis->setLabel("x");
ui->customPlot->yAxis->setLabel("y");
}
void MainWindow::readFile()
{
std::ifstream file(path.toStdString());
std::string eil;
int i, f, count = 0;
if (file.is_open())
{
for (i = 0; i < 80; i++) {
for (f = 0; f < 106; f++) {
count++;
file >> eil;
std::stringstream geek(eil);
geek >> viskas[i][f];
if (viskas[i][f] > max_verte) max_verte = viskas[i][f];
eil.clear();
}
}
}
else throw std::exception();
}
QString MainWindow::getFilename()
{
QString filename = QFileInfo(path).baseName();
return filename;
}
QString MainWindow::getPath()
{
path = QFileDialog::getOpenFileName(this, tr("Select text file"), "", tr("Text files (*.txt)"));
return path;
}
void MainWindow::paintPlot()
{
QCPColorMap* colorMap = new QCPColorMap(ui->customPlot->xAxis, ui->customPlot->yAxis);
colorMap->data()->setSize(nx, ny);
colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(-4, 4));
double x=0, y=0;
for (int xIndex = 0; xIndex < nx; ++xIndex)
{
for (int yIndex = 0; yIndex < ny; ++yIndex)
{
colorMap->data()->cellToCoord(xIndex, yIndex, &x, &y);
colorMap->data()->setCell(xIndex, yIndex, viskas[xIndex][yIndex]);
}
}
QCPColorScale* colorScale = new QCPColorScale(ui->customPlot);
ui->customPlot->plotLayout()->addElement(0, 1, colorScale);
colorScale->setType(QCPAxis::atRight);
colorMap->setColorScale(colorScale);
colorScale->axis()->setLabelFont(QFont("Times", 10, QFont::Bold));
colorScale->axis()->setLabel("Detektuotas signalas (V)");
colorMap->rescaleDataRange();
QCPMarginGroup* marginGroup = new QCPMarginGroup(ui->customPlot);
ui->customPlot->axisRect()->setMarginGroup(QCP::msBottom | QCP::msTop, marginGroup);
colorScale->setMarginGroup(QCP::msBottom | QCP::msTop, marginGroup);
ui->customPlot->rescaleAxes();
ui->customPlot->replot();
ui->customPlot->update();
colorMap->data()->clear();
MainWindow::setWindowTitle(getFilename());
QString plotcount = QString("%1").arg(ui->customPlot->plottableCount());
}
void MainWindow::on_actionOpen_triggered()
{
setupPlot();
getPath();
readFile();
paintPlot();
and this is the header file
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void readFile();
double viskas[80][106]={};
int nx = 80, ny = 106;
double max_verte=0;
QString getFilename();
QString getPath();
private slots:
void setupPlot();
void paintPlot();
void on_actionOpen_triggered();
void on_actionSave_triggered();
void on_actionSaveas_triggered();
private:
Ui::MainWindow *ui;
QString path;
QString filename;
};
#endif // MAINWINDOW_H
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
