Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Accessing value of lineEdit in .cpp file- [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Accessing value of lineEdit in .cpp file- [SOLVED]

Scheduled Pinned Locked Moved Qt Creator and other tools
23 Posts 4 Posters 18.1k 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.
  • O Offline
    O Offline
    ogopa
    wrote on last edited by
    #1

    Hi guys,
    I created a project in Qt creator called wave. I have made a gui platform in wave.ui and i have a lineEdit whose objectName is frequency. In my file wave.cpp, I am trying to access the value put into frequency and I am trying to do this with the line:
    wave::frequency.text
    But I get the error: 'frequency' is not a member of 'wave'.
    What is the right way to do this? I would appreciate any help.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      try in the constructor :
      @ui->frequency @
      if you used the wizard to make your wave project.

      if you need it elsewhere use a connection with signal-slot.

      But I'm only guessing here...

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        It depends on how you created your wave class in relation to the gui. The documentation lists three possible ways. What you tried would work if you inherited from the generated UI:: file. Usually, you don't do that, but use a member variable called ui (or, something else to match your taste) instead. Eddy assumes you use this approach :-)

        The answer above then gives you a pointer to the line edit itself. Its value can then be accessed using the text() method:

        @
        ui->frequency->text();
        @

        Note that frequency is a member variable of ui that you can access directly. It is not a method:
        @
        //wrong:
        ui->frequency()->text();
        @

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Andre, you have a wonderfull way of explaining things!

          @ogopa :
          The bottom line is : if you give us some code or more details we can help you without guessing or forecasting every possible way you could have done things.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • O Offline
            O Offline
            ogopa
            wrote on last edited by
            #5

            [quote author="Eddy" date="1311867800"]Andre, you have a wonderfull way of explaining things!

            @ogopa :
            The bottom line is : if you give us some code or more details we can help you without guessing or forecasting every possible way you could have done things.[/quote]

            Thanks alot guys. The information helped alot. sorry. here is the code of wave.cpp. i couldn't post the entire code because there is a 6000 character limit. So I posted just a little of it which includes line 369 :
            I used your advice and it worked. However. I am getting a new error:

            ./Wave/wave.cpp:369:34: error: ‘ui’ was not declared in this scope

            I'm not sure what it means. Please help

            line 369:@
            double freq = ui->frequency->text().toDouble();@

            @ static int write_loop(snd_pcm_t *handle,
            signed short *samples,
            snd_pcm_channel_area_t *areas)
            {
            double phase = 0;
            signed short *ptr;
            int err, cptr;

                   while (1) {
                          
                           {
            
            • line 369:* double freq = ui->frequency->text().toDouble();
              double ampl = ui->amplitude->text().toDouble();
              const snd_pcm_channel_area_t *areas;
              snd_pcm_uframes_t offset=0;
              int count = period_size;
              double *_phase;
              static double max_phase = 2. * M_PI;
              double phase = _phase;
              double step = max_phase
              freq/(double)rate;
              unsigned char samples[channels];
              int steps[channels];
              unsigned int chn;
              int format_bits = snd_pcm_format_width(format);
              unsigned int maxval = (1 << (format_bits - 1)) - 1;
              int bps = format_bits / 8; /
              bytes per sample */
              int phys_bps = snd_pcm_format_physical_width(format) / 8;
              int big_endian = snd_pcm_format_big_endian(format) == 1;
              int to_unsigned = snd_pcm_format_unsigned(format) == 1;
              int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
              format == SND_PCM_FORMAT_FLOAT_BE);
              float amplitude_scale = ampl/8.56;

                /* verify and prepare the contents of areas */
                for (chn = 0; chn < channels; chn++) {
                        if ((areas[chn].first % 8) != 0) {
                                printf("areas[%i].first == %i, aborting...\n", chn, areas[chn].first);
                                exit(EXIT_FAILURE);
                        }
                        samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
                        if ((areas[chn].step % 16) != 0) {
                                 printf("areas[%i].step == %i, aborting...\n", chn, areas[chn].step);
                                exit(EXIT_FAILURE);
                        }
                        steps[chn] = areas[chn].step / 8;
                        samples[chn] += offset * steps[chn];
                }
                /* fill the channel areas */
                while (count-- > 0) {
                        union {
                                float f;
                                int i;
                        } fval;
                        int res, i;
                        if (is_float) {
                                fval.f = amplitude_scale * sin(phase) * maxval;
                                res = fval.i;
                        } else
                                res = amplitude_scale * sin(phase) * maxval;
                        if (to_unsigned)
                                res ^= 1U << (format_bits - 1);
                        for (chn = 0; chn < channels; chn++) {
                                /* Generate data in native endian format */
                                if (big_endian) {
                                        for (i = 0; i < bps; i++)
                                                *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
                                } else {
                                        for (i = 0; i < bps; i++)
                                                *(samples[chn] + i) = (res >>  i * 8) & 0xff;
                                }
                                samples[chn] += steps[chn];
                        }
                        phase += step;
                        if (phase >= max_phase)
                                phase -= max_phase;
                }
                *_phase = phase;
              

            }
            ptr = samples;
            cptr = period_size;
            while (cptr > 0) {
            err = snd_pcm_writei(handle, ptr, cptr);
            if (err == -EAGAIN)
            continue;
            if (err < 0) {
            if (xrun_recovery(handle, err) < 0) {
            printf("Write error: %s\n", snd_strerror(err));
            exit(EXIT_FAILURE);
            }
            break; /* skip one period */
            }
            ptr += err * channels;
            cptr -= err;
            }
            }
            }
            @

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              if you want to have the value of your lineEdit elsewhere you can use a connect in your constructor :

              After looking into your code I've the impression you want to do that in your write_loop function.
              Here is an example.

              @#include "dialog.h"
              #include "ui_dialog.h"
              #include <QDebug>

              dialog.cpp :

              Dialog::Dialog(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::Dialog)
              {
              ui->setupUi(this);

              connect(ui->lineEdit, SIGNAL(textChanged(QString)), this , SLOT(testFunc(QString)) );
              

              }

              Dialog::~Dialog()
              {
              delete ui;
              }

              void Dialog::testFunc(QString str)
              {
              qDebug() << "teststring : " <<str;
              }@

              dialog.h :

              add in your class :

              @public slots:
              void testFunc(QString str);@

              The above is based on a project generated by QT Creator with a QDialog. In main an instance of this QDialog class is used.

              To understand fully what is happening you could read about "signals and slots":http://doc.qt.nokia.com/4.7/signalsandslots.html. In short : by using the connect Qt always makes sure you get the right value of your LineEdit where you need it.

              PS you can click on the signal and slots tag on the topright of your screen to see more examples, snippets, ... on the forum.

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • O Offline
                O Offline
                ogopa
                wrote on last edited by
                #7

                [quote author="Eddy" date="1311873669"]if you want to have the value of your lineEdit elsewhere you can use a connect in your constructor :

                After looking into your code I've the impression you want to do that in your write_loop function.
                Here is an example.

                @#include "dialog.h"
                #include "ui_dialog.h"
                #include <QDebug>

                dialog.cpp :

                Dialog::Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Dialog)
                {
                ui->setupUi(this);

                connect(ui->lineEdit, SIGNAL(textChanged(QString)), this , SLOT(testFunc(QString)) );
                

                }

                Dialog::~Dialog()
                {
                delete ui;
                }

                void Dialog::testFunc(QString str)
                {
                qDebug() << "teststring : " <<str;
                }@

                dialog.h :

                add in your class :

                @public slots:
                void testFunc(QString str);@

                The above is based on a project generated by QT Creator with a QDialog. In main an instance of this QDialog class is used.

                To understand fully what is happening you could read about "signals and slots":http://doc.qt.nokia.com/4.7/signalsandslots.html. In short : by using the connect Qt always makes sure you get the right value of your LineEdit where you need it.

                PS you can click on the signal and slots tag on the topright of your screen to see more examples, snippets, ... on the forum.[/quote]

                So this is what the first part of my program, wave.cpp looks like(below). I have added the piece of code you gave me. and I also added the lines (directlly below) to the file wave.h. However, I am still getting the same error: ‘ui’ was not declared in this scope, in the same place . I'm not sure whats wrong with it.
                @public slots:
                void testFunc(QString str);
                @

                @#include <QtGui>
                #include <QApplication>
                #include <qlineedit.h>
                #include "wave.h"
                #include "ui_wave.h"
                #include <QString>
                #include <QDebug>
                #include <stdio.h>
                #include <cstdio>
                #include <stdlib.h>
                #include <cstdlib>
                #include <string.h>
                #include <cstring>
                #include <sched.h>
                #include <errno.h>
                #include <getopt.h>
                #include <alsa/asoundlib.h>
                #include <sys/time.h>
                #include <sstream>
                #include <string>
                #include <math.h>
                #include <cmath>
                #include <iostream>
                using namespace std;

                static const char device = "plughw:0,0"; / playback device /
                static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /
                sample format /
                static unsigned int rate = 96000; /
                stream rate /
                static unsigned int channels = 128; /
                count of channels /
                static unsigned int buffer_time = 500000; /
                ring buffer length in us /
                static unsigned int period_time = 100000; /
                period time in us /
                //static double freq = 440; /
                sinusoidal wave frequency in Hz /
                static int verbose = 0; /
                verbose flag /
                static int resample = 1; /
                enable alsa-lib resampling /
                static int period_event = 0; /
                produce poll event after each period */
                static snd_pcm_sframes_t buffer_size;
                static snd_pcm_sframes_t period_size;
                static snd_output_t *output = NULL;
                //static double amplitude = 1;

                wave::wave(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::wave)
                {
                ui->setupUi(this);
                connect(ui->frequency, SIGNAL(textChanged(QString)) , this , SLOT(testFunc(QString)));
                }

                wave::~wave()
                {
                delete ui;
                }

                void wave::testFunc(QString str)
                {
                qDebug() << "teststring: "<<str;
                }

                void wave::on_pushButton_clicked()
                {......
                @

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on last edited by
                  #8

                  Hi ogopa,

                  Do you use a form wit ui file? And is your class based on it?
                  In a previous post you said you got it working. That's why I assumed you did use an ui file.

                  The code I showed you is an example on how you can do things, to learn the general principles. You can not just copy it in your code.

                  Try it out using a new project based on QWidget, choose QDialog in one of the next steps and try my suggestion to learn things.

                  Did you read the signal and slots link I gave you?

                  Here is some explanation:
                  TestFunc is an example slot compararable to your function. Using the connect you can use the value of the lineEdit in your function.

                  Qt Certified Specialist
                  www.edalsolutions.be

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    ogopa
                    wrote on last edited by
                    #9

                    [quote author="Eddy" date="1311879430"]Hi ogopa,

                    Do you use a form wit ui file? And is your class based on it?
                    In a previous post you said you got it working. That's why I assumed you did use an ui file.

                    The code I showed you is an example on how you can do things, to learn the general principles. You can not just copy it in your code.

                    Try it out using a new project based on QWidget, choose QDialog in one of the next steps and try my suggestion to learn things.

                    Did you read the signal and slots link I gave you?

                    Here is some explanation:
                    TestFunc is an example slot compararable to your function. Using the connect you can use the value of the lineEdit in your function.

                    [/quote]

                    Thanks for the help so far. So i've been reading the signals and slots link you sent me and I tried out a simple project with QDialog. I'm still trying to connect the lineEdit to another function, I can't seem to nail it :(. I named the test function write_loop again :p. Below is the cpp file and below that is the header file.

                    dialog.cpp
                    @#include "dialog.h"
                    #include "ui_dialog.h"
                    #include <QDialog>
                    #include <QDebug>
                    #include <iostream>
                    using namespace std;

                    static double txt;

                    dialog::dialog(QWidget *parent) :
                    QDialog(parent),
                    ui(new Ui::dialog)
                    {
                    ui->setupUi(this);
                    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this , SLOT(write_loop(QString)) );
                    }

                    dialog::~dialog()
                    {
                    delete ui;
                    }

                    void dialog::write_loop(QString str)
                    {
                    qDebug() << "teststring : " <<str;
                    }

                    void dialog::on_pushButton_clicked()
                    {
                    txt = ui->lineEdit->text().toDouble();
                    cout<<txt;
                    }

                    static int write_loop(QString)
                    {
                    txt = ui->lineEdit->text().toDouble();
                    cout<<"I have"<<txt;
                    }@

                    dialog.h
                    @#ifndef DIALOG_H
                    #define DIALOG_H

                    #include <QDialog>
                    #include <QDebug>

                    namespace Ui {
                    class dialog;
                    }

                    class dialog : public QDialog
                    {
                    Q_OBJECT

                    public:
                    explicit dialog(QWidget *parent = 0);
                    ~dialog();
                    public slots:
                    void write_loop(QString str);

                    private slots:
                    void on_pushButton_clicked();

                    private:
                    Ui::dialog *ui;
                    };

                    #endif // DIALOG_H
                    @

                    I also tried this, but it didn't work:
                    @static int write_loop(QString)
                    {
                    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this , SLOT(write_loop(QString)) );
                    txt = ui->lineEdit->text().toDouble();
                    cout<<"I have"<<txt;
                    }
                    @

                    1 Reply Last reply
                    0
                    • EddyE Offline
                      EddyE Offline
                      Eddy
                      wrote on last edited by
                      #10

                      Have you changed the value of the lineedit to 5 for instance? Then normally you should get
                      bq. teststring : 5
                      in the application output.

                      What do you have in the application output pane?

                      Qt Certified Specialist
                      www.edalsolutions.be

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        ogopa
                        wrote on last edited by
                        #11

                        I don't have an output because when I build or run, it gives me the error:
                        ./Dialog/dialog.cpp:41:11: error: ‘ui’ was not declared in this scope

                        1 Reply Last reply
                        0
                        • EddyE Offline
                          EddyE Offline
                          Eddy
                          wrote on last edited by
                          #12

                          To what line number in the above code does that point?

                          Qt Certified Specialist
                          www.edalsolutions.be

                          1 Reply Last reply
                          0
                          • O Offline
                            O Offline
                            ogopa
                            wrote on last edited by
                            #13

                            [quote author="Eddy" date="1311969905"]To what line number in the above code does that point?[/quote]

                            line 37:
                            @static int write_loop(QString)
                            {
                            txt = ui->lineEdit->text().toDouble();......................line 37
                            cout<<"I have"<<txt;
                            }@

                            1 Reply Last reply
                            0
                            • EddyE Offline
                              EddyE Offline
                              Eddy
                              wrote on last edited by
                              #14

                              Do you know how to use Qt Creator? Do you know how to debug? Please read the Qt Creator manual if you don't. The purpose of this forum is not that we debug for you. You should be able to narrow down the problems yourself.

                              So your problem is merely about the static variable. Why do you want to use static? Using the signal slots you can have the value of the lineEdit where you want all the times.

                              If you keep only the function with the qDebug line in it then your program should work and you have a solution to your initial problem : using the vale of lineEdit in a function.
                              Hint : comment the others out and start from there.

                              Qt Certified Specialist
                              www.edalsolutions.be

                              1 Reply Last reply
                              0
                              • O Offline
                                O Offline
                                ogopa
                                wrote on last edited by
                                #15

                                [quote author="Eddy" date="1311971610"]Do you know how to use Qt Creator? Do you know how to debug? Please read the Qt Creator manual if you don't. The purpose of this forum is not that we debug for you. You should be able to narrow down the problems yourself.

                                So your problem is merely about the static variable. Why do you want to use static? Using the signal slots you can have the value of the lineEdit where you want all the times.

                                If you keep only the function with the qDebug line in it then your program should work and you have a solution to your initial problem : using the vale of lineEdit in a function.
                                Hint : comment the others out and start from there.[/quote]

                                Yayy, I got it, Thanks alot. I now have new errors. All of them are similar:
                                wave.cpp:(.text+0x7b3): undefined reference to snd_pcm_hw_params_set_rate_resample' wave.cpp:(.text+0x7c6): undefined reference to snd_strerror'
                                wave.cpp:(.text+0x7f6): undefined reference to snd_pcm_hw_params_set_access' wave.cpp:(.text+0x809): undefined reference to snd_strerror'
                                wave.cpp:(.text+0x83c): undefined reference to `snd_pcm_hw_params_set_format'

                                its the same for all others from the alsa sound library. I have included alsa/asoundlib.h, so i'm confused as to why i am getting these errors. Could you please help me.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mlong
                                  wrote on last edited by
                                  #16

                                  Make sure you're linking the asound library, not just include its header files.

                                  Software Engineer
                                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                  1 Reply Last reply
                                  0
                                  • EddyE Offline
                                    EddyE Offline
                                    Eddy
                                    wrote on last edited by
                                    #17

                                    bq. Yayy, I got it, Thanks alot. I now have new errors. All of them are similar:

                                    Glad you made it that far!

                                    You described your new problem much better. Now it's easier for the forum members to understand your problem and help out.

                                    Qt Certified Specialist
                                    www.edalsolutions.be

                                    1 Reply Last reply
                                    0
                                    • O Offline
                                      O Offline
                                      ogopa
                                      wrote on last edited by
                                      #18

                                      [quote author="mlong" date="1311975549"]Make sure you're linking the asound library, not just include its header files.
                                      [/quote]

                                      Hi, I tried searching online for your suggestion as I am not sure how to do this, but I couldn't find anything that could help me. Would you please explain a little more? I'd really appreciate it.

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        mlong
                                        wrote on last edited by
                                        #19

                                        What does your .pro file look like?

                                        Software Engineer
                                        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                        1 Reply Last reply
                                        0
                                        • O Offline
                                          O Offline
                                          ogopa
                                          wrote on last edited by
                                          #20

                                          [quote author="mlong" date="1312296267"]What does your .pro file look like?
                                          [/quote]

                                          wave.pro

                                          @#-------------------------------------------------

                                          Project created by QtCreator 2011-07-26T15:57:33

                                          #-------------------------------------------------

                                          QT += core gui

                                          TARGET = Wave
                                          TEMPLATE = app

                                          SOURCES += main.cpp
                                          wave.cpp

                                          HEADERS += wave.h

                                          FORMS += wave.ui@

                                          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