PDA

View Full Version : LineEdit.



Rewo
30th June 2010, 12:00
Hi,

I want to create simple program. I want to use lineEdit for inserting a 9-digit number. I've set maxLength = 9.

Is there any way to make "if" with checking current length of text in lineEdit ? Unfortunately lineEdit->length() doesn't exist.

When I'm trying to make a QString which will take a value of lineEdit, class isn't looking if QString has length = 9.

I tried something like that:


QString lineeditl;
lineeditl = lineEdit->text(); // i think that line isn't working properly (have no 'if' for reference it)
if(lineeditl.length()==9) {
label_2->setText("Correct.");
}

ruben.rodrigues
30th June 2010, 12:20
Hi,

I want to create simple program. I want to use lineEdit for inserting a 9-digit number. I've set maxLength = 9.

Is there any way to make "if" with checking current length of text in lineEdit ? Unfortunately lineEdit->length() doesn't exist.

When I'm trying to make a QString which will take a value of lineEdit, class isn't looking if QString has length = 9.

I tried something like that:


QString lineeditl;
lineeditl = lineEdit->text(); // i think that line isn't working properly (have no 'if' for reference it)
if(lineeditl.length()==9) {
label_2->setText("Correct.");
}

your code looks correct.

are you setting the gui properly?

ui.setupUi(this); //@ the constructor

and I think you should then use ui.lineEdit->text();

Do want to check this as the users types or after pushing a button
if possible place all the code.

Zlatomir
30th June 2010, 12:21
Did you connect the textChanged(QString) signal of the lineEdit with a function/slot that sets the QString lineeditl; ?
Or does the lineEdit has the text from the beginning? (not the use entering the text)

Post a compilable example.

Rewo
30th June 2010, 12:29
@ruben.rodrigues : Gui setting is good, don't worry about it.

@Zlatomir : I tried to use textChanged(QString) signal, but I don't understand why QString parameter is needed. Unfortunately it's not bool signal. LineEdit is empty at start (hasn't got any value).

ruben.rodrigues
30th June 2010, 12:48
can you post the line where you do the connection to the signal textchannged and the function that is called when the text changes?

Rewo
30th June 2010, 12:50
I can't, because I don't know how to use textChanged. Imo it should be bool singal, dunno what QString parameter needing does here.

ruben.rodrigues
30th June 2010, 12:58
I can't, because I don't know how to use textChanged. Imo it should be bool singal, dunno what QString parameter needing does here.

ah! ok

at the constructor of your gui class you need to write a line like this:

connect(lineEdit, SIGNAL(textChanged()), this , SLOT(checkTextLenght());

define the slot checkTextLenght on your header:

private slots:
void checkTextLenght();

and of course programm the function:

void YourClass::checkTextLenght()
{
your code to check the lenght
QString lineeditl;
lineeditl = lineEdit->text(); // i think that line isn't working properly (have no 'if' for reference it)
if(lineeditl.length()==9) {
label_2->setText("Correct.");
}
}

Let me know how it works and if the syntax doesn't match is because I don't have qt on my pc.

Zlatomir
30th June 2010, 13:12
@Zlatomir : I tried to use textChanged(QString) signal, but I don't understand why QString parameter is needed. Unfortunately it's not bool signal. LineEdit is empty at start (hasn't got any value).
The QString parameter it is needed because you need the string.

Right now your QString remains empty...

Create a slot that take a QString parameter, and initialise the lineeditl with that QString, and connect the lineEdit textChanged(QString) with the slot you created (so any time the user change the string you will have the lineeditl updated)

Rewo
30th June 2010, 13:16
@ruben.rodrigues

I did it, but nothing changed.

In communicates these lines appeared:


Object::connect: No such signal QLineEdit::textChanged() in mainwindow.cpp:9
Object::connect: (sender name: 'lineEdit')
Object::connect: (receiver name: 'MainWindow')

Zlatomir
30th June 2010, 13:24
that is because QLineEdit doesn't have a textChanged( VOID) ... it's a textChanged(QString) or QString&

Post the project...

Rewo
30th June 2010, 13:29
It's my mainwindow.cpp (nothing else is needed, because rest of project is correct)


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>

QString lineeditl;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);
//connect(pnt,SIGNAL(triggered()),this,SLOT(pnts())) ;
connect(lineEdit,SIGNAL(textChanged()),this,SLOT(c heckTextLength()));
//if(lineeditl[0] == '7') { label_2->setText("Plus ?"); }
}

void MainWindow::checkTextLength(){
lineeditl = lineEdit->text();
if(lineeditl.length()==9) {
label_2->setText("Correct.");
}
}

MainWindow::~MainWindow()
{
}

Every time I'm trying to put QString in textChanged, it gives me information that:


c:\Qt\4.6.2\include/QtGui/../../src/gui/widgets/qlineedit.h:196: error: 'void QLineEdit::textChanged(const QString&)' is protected

Zlatomir
30th June 2010, 13:36
create a slot in mainWidow something like:


//...
public slots:
void setTextInQString (QString inString) {
lineeditl = inString;
}

And connect the textChanged(QString) with setTextInQString(QString) in mainWindow constructor

And you will need more signals and slots, because it the constructor of mainWidondow the QString will be empty... Only the connections need to be in the constructor!!!

Rewo
30th June 2010, 13:43
It's hard to understand.

I did:

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"

QString lineeditl;

class MainWindow : public QMainWindow, Ui::MainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void setTextInQString (QString inString) {
lineeditl = inString;
}
/*protected slots:
void pnts();
void checkTextLength();*/
};

