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 20.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.
  • 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
                            • M Offline
                              M Offline
                              mlong
                              wrote on last edited by
                              #21

                              It's not enough to simply include the <alsa/asoundlib.h> header in your code. You also have to tell the linker that you want to use the asound library. Use the LIBS variable in your .pro file to do so. I don't know for sure what the proper library name to use is (off the top of my head.) Probably want to use -Lasound or something like that.

                              There are a number of threads that discuss how to include libraries.

                              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
                                #22

                                Thanks alot, I included this line in my wave.pro and it worked:
                                @LIBS += -lasound@

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

                                  Good deal! Be sure to change the title of the thread to add [Solved] if everything is working now.

                                  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

                                  • Login

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