How to access an object inside a privateslot
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:
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 )
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
("|"));
//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
newlineEdit.setText(lineEdit->text());
serie.setPattern("[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}");
//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
Re: How to access an object inside a privateslot
Basic C++ question, make it a member variable.
PS. And there is no need to allocate the string list on the heap.
Re: How to access an object inside a privateslot
arrrg ... :crying:
Thanks
Re: How to access an object inside a privateslot
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?
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 )
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("|"));
//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
newlineEdit.setText(lineEdit->text());
serie.setPattern("");
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
Re: How to access an object inside a privateslot
ufff ... solved it like this:
Code:
void IdDoAutomovelDialog::on_lineEdit_textChanged(){
okButton->setEnabled(lineEdit->hasAcceptableInput());
//Limpar chapa
labelDescSerie->setText("?");
//Verificar qual a série de matrÃ*cula
newlineEdit.setText(lineEdit->text());
serie.setPattern("");
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.pattern(),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.pattern(),0));
}
}
}
}
Re: How to access an object inside a privateslot
Make it an input parameter of your slot. Then do your switch(ipIndex).
Re: How to access an object inside a privateslot
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.
Re: How to access an object inside a privateslot
The original QLineEdit contains something like:
"egexp1"<<"|"<<""regexp"<<"|"<<"egexp3"<<...<<"reg expn"
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;)