Accessing value of lineEdit in .cpp file- [SOLVED]
-
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. -
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();
@ -
[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_phasefreq/(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;
}
}
}
@ -
-
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 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()
{......
@ -
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 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_OBJECTpublic:
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;
}
@ -
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 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 tosnd_pcm_hw_params_set_rate_resample' wave.cpp:(.text+0x7c6): undefined reference to
snd_strerror'
wave.cpp:(.text+0x7f6): undefined reference tosnd_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.
-
[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.
-
[quote author="mlong" date="1312296267"]What does your .pro file look like?
[/quote]@#-------------------------------------------------
Project created by QtCreator 2011-07-26T15:57:33
#-------------------------------------------------
QT += core gui
TARGET = Wave
TEMPLATE = appSOURCES += main.cpp
wave.cppHEADERS += wave.h
FORMS += wave.ui@