PDA

View Full Version : How to access an object inside a privateslot



graciano
20th January 2009, 18:44
Hi!
I want to use the QStringList *listaMatriculasValidas inside the "void IdDoAutomovelDialog::on_lineEdit_textChanged()" private slot, where the connection is made automatically by the setupUi()!
How can i access it where "x marks the spot" :)
Here is the code:

#include <QtGui>

#include "IdDoAutomovelDialog.h"

IdDoAutomovelDialog:: IdDoAutomovelDialog(QWidget *parent) : QDialog(parent){
setupUi(this);

//Validar a matrÃ*cula introduzida ( http://pt.wikipedia.org/wiki/Matr%C3%ADculas_autom%C3%B3veis_em_Portugal )
QStringList *listaMatriculasValidas = new QStringList;
listaMatriculasValidas->operator <<("[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}");
listaMatriculasValidas->operator <<("[0-9]{2,2}-[0-9]{2,2}-[A-Za-z]{2,2}");
listaMatriculasValidas->operator <<("[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}");
QRegExp regExp(listaMatriculasValidas->join("|"));
lineEdit->setValidator(new QRegExpValidator(regExp, this));

//Definir acções
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}

void IdDoAutomovelDialog::on_lineEdit_textChanged(){
okButton->setEnabled(lineEdit->hasAcceptableInput());

////////////////////////////
// //
// X marks the spot
// //
////////////////////////////

//Limpar chapa
labelDescSerie->setText("?");

//Verificar qual a série de matrÃ*cula
QLineEdit newlineEdit;
newlineEdit.setText(lineEdit->text());

QRegExp serie;
serie.setPattern("[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}");
QRegExpValidator validator(serie,this);

//Chapa de modelo anterior a 1992
newlineEdit.setValidator(&validator);
if(newlineEdit.hasAcceptableInput())
labelDescSerie->setText("Chapa de modelo anterior a 1992");

//Chapa de modelo utilizado entre 1992 e 2005
serie.setPattern("[0-9]{2,2}-[0-9]{2,2}-[A-Za-z]{2,2}");
validator.setRegExp(serie);
if(newlineEdit.hasAcceptableInput())
labelDescSerie->setText("Chapa de modelo utilizado entre 1992 e 2005");

//Chapa de modelo posterior a 2005
serie.setPattern("[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}");
validator.setRegExp(serie);
if(newlineEdit.hasAcceptableInput())
labelDescSerie->setText("Chapa de modelo posterior a 2005");
}


The idea is not to repeat the strings and the use Iteration to optimize the code.
Thanks

jpn
20th January 2009, 19:15
Basic C++ question, make it a member variable.

PS. And there is no need to allocate the string list on the heap.

graciano
20th January 2009, 19:39
arrrg ... :crying:
Thanks

graciano
20th January 2009, 22:40
This is more what i had in mind but now i need to ask a final question.
In line 39 i used the int 1 (one) just to test it.
Is there a way to return the index position in the string list (an integer) to use in there?

#include <QtGui>

#include "IdDoAutomovelDialog.h"

IdDoAutomovelDialog::IdDoAutomovelDialog(QWidget *parent) : QDialog(parent){
setupUi(this);

//Validar a matrÃ*cula introduzida ( http://pt.wikipedia.org/wiki/Matr%C3%ADculas_autom%C3%B3veis_em_Portugal )
listaMatriculasValidas << "[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}";
listaMatriculasValidas << "[0-9]{2,2}-[0-9]{2,2}-[A-Za-z]{2,2}";
listaMatriculasValidas << "[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}";
QRegExp regExp(listaMatriculasValidas.join("|"));
lineEdit->setValidator(new QRegExpValidator(regExp, this));

//Definir acções
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}

void IdDoAutomovelDialog::on_lineEdit_textChanged(){
okButton->setEnabled(lineEdit->hasAcceptableInput());

//Limpar chapa
labelDescSerie->setText("?");

//Verificar qual a série de matrÃ*cula
QLineEdit newlineEdit;
newlineEdit.setText(lineEdit->text());

QRegExp serie;
serie.setPattern("");
QRegExpValidator validator(serie,this);
newlineEdit.setValidator(&validator);
for ( QStringList::Iterator it = listaMatriculasValidas.begin(); it != listaMatriculasValidas.end();++it ){
serie.setPattern(*it);
//serie.setPattern("[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}");
validator.setRegExp(serie);
if(newlineEdit.hasAcceptableInput())
switch(1){
case 1: labelDescSerie->setText("Chapa de modelo anterior a 1992");
break;
case 2: labelDescSerie->setText("Chapa de modelo utilizado entre 1992 e 2005");
break;
case 3: labelDescSerie->setText("Chapa de modelo posterior a 2005");
break;
default:labelDescSerie->setText("???");
}
}
}
Thanks

graciano
21st January 2009, 10:40
ufff ... solved it like this:

void IdDoAutomovelDialog::on_lineEdit_textChanged(){
okButton->setEnabled(lineEdit->hasAcceptableInput());

//Limpar chapa
labelDescSerie->setText("?");

//Verificar qual a série de matrÃ*cula
QLineEdit newlineEdit;
newlineEdit.setText(lineEdit->text());

QRegExp serie;
serie.setPattern("");
QRegExpValidator validator(serie,this);
newlineEdit.setValidator(&validator);
for ( QStringList::Iterator it = listaMatriculasValidas.begin(); it != listaMatriculasValidas.end();++it ){
serie.setPattern(*it);
validator.setRegExp(serie);
if(newlineEdit.hasAcceptableInput()){
switch(listaMatriculasValidas.indexOf(serie.patter n(),0)+1){
case 1: labelDescSerie->setText("Chapa de modelo anterior a 1992");
break;
case 2: labelDescSerie->setText("Chapa de modelo utilizado entre 1992 e 2005");
break;
case 3: labelDescSerie->setText("Chapa de modelo posterior a 2005");
break;
default:labelDescSerie->setNum(listaMatriculasValidas.indexOf(serie.patter n(),0));
}
}
}
}

lyuts
21st January 2009, 11:08
Make it an input parameter of your slot. Then do your switch(ipIndex).

jpn
21st January 2009, 13:26
Something I don't get is that a slot connected to QLineEdit::textChanged() creates another QLineEdit every time the text of the former line edit changes.

graciano
21st January 2009, 18:09
The original QLineEdit contains something like:
"egexp1"<<"|"<<""regexp"<<"|"<<"egexp3"<<...<<"regexpn"
This way i can validade entries that satisfy regexp1 or regexp2 or ....

Inside the slot the newLineEdit is used to validate a specific regexp(i) so that i can use it to obtain an index in the string list.

Perhaps it is not the cleanest aproach but it is working.

If you want to take a look and comment it i would apreciate:

http://www.box.net/shared/fpq98hvito

Still trying to think Qt;)