Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. External Variables not transferring
Forum Updated to NodeBB v4.3 + New Features

External Variables not transferring

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 158 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thinkingcow
    wrote on last edited by thinkingcow
    #1

    Hi, I'm trying to create a program that takes inputs from an external file "C:/Repo/GraphTestData.txt". This takes place in my main.cpp file, where it is parsed into two double QVectors x and y. Then, I externally export x and y to a shared header "data.h", where it gets accessed in another file named "networkplot.cpp". Here, I use QCustomPlot to plot the data onto a graph. However, the console says that data.h is not used directly by either of the cpp files. How can I fix this?

    // data.h
    #ifndef DATA_H
    #define DATA_H
    
    #include <QVector>
    
    extern QVector<double> x;
    extern QVector<double> y;
    
    #endif // DATA_H
    
    #ifndef NETWORKPLOT_H
    #define NETWORKPLOT_H
    
    #include <QMainWindow>
    #include <QVector>
    QT_BEGIN_NAMESPACE
    namespace Ui { class networkplot; }
    QT_END_NAMESPACE
    
    class networkplot : public QMainWindow
    {
        Q_OBJECT
    
    public:
        networkplot(QWidget *parent = nullptr);
        ~networkplot();
    
    private:
        Ui::networkplot *ui;
    };
    #endif // NETWORKPLOT_H
    
    #include "networkplot.h"
    #include "ui_networkplot.h"
    #include <QDebug>
    #include "data.h"
    
    networkplot::networkplot(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::networkplot)
    {
        ui->setupUi(this);
        extern QVector<double> x, y;
        /*QVector<double> x(100),y(100);
        for(int i=0;i<100;i++)
        {
            x[i] = i/50.0 - 1; // x goes from -1 to 1
            y[i] = x[i]*x[i]; // quadratic function
        }*/
        ui->customplot->addGraph();
    
        ui->customplot->graph(0)->setScatterStyle(QCPScatterStyle::ssDot);
        ui->customplot->graph()->setLineStyle(QCPGraph::lsLine);
        ui->customplot->xAxis->setLabel("X");
        ui->customplot->yAxis->setLabel("Y");
        ui->customplot->yAxis->setRange(-6000,100);
        ui->customplot->yAxis->setRange(-6000,8000);
    
        ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
        ui->customplot->graph(0)->setData(x,y);
        ui->customplot->rescaleAxes();
        ui->customplot->replot();
        ui->customplot->update();
    }
    
    networkplot::~networkplot()
    {
        delete ui;
    }
    
    
    #include "networkplot.h"
    #include <QApplication>
    #include <QFile>
    #include <QTextStream>
    #include <QVector>
    #include <QDebug>
    #include "data.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        // File path
        QString filePath = "C:/Repo/GraphTestData.txt";
    
        // Vectors to store data
        QVector<double> x;
        QVector<double> y;
    
        // Open the file
        QFile file(filePath);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qDebug() << "Error opening file:" << file.errorString();
            return -1;
        }
    
        // Read the file line by line
        QTextStream in(&file);
        while (!in.atEnd()) {
            // Read each line
            QString line = in.readLine();
    
            // Split the line into two doubles
            QStringList parts = line.split(" ");
            if (parts.size() == 2) {
                bool conversionX, conversionY;
                double numberX = parts[0].toDouble(&conversionX);
                double numberY = parts[1].toDouble(&conversionY);
    
                // Check if conversion was successful
                if (conversionX && conversionY) {
                    // Append to vectors
                    x.append(numberX);
                    y.append(numberY);
                } else {
                    qDebug() << "Error converting line to numbers:" << line;
                }
            } else {
                qDebug() << "Invalid line format:" << line;
            }
        }
    
        // Close the file
        file.close();
    
        // Print the content of vectors
        qDebug() << "Vector x:" << x;
        qDebug() << "Vector y:" << y;
        networkplot w;
        w.show();
        return a.exec();
    }
    
    

    61616dbd-ec25-4bfd-873a-3d7beeccef1f-image.png
    939edb75-b88d-4ca0-b5fd-e9809c3a8757-image.png

    It's my first time working with C++, so I would greatly appreciate some help!

    JonBJ 1 Reply Last reply
    0
    • T thinkingcow

      Hi, I'm trying to create a program that takes inputs from an external file "C:/Repo/GraphTestData.txt". This takes place in my main.cpp file, where it is parsed into two double QVectors x and y. Then, I externally export x and y to a shared header "data.h", where it gets accessed in another file named "networkplot.cpp". Here, I use QCustomPlot to plot the data onto a graph. However, the console says that data.h is not used directly by either of the cpp files. How can I fix this?

      // data.h
      #ifndef DATA_H
      #define DATA_H
      
      #include <QVector>
      
      extern QVector<double> x;
      extern QVector<double> y;
      
      #endif // DATA_H
      
      #ifndef NETWORKPLOT_H
      #define NETWORKPLOT_H
      
      #include <QMainWindow>
      #include <QVector>
      QT_BEGIN_NAMESPACE
      namespace Ui { class networkplot; }
      QT_END_NAMESPACE
      
      class networkplot : public QMainWindow
      {
          Q_OBJECT
      
      public:
          networkplot(QWidget *parent = nullptr);
          ~networkplot();
      
      private:
          Ui::networkplot *ui;
      };
      #endif // NETWORKPLOT_H
      
      #include "networkplot.h"
      #include "ui_networkplot.h"
      #include <QDebug>
      #include "data.h"
      
      networkplot::networkplot(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::networkplot)
      {
          ui->setupUi(this);
          extern QVector<double> x, y;
          /*QVector<double> x(100),y(100);
          for(int i=0;i<100;i++)
          {
              x[i] = i/50.0 - 1; // x goes from -1 to 1
              y[i] = x[i]*x[i]; // quadratic function
          }*/
          ui->customplot->addGraph();
      
          ui->customplot->graph(0)->setScatterStyle(QCPScatterStyle::ssDot);
          ui->customplot->graph()->setLineStyle(QCPGraph::lsLine);
          ui->customplot->xAxis->setLabel("X");
          ui->customplot->yAxis->setLabel("Y");
          ui->customplot->yAxis->setRange(-6000,100);
          ui->customplot->yAxis->setRange(-6000,8000);
      
          ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
          ui->customplot->graph(0)->setData(x,y);
          ui->customplot->rescaleAxes();
          ui->customplot->replot();
          ui->customplot->update();
      }
      
      networkplot::~networkplot()
      {
          delete ui;
      }
      
      
      #include "networkplot.h"
      #include <QApplication>
      #include <QFile>
      #include <QTextStream>
      #include <QVector>
      #include <QDebug>
      #include "data.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          // File path
          QString filePath = "C:/Repo/GraphTestData.txt";
      
          // Vectors to store data
          QVector<double> x;
          QVector<double> y;
      
          // Open the file
          QFile file(filePath);
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              qDebug() << "Error opening file:" << file.errorString();
              return -1;
          }
      
          // Read the file line by line
          QTextStream in(&file);
          while (!in.atEnd()) {
              // Read each line
              QString line = in.readLine();
      
              // Split the line into two doubles
              QStringList parts = line.split(" ");
              if (parts.size() == 2) {
                  bool conversionX, conversionY;
                  double numberX = parts[0].toDouble(&conversionX);
                  double numberY = parts[1].toDouble(&conversionY);
      
                  // Check if conversion was successful
                  if (conversionX && conversionY) {
                      // Append to vectors
                      x.append(numberX);
                      y.append(numberY);
                  } else {
                      qDebug() << "Error converting line to numbers:" << line;
                  }
              } else {
                  qDebug() << "Invalid line format:" << line;
              }
          }
      
          // Close the file
          file.close();
      
          // Print the content of vectors
          qDebug() << "Vector x:" << x;
          qDebug() << "Vector y:" << y;
          networkplot w;
          w.show();
          return a.exec();
      }
      
      

      61616dbd-ec25-4bfd-873a-3d7beeccef1f-image.png
      939edb75-b88d-4ca0-b5fd-e9809c3a8757-image.png

      It's my first time working with C++, so I would greatly appreciate some help!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @thinkingcow
      I think you can find more information for the "fix available" by hovering over the warning message or clicking something.

      Anyway, data.h declares that there are two global variables somewhere, x and y (btw really bad names for global variables).

      But in fact no such global variables are actually defined anywhere in your code (might be in a data.cpp but you don't show any such file). And the 2 extern variables are not referenced anywhere. networkplot::networkplot has its own extern statement for them, main() defines an x & y but they are local variables, not globals.

      You may be new to C++ but presumably not to C, else you would likely not know about extern. So the code question you have is really about C rather than C++.

      Don't use any extern here, you don't need it. By the look of things, pass x & y as parameters from where they are defined in main() to your networkplot class instance. Probably as extra parameters to the constructor. Or via a separate method, plot(), which you call after construction that does just the ui->customplot->graph(0)->setData(x,y); part.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved