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. How to pass the values from the GUI to the main.cpp to execute a calculation?
Forum Updated to NodeBB v4.3 + New Features

How to pass the values from the GUI to the main.cpp to execute a calculation?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.7k 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.
  • SGaistS SGaist

    Hi,

    Why not integrate the Qwt stuff in your main widget ? That would likely simplify your code.

    F Offline
    F Offline
    Flavio Mesquita
    wrote on last edited by
    #4

    @SGaist I tried waht u said, although it solves the problem of passing the arguments, it doesn´t work. it says:
    : error: no matching function for call to 'QVector<QPointF>::QVector(double&, double&, double&)'
    QVector<QPointF> ricker(f=10, dt=0.008, length = 60.0)

    Doesn´t matter where i put the vector inside the code. ^

    jsulmJ 1 Reply Last reply
    0
    • F Flavio Mesquita

      @SGaist I tried waht u said, although it solves the problem of passing the arguments, it doesn´t work. it says:
      : error: no matching function for call to 'QVector<QPointF>::QVector(double&, double&, double&)'
      QVector<QPointF> ricker(f=10, dt=0.008, length = 60.0)

      Doesn´t matter where i put the vector inside the code. ^

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #5

      @Flavio-Mesquita said in How to pass the values from the GUI to the main.cpp to execute a calculation?:

      QVector<QPointF> ricker(f=10, dt=0.008, length = 60.0)

      This code is invalid.
      Should be

      QVector<QPointF> ricker;
      ricker.append(QPointF(x, y));
      

      See http://doc.qt.io/qt-5/qpointf.html and http://doc.qt.io/qt-5/qvector.html
      There is no QPointF constructor taking 3 arguments also it does not know anything about f, dt or length, so I'm not sure what you're trying to achieve.

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

      F 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Flavio-Mesquita said in How to pass the values from the GUI to the main.cpp to execute a calculation?:

        QVector<QPointF> ricker(f=10, dt=0.008, length = 60.0)

        This code is invalid.
        Should be

        QVector<QPointF> ricker;
        ricker.append(QPointF(x, y));
        

        See http://doc.qt.io/qt-5/qpointf.html and http://doc.qt.io/qt-5/qvector.html
        There is no QPointF constructor taking 3 arguments also it does not know anything about f, dt or length, so I'm not sure what you're trying to achieve.

        F Offline
        F Offline
        Flavio Mesquita
        wrote on last edited by
        #6

        @jsulm Thanks for pointing, I was trying to set the arguments in order to make the function run. The f argument has to be passed at : curve->setSamples(ricker(f));. At the example below i chose f=10. Anyway, I dont get why when I make a Qt console application it works fine. If u have qwt in ur computer, u can simply copy and paste it this file and run, it will work.
        I know that in order to put it in a project with a GUI, where the arguments f, dt and polarity have to be passed from the GUI to the function I need to do something else, maybe SIGNAL/SLOT I just dont know how and where to put it.

        I put the code like that:

        #include <qapplication.h>
        #include <qwt_plot.h>
        #include <qwt_plot_curve.h>
        #include <qwt_plot_grid.h>
        #include <qwt_symbol.h>
        #include <qwt_legend.h>
        #include <cmath>

        const double pi = 3.14159265358979323846;
        int polarity=-1;

        QVector<QPointF> ricker(double f, double dt = 0.001,double length = 60.0 )
        {
        size_t N = (length - dt/2.0)/dt;
        QVector<QPointF> w(N);
        for (size_t i = 0; i < N; ++i)
        {
        double t = -length/2 + idt;
        w[i].setX(t);
        w[i].setY(polarity
        ((1.0 - 2pipifftt) * exp(-pipifft*t)));
        }
        return w;
        }

        int main( int argc, char **argv )
        {
        QApplication a( argc, argv );

        QwtPlot plot;
        plot.setTitle( "Ricker Wavelet" );
        plot.setCanvasBackground( Qt::white );
        plot.setAxisScale( QwtPlot::xBottom, -0.25, 0.25 );
        plot.setAxisScale( QwtPlot::yLeft, -1.0, 1.0 );
        plot.insertLegend( new QwtLegend() );
        
        QwtPlotGrid *grid = new QwtPlotGrid();
        grid->attach( &plot );
        
        QwtPlotCurve *curve = new QwtPlotCurve();
        curve->setTitle( "Wavelet" );
        curve->setPen( Qt::blue, 4 ),
        curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
        
        curve->setSamples(ricker(10));
        curve->attach( &plot );
        plot.resize(800, 600);
        plot.show();
        return a.exec();
        

        }

        jsulmJ 1 Reply Last reply
        0
        • M Offline
          M Offline
          MrShawn
          wrote on last edited by
          #7

          Hi,

          You need a qobject child with slots to attach your signals to.

          I see you are generating everything programatically which is fine but in main you have nothing to connect your signal to.

          Take your code and put it in a constructor for some class inheriting QObject. (you will need to make your ptrs into class data otherwise you will lose your pointers after the constructor.).

          After you have that in a QObject you can create the slots that you need to connect the signals from your different ui components.

          Also I suggest taking a step back... It looks like you started with console application, and are now using some UI stuff. I would start with with widget application and build your UI in the designer. (this will allow you to access the components that will eventually feed the data into your ricker function). I would then integrate your code into the mainwindow class just as @SGaist eluded to.

          Either way to get your signals and slots your going to need a class that inherits from QObject somehow and calls the proper code that you need to implement.

          Good Luck,
          Shawn

          1 Reply Last reply
          1
          • F Flavio Mesquita

            @jsulm Thanks for pointing, I was trying to set the arguments in order to make the function run. The f argument has to be passed at : curve->setSamples(ricker(f));. At the example below i chose f=10. Anyway, I dont get why when I make a Qt console application it works fine. If u have qwt in ur computer, u can simply copy and paste it this file and run, it will work.
            I know that in order to put it in a project with a GUI, where the arguments f, dt and polarity have to be passed from the GUI to the function I need to do something else, maybe SIGNAL/SLOT I just dont know how and where to put it.

            I put the code like that:

            #include <qapplication.h>
            #include <qwt_plot.h>
            #include <qwt_plot_curve.h>
            #include <qwt_plot_grid.h>
            #include <qwt_symbol.h>
            #include <qwt_legend.h>
            #include <cmath>

            const double pi = 3.14159265358979323846;
            int polarity=-1;

            QVector<QPointF> ricker(double f, double dt = 0.001,double length = 60.0 )
            {
            size_t N = (length - dt/2.0)/dt;
            QVector<QPointF> w(N);
            for (size_t i = 0; i < N; ++i)
            {
            double t = -length/2 + idt;
            w[i].setX(t);
            w[i].setY(polarity
            ((1.0 - 2pipifftt) * exp(-pipifft*t)));
            }
            return w;
            }

            int main( int argc, char **argv )
            {
            QApplication a( argc, argv );

            QwtPlot plot;
            plot.setTitle( "Ricker Wavelet" );
            plot.setCanvasBackground( Qt::white );
            plot.setAxisScale( QwtPlot::xBottom, -0.25, 0.25 );
            plot.setAxisScale( QwtPlot::yLeft, -1.0, 1.0 );
            plot.insertLegend( new QwtLegend() );
            
            QwtPlotGrid *grid = new QwtPlotGrid();
            grid->attach( &plot );
            
            QwtPlotCurve *curve = new QwtPlotCurve();
            curve->setTitle( "Wavelet" );
            curve->setPen( Qt::blue, 4 ),
            curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
            
            curve->setSamples(ricker(10));
            curve->attach( &plot );
            plot.resize(800, 600);
            plot.show();
            return a.exec();
            

            }

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @Flavio-Mesquita I'm not sure what the problem is now. In your example at the beginning you already read all this information from the UI, so what is not working?

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

            F 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Flavio-Mesquita I'm not sure what the problem is now. In your example at the beginning you already read all this information from the UI, so what is not working?

              F Offline
              F Offline
              Flavio Mesquita
              wrote on last edited by
              #9

              @jsulm The way I found to pass the argument f from the interface to the main was using QProcess, like this:

              Inserting a void in the interface:

              void interface2::wavelet()
              {

              std::cin >>f;
              std::cout << f <<endl;
              }

              And adding these lines before the Qwt stuff on the main code:

              QProcess p1;
              p1.start("interface2.exe");
              p1.write("f\n");
              QObject::connect(&p1, &QProcess::readyRead, &p1{
              //output to qDebug, you may want to update some GUI component instead
              qDebug() << p1.readAll();
              });

              Although it doesn´t give any erros while compiling it crashes, I believe it is some type error in the variable f, since it is declared as double on the ricker vector, and write sends it like a qint64. Is there anyway to define the type inside write ()?

              jsulmJ 2 Replies Last reply
              0
              • F Flavio Mesquita

                @jsulm The way I found to pass the argument f from the interface to the main was using QProcess, like this:

                Inserting a void in the interface:

                void interface2::wavelet()
                {

                std::cin >>f;
                std::cout << f <<endl;
                }

                And adding these lines before the Qwt stuff on the main code:

                QProcess p1;
                p1.start("interface2.exe");
                p1.write("f\n");
                QObject::connect(&p1, &QProcess::readyRead, &p1{
                //output to qDebug, you may want to update some GUI component instead
                qDebug() << p1.readAll();
                });

                Although it doesn´t give any erros while compiling it crashes, I believe it is some type error in the variable f, since it is declared as double on the ricker vector, and write sends it like a qint64. Is there anyway to define the type inside write ()?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @Flavio-Mesquita Now I'm completely lost - why is your interface a stand alone application now?
                And how it can crash while compiling? Do you maybe mean it does not compile and shows you a compiler error?

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

                1 Reply Last reply
                0
                • F Flavio Mesquita

                  @jsulm The way I found to pass the argument f from the interface to the main was using QProcess, like this:

                  Inserting a void in the interface:

                  void interface2::wavelet()
                  {

                  std::cin >>f;
                  std::cout << f <<endl;
                  }

                  And adding these lines before the Qwt stuff on the main code:

                  QProcess p1;
                  p1.start("interface2.exe");
                  p1.write("f\n");
                  QObject::connect(&p1, &QProcess::readyRead, &p1{
                  //output to qDebug, you may want to update some GUI component instead
                  qDebug() << p1.readAll();
                  });

                  Although it doesn´t give any erros while compiling it crashes, I believe it is some type error in the variable f, since it is declared as double on the ricker vector, and write sends it like a qint64. Is there anyway to define the type inside write ()?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  @Flavio-Mesquita Also if you really want you app to be a stand alone application then you're using QProcess wrongly: you are passing string "f\n" to it and not the value of variable f.

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

                  F 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Flavio-Mesquita Also if you really want you app to be a stand alone application then you're using QProcess wrongly: you are passing string "f\n" to it and not the value of variable f.

                    F Offline
                    F Offline
                    Flavio Mesquita
                    wrote on last edited by
                    #12

                    @jsulm I was suspecting I was doing something wrong there, I´m reading how to use QProcess right now, I got thi idea from an example I found in a forum. How would I do the pass the variable f trough it?

                    jsulmJ 1 Reply Last reply
                    0
                    • F Flavio Mesquita

                      @jsulm I was suspecting I was doing something wrong there, I´m reading how to use QProcess right now, I got thi idea from an example I found in a forum. How would I do the pass the variable f trough it?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      @Flavio-Mesquita It depends. Do you want to pass this variable as parameter when starting the process? If so then:

                      QStringList arguments;
                      arguments << f;
                      myProcess->start(program, arguments);
                      

                      See http://doc.qt.io/qt-5/qprocess.html
                      Else

                      myProcess->write(QByteArray::number(f));
                      

                      But you're aware that you will need to read it from stdin in your GUI app?

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

                      F 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Flavio-Mesquita It depends. Do you want to pass this variable as parameter when starting the process? If so then:

                        QStringList arguments;
                        arguments << f;
                        myProcess->start(program, arguments);
                        

                        See http://doc.qt.io/qt-5/qprocess.html
                        Else

                        myProcess->write(QByteArray::number(f));
                        

                        But you're aware that you will need to read it from stdin in your GUI app?

                        F Offline
                        F Offline
                        Flavio Mesquita
                        wrote on last edited by
                        #14

                        @jsulm I finally got it "working", did the way sgaist said, I was forgetting to declare the Qvector on the header., that was all.
                        like this:
                        QVector<QPointF> ricker(double f, double dt, double length );

                        The only issue now, is the plot that closes too fast, I just see the window for miliseconds and disapear, how do I fix it?

                        1 Reply Last reply
                        0

                        • Login

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