#endif // MAINWINDOW_H

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>

QString lineeditl;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);
//connect(pnt,SIGNAL(triggered()),this,SLOT(pnts())) ;
connect(lineEdit,SIGNAL(textChanged()),this,SLOT(c heckTextLength()));
//if(lineeditl[0] == '7') { label_2->setText("Plus ?"); }
connect(lineEdit,textChanged(QString),this,setText InQString(QString));
}

void MainWindow::checkTextLength(){
lineeditl = lineEdit->text();
if(lineeditl.length()==9) {
label_2->setText("Correct.");
}
}

MainWindow::~MainWindow()
{
}

Of course - doesn't work.

msrihari
30th June 2010, 13:55
it should be:

connect(lineEdit,SIGNAL(textChanged(QString)),this ,SLOT(setTextInQString(QString)));

Rewo
30th June 2010, 14:07
mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this);
//connect(pnt,SIGNAL(triggered()),this,SLOT(pnts())) ;
connect(lineEdit,SIGNAL(textChanged()),this,SLOT(c heckTextLength()));
//if(lineeditl[0] == '7') { label_2->setText("Plus ?"); }
connect(lineEdit,SIGNAL(textChanged(QString)),this ,SLOT(setTextInQString(QString)));
//,,,SLOT(setTextInQString)
}

MainWindow::~MainWindow()
{
}

void MainWindow::checkTextLength(){
//lineeditl = lineEdit->text();
if(lineeditl.length()==9) {
label_2->setText("Correct.");
}
}


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"

class MainWindow : public QMainWindow, Ui::MainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void setTextInQString (QString inString) {
lineeditl = inString;
}
protected slots:
/*void pnts();*/
void checkTextLength();
};

#endif // MAINWINDOW_H

Problem:


QString lineeditl;

Where should i define it ? There are errors with multiple definition of this var, wherever I'll put that line.

Zlatomir
30th June 2010, 14:10
You have gone in another direction... if you directly check the QString returned you won't need the extra QString variable.

I have wrote a little sample project (it's with a QString variable and a Button to check)

and modified for check directly... (so you have both methods now ;) )

Advice: Read more on signals and slots ;)

Rewo
30th June 2010, 14:20
This program resolves everything. Thank You very much !

lyuts
2nd July 2010, 09:37
Hi,

I want to create simple program. I want to use lineEdit for inserting a 9-digit number. I've set maxLength = 9.

Is there any way to make "if" with checking current length of text in lineEdit ? Unfortunately lineEdit->length() doesn't exist.


Why not just use QRegExpValidator (http://doc.qt.nokia.com/4.6/qregexpvalidator.html)? You regex will look like
[0-9]{,9}