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. It does not create a chart
Forum Updated to NodeBB v4.3 + New Features

It does not create a chart

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5chartthread
14 Posts 3 Posters 1.5k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    deleted286
    wrote on 11 Jan 2021, 18:21 last edited by
    #1

    I have 2 different threads. In thread one reading data from txt and in thread 2 copy that file into another txt. I want to create a chart via thread 2(which is writingData() ). I have the following code. But there is no chart when i run the code. I have also mythread.h and myhread.cpp

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtCore>
    #include <QMainWindow>
    #include <QtWidgets/QMainWindow>
    #include <QtCharts>
    #include <QChart>
    
    QT_CHARTS_USE_NAMESPACE
    using namespace QtCharts;
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        initChartView();
        connect(m_timer, SIGNAL(timeout()), SLOT(Timer_Slot()));
        m_timer->setInterval(500);
        m_timer->start();
    
        myThreadObject = new mythread();
        myQThread = new QThread();
    
        myThreadObject->moveToThread(myQThread);
    
        connect(this, &MainWindow::startWriting, myThreadObject, &mythread::writeData);
        connect(myThreadObject, &mythread::writingDone, this, &MainWindow::writingDoneByThread);
    
        // Start the new thread
        myQThread->start();
    
    
    }
    
    void MainWindow::writingDoneByThread()
    {
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::initChartView()
    {
        QChart *chart = new QChart();
        chart->ChartTypeCartesian;
        chart->addSeries(series);
        qDebug()<< series;
        QPen green(Qt::red);
        green.setWidth(2);
        series->setPen(green);
        chart->legend()->hide();
        chart->setTitle("deneme");
        chart->setAnimationOptions(QChart::AllAnimations);
    
        QValueAxis *axisX=new QValueAxis;
        axisX->setTickCount(10);
        axisX->setRange(0,5);
        axisX->applyNiceNumbers();
        chart->addAxis(axisX, Qt::AlignBottom);
        series->attachAxis(axisX);
    
        QValueAxis *axisY = new QValueAxis;
        axisY->setTickCount(10);
        axisY->setRange(0, 5);
        axisY->applyNiceNumbers();
        chart->addAxis(axisY, Qt::AlignLeft);
        series->attachAxis(axisY);
    
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
        this->setCentralWidget(chartView);
    }
    
    void MainWindow::Timer_Slot()
    {
        static float q_x;
        if(!queue.isEmpty())
        {
            double num=queue.dequeue();
            q_x += 0.1;
            series->append(q_x, num);
            chart->update();
    
        }
    
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include "mythread.h"
    #include <QMainWindow>
    #include <QThread>
    #include <QtCharts/QChartView>
    #include <QtCharts/QLineSeries>
    #include <QtCharts/QLogValueAxis>
    #include <QtCharts/QValueAxis>
    #include <QTimer>
    
    QT_CHARTS_USE_NAMESPACE
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
    class MainWindow;
    }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = Q_NULLPTR);
        ~MainWindow();
    
        void initChartView();
        void Timer_Slot();
    private:
        Ui::MainWindow *ui;
        mythread* myThreadObject;    //mythreadden yeni bir obje yaratıyorum
        QThread* myQThread;  //QThreadden bir obje yaratıyorum
        QChart *chart = new QChart();
        QLineSeries *series = new QLineSeries();
        QQueue<double> queue;
        QTimer *m_timer = new QTimer();
    signals:
        void startWriting();
    public slots:
        void writingDoneByThread();
    
    };
    #endif // MAINWINDOW_H
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Jan 2021, 18:25 last edited by
      #2

      Hi,

      Your queue is empty because nothing fills it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted286
        wrote on 11 Jan 2021, 18:26 last edited by
        #3

        In here I thought I was adding the datas into the queue

        void mythread::writeData()
        {
        
            QFile::copy("C:/Users/ilknu/Documents/MyThread/deneme.txt", "C:/Users/ilknu/Documents/MyThread/n.txt");
            QFile file2("C:/Users/ilknu/Documents/MyThread/n.txt");
        
            qDebug() << "im working in writeData part " ;
        
            if(file2.open(QIODevice::ReadOnly))
            {
                QTextStream in(&file2);
                while(!in.atEnd())
                {
                    QString line2 = in.readAll();
                    QStringList list2 = line2.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                    for(const QString &entry : list2)
                    {
                        double num = entry.toDouble();
                        queue.enqueue(num);
                        qDebug() << num;
                    }
                }
            }
            file.close();
            emit writingDone();
        }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Jan 2021, 18:28 last edited by
          #4

          That queue variable belongs to your mythread class. It's not the same as the one from your MainWindow class even if it has the same name.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 11 Jan 2021, 18:30
          1
          • S SGaist
            11 Jan 2021, 18:28

            That queue variable belongs to your mythread class. It's not the same as the one from your MainWindow class even if it has the same name.

            D Offline
            D Offline
            deleted286
            wrote on 11 Jan 2021, 18:30 last edited by
            #5

            @SGaist can i connect them, or what can i do for this problem? I read and write in mythread.cpp
            If i move initChartView and timer_slot, is it true?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Jan 2021, 18:48 last edited by
              #6

              @suslucoder said in It does not create a chart:

              If i move initChartView and timer_slot, is it true?

              It won't change anything as these two variables will still be unrelated.

              @suslucoder said in It does not create a chart:

              can i connect them, or what can i do for this problem? I read and write in mythread.cpp

              You have to get the data from your mythread object back to your MainWindow.

              As I already suggested, you really should start with C++ basics before playing with threading.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply 11 Jan 2021, 19:01
              1
              • S SGaist
                11 Jan 2021, 18:48

                @suslucoder said in It does not create a chart:

                If i move initChartView and timer_slot, is it true?

                It won't change anything as these two variables will still be unrelated.

                @suslucoder said in It does not create a chart:

                can i connect them, or what can i do for this problem? I read and write in mythread.cpp

                You have to get the data from your mythread object back to your MainWindow.

                As I already suggested, you really should start with C++ basics before playing with threading.

                D Offline
                D Offline
                deleted286
                wrote on 11 Jan 2021, 19:01 last edited by
                #7

                @SGaist said in It does not create a chart:

                You have to get the data from your mythread object back to your MainWindow.

                What is the way for getting datas from mythread to my mainwindow? I would be very happy if you could show me the way

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 11 Jan 2021, 19:18 last edited by
                  #8

                  Use a signal to transfer the queue. Make it a const reference.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  D 1 Reply Last reply 11 Jan 2021, 20:44
                  1
                  • S SGaist
                    11 Jan 2021, 19:18

                    Use a signal to transfer the queue. Make it a const reference.

                    D Offline
                    D Offline
                    deleted286
                    wrote on 11 Jan 2021, 20:44 last edited by
                    #9

                    @SGaist can you show me an example? I did not understand

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 11 Jan 2021, 20:54 last edited by
                      #10

                      What did you not understand ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      D 1 Reply Last reply 11 Jan 2021, 21:02
                      0
                      • S SGaist
                        11 Jan 2021, 20:54

                        What did you not understand ?

                        D Offline
                        D Offline
                        deleted286
                        wrote on 11 Jan 2021, 21:02 last edited by
                        #11

                        @SGaist if my slot is my queue, what should be the signal. I didnt understand it.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 11 Jan 2021, 21:38 last edited by
                          #12

                          Your slot is not your queue. Did you read the chapter about signals and slots ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          D 1 Reply Last reply 12 Jan 2021, 05:45
                          0
                          • S SGaist
                            11 Jan 2021, 21:38

                            Your slot is not your queue. Did you read the chapter about signals and slots ?

                            D Offline
                            D Offline
                            deleted286
                            wrote on 12 Jan 2021, 05:45 last edited by
                            #13

                            @SGaist yes i did. But i cannot understand how can i get datas

                            J 1 Reply Last reply 12 Jan 2021, 05:58
                            0
                            • D deleted286
                              12 Jan 2021, 05:45

                              @SGaist yes i did. But i cannot understand how can i get datas

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 12 Jan 2021, 05:58 last edited by
                              #14

                              @suslucoder What exactly can't you understand? In the documentation @SGaist posted there is even an example with signal/slot with parameter...

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              6/14

                              11 Jan 2021, 18:48

                              • Login

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