PDA

View Full Version : Qt 4.8: How to use setText() of QLineEdit.



amitahire
26th June 2012, 05:53
I recently migrated from Qt 3 to Qt4.8 ( I know huge jump.) Was restricted to work on qt3 before. Anyway, I was trying out this particular program, where I am creating two QLineEdit boxes and trying to to get the text of l1 to l2 (l1 and l2 bring the names of the QLineEdit respectively.) For this I was created the signal and slots but when I compile I get this error "Undefined reference to setText()". I was using the signal textChanged() of l1 and slot of l2 - setText(). Think anyone can help me on this?



I think I am just missing something. Got gambled with all the qt3 and 4 concepts. Please pardon my poor code and concepts. Patience would be appriciated. Thanks.
try1.cpp

#include<QtGui>
#include<QDialog>
#include<QLineEdit>
#include<QLabel>
#include<QPushButton>
#include "try1.h"
#include "ui_try1.h"

try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
{
//ui->setupUi(this);
connect(ui->l1,SIGNAL(textChanged(const QString &)),ui->l2,SLOT(setText(const QString &)) );
//connect(ui->Quit,SIGNAL(clicked()),this,SLOT(close()));
}

try1.h

#ifndef TRY1_H
#define TRY1_H
#include "ui_try1.h"
#include<QDialog>
#include<QLineEdit>
#include<QLabel>
#include<QPushButton>
//class ;
//class QLineEdit;
//class QPushButton;

namespace Ui
{
class Dialog;
}


class try1:public QDialog
{
Q_OBJECT
public:
try1(QWidget *parent=0);
private:
Ui::Dialog *ui;
signals:
void textChanged(const QString &text);
// void clicked();
private slots:
void setText(const QString &text);
//void close();
private:
QLabel *l1;
QLabel *l2;
QLineEdit *t1;
QLineEdit *t2;
QPushButton *Quit;
};

#endif



main.cpp (init)

#include<QtGui>
#include<QApplication>
#include<QDialog>
#include "try1.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
try1 d;
d.show();
return app.exec();
}



Also attaching the zip file of the project. And again really sorry for messy code.

myta212
26th June 2012, 06:21
Hi,
Please use this code.
try1.cpp


#include "try1.h"

try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->l1, SIGNAL(textChanged(const QString &)), ui->l2,SLOT(settext(const QString &)) );
connect(this, SIGNAL(settText(QString)), ui->l2, SLOT(setText(QString)));
}

void try1::settext(const QString a)
{
emit settText(a);
}


try1.h


#ifndef TRY1_H
#define TRY1_H
#include "ui_try1.h"
#include<QDialog>
#include<QLineEdit>
#include<QLabel>
#include<QPushButton>


namespace Ui
{
class Dialog;
}


class try1:public QDialog
{
Q_OBJECT
public:
try1(QWidget *parent=0);

private:
Ui::Dialog *ui;

signals:
void settText(QString);

private slots:
void settext(const QString a);

private:
QLabel *l1;
QLabel *l2;
QLineEdit *t1;
QLineEdit *t2;
QPushButton *Quit;
};

#endif



Best regards,

Myta

amitahire
26th June 2012, 06:30
Thanks for replying Myta. But I dont understand what you are trying to do a bit more explanation could have helped.

From what I guessed - you are creating a seperate settext ( with a small t?) or is it a typo? Why the need to create a seperate signal?


Hi,
Please use this code.
try1.cpp


#include "try1.h"

try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->l1, SIGNAL(textChanged(const QString &)), ui->l2,SLOT(settext(const QString &)) );
connect(this, SIGNAL(settText(QString)), ui->l2, SLOT(setText(QString)));
}

void try1::settext(const QString a)
{
emit settText(a);
}


try1.h


#ifndef TRY1_H
#define TRY1_H
#include "ui_try1.h"
#include<QDialog>
#include<QLineEdit>
#include<QLabel>
#include<QPushButton>


namespace Ui
{
class Dialog;
}


class try1:public QDialog
{
Q_OBJECT
public:
try1(QWidget *parent=0);

private:
Ui::Dialog *ui;

signals:
void settText(QString);

private slots:
void settext(const QString a);

private:
QLabel *l1;
QLabel *l2;
QLineEdit *t1;
QLineEdit *t2;
QPushButton *Quit;
};

#endif



Best regards,

Myta

myta212
26th June 2012, 06:53
Hi,
I am try to separate signal and slot from QLineEdit 1 and QLineEdit2.
This is more simple than above code. You dont need to add signal/slot setText in your header file. Because this function exist in QLineEdit function.
Your code is right, but You must remove const variable in your cpp code. Please check this code :

try1.cpp


#include "try1.h"

try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->l1, SIGNAL(textChanged(QString)), ui->l2, SLOT(setText(QString)) );
}


try1.h


#ifndef TRY1_H
#define TRY1_H
#include "ui_try1.h"
#include<QDialog>
#include<QLineEdit>
#include<QLabel>
#include<QPushButton>


namespace Ui
{
class Dialog;
}


class try1:public QDialog
{
Q_OBJECT
public:
try1(QWidget *parent=0);

private:
Ui::Dialog *ui;

private:
QLabel *l1;
QLabel *l2;
QLineEdit *t1;
QLineEdit *t2;
QPushButton *Quit;
};

#endif



Best regards,

Myta

amitahire
26th June 2012, 07:03
Thanks a lot Myta. Its works. So if the signal and slots are predefined you dont have to mention them in the .h file? As soon as I removed them it was working.

Thanks again.


Hi,
I am try to separate signal and slot from QLineEdit 1 and QLineEdit2.
This is more simple than above code. You dont need to add signal/slot setText in your header file. Because this function exist in QLineEdit function.
Your code is right, but You must remove const variable in your cpp code. Please check this code :

try1.cpp


#include "try1.h"

try1::try1(QWidget *parent):QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->l1, SIGNAL(textChanged(QString)), ui->l2, SLOT(setText(QString)) );
}


try1.h


#ifndef TRY1_H
#define TRY1_H
#include "ui_try1.h"
#include<QDialog>
#include<QLineEdit>
#include<QLabel>
#include<QPushButton>


namespace Ui
{
class Dialog;
}


class try1:public QDialog
{
Q_OBJECT
public:
try1(QWidget *parent=0);

private:
Ui::Dialog *ui;

private:
QLabel *l1;
QLabel *l2;
QLineEdit *t1;
QLineEdit *t2;
QPushButton *Quit;
};

#endif



Best regards,

Myta