Results 1 to 8 of 8

Thread: Connect SINAGLS?SLOTS

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Connect SINAGLS?SLOTS

    Hello Guys,

    Im trying to Connect multiple QlineEdit::textChanged(QString) SIGNAL with one SLOT , but in this slot i need it to change the Label color of the particular LineEdit that trigger the Slot.

    so how can i connect a textchanged signal correctly to the slot ??

    the problem the slot signature is not the same the as signal signature, which i know that u can not connect them this way.

    but can i change the signal signature ?? because i need to know the lable for that particular lineEdit.

    i'll try to illustrate more by this exmple:



    Qt Code:
    1. connect(ui->LEdit1, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(..,.,))); // change lable1 color
    2.  
    3. connect(ui->LEdit2, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable2 color
    4.  
    5. connect(ui->LEdit3, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable3 color
    6.  
    7. connect(ui->LEdit4, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable4 color
    8.  
    9. connect(ui->LEdit5, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable5 color
    10.  
    11.  
    12.  
    13. void setLableColor(QLabel *lable, QString txt , QString color)
    14. {
    15. //change the lable color ;
    16. }
    To copy to clipboard, switch view to plain text mode 

    before i used to have a textchange slot for each lineEdit and then call the setLableColor from there. but i though maybe is should be a way to just have one slot and connect all the LineEdit to this as im doing the same thing its just a different label.
    so, is there a way to connect with this slot ?? or how should i do it ??


    Thank you so much in advnace for any help and suggestions.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Connect SINAGLS?SLOTS

    you can get the QLineEdit which emited the signal inside the slot using sender() function. Here is an example with single slot

    Qt Code:
    1. connect(ui->LEdit1, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
    2. connect(ui->LEdit2, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
    3. connect(ui->LEdit3, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
    4. connect(ui->LEdit4, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
    5. connect(ui->LEdit5, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
    6.  
    7. void setColor(QString txt)
    8. {
    9. QLineEdit * LEdit = dynamic_cast<QLineEdit *>(sender());
    10. if(LEdit != 0)
    11. {
    12. //change the lable color ;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    jesse_mark (21st November 2012)

  4. #3
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect SINAGLS?SLOTS

    Thank you so much,

    Sorry, but how i know which LineEdit is this ??? i mean, how would i deiced is the sender for example is LEdit1 not LEdit2 ??

    PS: do u think its better to do it this way, where i detect which lineEdit emit the signal and change the lable color.
    or i keep using a different slot for each lineEdit ??

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect SINAGLS?SLOTS


  6. The following user says thank you to Lesiok for this useful post:

    jesse_mark (21st November 2012)

  7. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect SINAGLS?SLOTS

    I would say a better way is to treat the label + line edit as an 'entity'

    e.g.
    Qt Code:
    1. class LabelAndEdit
    2. {
    3. public:
    4. LabelAndEdit()
    5. : edit(new QLineEdit), label(new QLabel)
    6. {
    7. label->setBuddy(edit);
    8. connect(edit, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
    9. }
    10.  
    11. QLineEdit* lineEdit() const {return edit;}
    12. QLabel* label() const {return label;}
    13.  
    14. public slots:
    15. void setColor(QString color)
    16. {
    17. // set color on label
    18. }
    19.  
    20. private:
    21. QLineEdit* edit;
    22. QLabel* label;
    23. }
    To copy to clipboard, switch view to plain text mode 

    This is much better encapsulation. You could modify it to use existing QLineEdit etc. If you are using the designer and coming up with things like lineedit1, lineedit2, lineedit3, lineedit4, ... then it will lead to messy code. You should get used to 'designing' in code and it will be much cleaner and simpler.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. The following user says thank you to amleto for this useful post:

    jesse_mark (21st November 2012)

  9. #6
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connect SINAGLS?SLOTS

    Thanks y'all for the useful hints.

    Amleto, Yes i do use the designer, but of coruse i dont use lineedit1,...lineeditn to name my objects,I do use meaningful names.

    I like your lableAndEdit approach, would not come to my mind

    Why it is better to design in code ? is using the designer is a bad habit ??
    I am using the designer because its much easier and clearer and much faster, especially if i have a lot of widgets to layout, where much faster to deal with the size, layouts, adding and modifying.
    I think using the designer is more cleaner and simpler than using the code, where all the setting the such as size, color, font, layouts, location, .... and many other attributes can be set and be hidden in the code and easily modified from the designer.

    I am new and i am still learning, so i don't know why using the code is better unless in cases where i need to create a especial use widget, as the LabelAndEdit you show me now.

    I think I will post this is as a question, so I would know more and new noob like me will now that too.

    Thank you so much,

  10. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connect SINAGLS?SLOTS

    Amleto, Yes i do use the designer, but of coruse i dont use lineedit1,...lineeditn to name my objects,I do use meaningful names.
    The point is not the suitability of names, but the fact that there are so many.

    If you design in designer, and then want to link lineedits to labels, you have to 'hard code' it using the variable names you gave each individual element in the designer.

    If you do it in code you don't need to worry about variable names and you can set up a short factory pattern or loop that can deal with any number of elements.

    e.g.

    you have designed a form with a bunch of lineedits and then you need to connect them in a class ctor you end up with code like this

    Qt Code:
    1. classX::classX()
    2. {
    3. QWidget* w1 = new QWidget;
    4. connect(ui->lineedit1, SIGNAL(...), w1, SLOT(...));
    5.  
    6. QWidget* w2 = new QWidget;
    7. connect(ui->lineedit2, SIGNAL(...), w1, SLOT(...));
    8.  
    9. QWidget* w3 = new QWidget;
    10. connect(ui->lineedit3, SIGNAL(...), w1, SLOT(...));
    11.  
    12. QWidget* w4 = new QWidget;
    13. connect(ui->lineedit4, SIGNAL(...), w1, SLOT(...));
    14.  
    15. QWidget* w5 = new QWidget;
    16. connect(ui->lineedit5, SIGNAL(...), w1, SLOT(...));
    17. }
    To copy to clipboard, switch view to plain text mode 

    c.f.

    Qt Code:
    1. classX::classX()
    2. {
    3. for (int i = 0; i < N; ++i)
    4. {
    5. QLineEdit* le = new QLineEdit;
    6. QWidget* w = new QWidget;
    7. connect(le, SIGNAL(...), w, SLOT(...));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 21st November 2012 at 22:12.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Connect SINAGLS?SLOTS

    having single slot for multiple QLineEdit will be a nice approach.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. The following user says thank you to Santosh Reddy for this useful post:

    jesse_mark (21st November 2012)

Similar Threads

  1. Replies: 1
    Last Post: 28th January 2012, 12:35
  2. How to use connect() in IDE's(slots)
    By wenn32 in forum Newbie
    Replies: 7
    Last Post: 6th July 2010, 16:21
  3. Replies: 16
    Last Post: 16th February 2010, 13:17
  4. Connect to...without slots...
    By Peppy in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2009, 13:47
  5. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38

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
  •  
Qt is a trademark of The Qt Company.