Results 1 to 8 of 8

Thread: How to access an object inside a privateslot

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to access an object inside a privateslot

    Hi!
    I want to use the QStringList *listaMatriculasValidas inside the "void IdDoAutomovelDialog:n_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:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "IdDoAutomovelDialog.h"
    4.  
    5. IdDoAutomovelDialog:: IdDoAutomovelDialog(QWidget *parent) : QDialog(parent){
    6. setupUi(this);
    7.  
    8. //Validar a matrÃ*cula introduzida ( http://pt.wikipedia.org/wiki/Matr%C3%ADculas_autom%C3%B3veis_em_Portugal )
    9. QStringList *listaMatriculasValidas = new QStringList;
    10. listaMatriculasValidas->operator <<("[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}");
    11. listaMatriculasValidas->operator <<("[0-9]{2,2}-[0-9]{2,2}-[A-Za-z]{2,2}");
    12. listaMatriculasValidas->operator <<("[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}");
    13. QRegExp regExp(listaMatriculasValidas->join("|"));
    14. lineEdit->setValidator(new QRegExpValidator(regExp, this));
    15.  
    16. //Definir acções
    17. connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    18. connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    19. }
    20.  
    21. void IdDoAutomovelDialog::on_lineEdit_textChanged(){
    22. okButton->setEnabled(lineEdit->hasAcceptableInput());
    23.  
    24. ////////////////////////////
    25. // //
    26. // X marks the spot
    27. // //
    28. ////////////////////////////
    29.  
    30. //Limpar chapa
    31. labelDescSerie->setText("?");
    32.  
    33. //Verificar qual a série de matrÃ*cula
    34. QLineEdit newlineEdit;
    35. newlineEdit.setText(lineEdit->text());
    36.  
    37. QRegExp serie;
    38. serie.setPattern("[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}");
    39. QRegExpValidator validator(serie,this);
    40.  
    41. //Chapa de modelo anterior a 1992
    42. newlineEdit.setValidator(&validator);
    43. if(newlineEdit.hasAcceptableInput())
    44. labelDescSerie->setText("Chapa de modelo anterior a 1992");
    45.  
    46. //Chapa de modelo utilizado entre 1992 e 2005
    47. serie.setPattern("[0-9]{2,2}-[0-9]{2,2}-[A-Za-z]{2,2}");
    48. validator.setRegExp(serie);
    49. if(newlineEdit.hasAcceptableInput())
    50. labelDescSerie->setText("Chapa de modelo utilizado entre 1992 e 2005");
    51.  
    52. //Chapa de modelo posterior a 2005
    53. serie.setPattern("[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}");
    54. validator.setRegExp(serie);
    55. if(newlineEdit.hasAcceptableInput())
    56. labelDescSerie->setText("Chapa de modelo posterior a 2005");
    57. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  3. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to access an object inside a privateslot

    arrrg ...
    Thanks

  4. #4
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "IdDoAutomovelDialog.h"
    4.  
    5. IdDoAutomovelDialog::IdDoAutomovelDialog(QWidget *parent) : QDialog(parent){
    6. setupUi(this);
    7.  
    8. //Validar a matrÃ*cula introduzida ( http://pt.wikipedia.org/wiki/Matr%C3%ADculas_autom%C3%B3veis_em_Portugal )
    9. listaMatriculasValidas << "[A-Za-z]{2,2}-[0-9]{2,2}-[0-9]{2,2}";
    10. listaMatriculasValidas << "[0-9]{2,2}-[0-9]{2,2}-[A-Za-z]{2,2}";
    11. listaMatriculasValidas << "[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}";
    12. QRegExp regExp(listaMatriculasValidas.join("|"));
    13. lineEdit->setValidator(new QRegExpValidator(regExp, this));
    14.  
    15. //Definir acções
    16. connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    17. connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    18. }
    19.  
    20. void IdDoAutomovelDialog::on_lineEdit_textChanged(){
    21. okButton->setEnabled(lineEdit->hasAcceptableInput());
    22.  
    23. //Limpar chapa
    24. labelDescSerie->setText("?");
    25.  
    26. //Verificar qual a série de matrÃ*cula
    27. QLineEdit newlineEdit;
    28. newlineEdit.setText(lineEdit->text());
    29.  
    30. QRegExp serie;
    31. serie.setPattern("");
    32. QRegExpValidator validator(serie,this);
    33. newlineEdit.setValidator(&validator);
    34. for ( QStringList::Iterator it = listaMatriculasValidas.begin(); it != listaMatriculasValidas.end();++it ){
    35. serie.setPattern(*it);
    36. //serie.setPattern("[0-9]{2,2}-[A-Za-z]{2,2}-[0-9]{2,2}");
    37. validator.setRegExp(serie);
    38. if(newlineEdit.hasAcceptableInput())
    39. switch(1){
    40. case 1: labelDescSerie->setText("Chapa de modelo anterior a 1992");
    41. break;
    42. case 2: labelDescSerie->setText("Chapa de modelo utilizado entre 1992 e 2005");
    43. break;
    44. case 3: labelDescSerie->setText("Chapa de modelo posterior a 2005");
    45. break;
    46. default:labelDescSerie->setText("???");
    47. }
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 
    Thanks

  5. #5
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to access an object inside a privateslot

    ufff ... solved it like this:
    Qt Code:
    1. void IdDoAutomovelDialog::on_lineEdit_textChanged(){
    2. okButton->setEnabled(lineEdit->hasAcceptableInput());
    3.  
    4. //Limpar chapa
    5. labelDescSerie->setText("?");
    6.  
    7. //Verificar qual a série de matrÃ*cula
    8. QLineEdit newlineEdit;
    9. newlineEdit.setText(lineEdit->text());
    10.  
    11. QRegExp serie;
    12. serie.setPattern("");
    13. QRegExpValidator validator(serie,this);
    14. newlineEdit.setValidator(&validator);
    15. for ( QStringList::Iterator it = listaMatriculasValidas.begin(); it != listaMatriculasValidas.end();++it ){
    16. serie.setPattern(*it);
    17. validator.setRegExp(serie);
    18. if(newlineEdit.hasAcceptableInput()){
    19. switch(listaMatriculasValidas.indexOf(serie.pattern(),0)+1){
    20. case 1: labelDescSerie->setText("Chapa de modelo anterior a 1992");
    21. break;
    22. case 2: labelDescSerie->setText("Chapa de modelo utilizado entre 1992 e 2005");
    23. break;
    24. case 3: labelDescSerie->setText("Chapa de modelo posterior a 2005");
    25. break;
    26. default:labelDescSerie->setNum(listaMatriculasValidas.indexOf(serie.pattern(),0));
    27. }
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to access an object inside a privateslot

    Make it an input parameter of your slot. Then do your switch(ipIndex).
    I'm a rebel in the S.D.G.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  8. #8
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Access an object stored in the list.
    By cbarmpar in forum General Programming
    Replies: 2
    Last Post: 21st September 2008, 20:19
  2. Access an object from another file.
    By cbarmpar in forum General Programming
    Replies: 1
    Last Post: 6th September 2008, 22:17
  3. Replies: 4
    Last Post: 26th June 2007, 19:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.