Results 1 to 6 of 6

Thread: Slot to color background of line edit on textedited

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Slot to color background of line edit on textedited

    I have the following code to change the background of a line edit when it is edited by the user:

    Qt Code:
    1. ProgramMain::ProgramMain() {
    2. connect( ui.vendorname, SIGNAL( textEdited() ), this, SLOT( colorbackground() ) );
    3. }
    4.  
    5.  
    6. ProgramMain::colorbackground() {
    7. ui.vendorname->setStyleSheet( QString( "background-color: yellow"));
    8. }
    To copy to clipboard, switch view to plain text mode 

    This works, however I have many line edits in the UI file and I would like to have them all change color if the user edits them.

    is there any way I can make a single slot that can handle any line edit? Or will I need to make a slot for each line edit that I want to change the color for?

  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: Slot to color background of line edit on textedited

    1) Use QObject::sender() to identify the line edit being edited:
    Qt Code:
    1. connect( lineEditA, SIGNAL( textEdited() ), this, SLOT(colorbackground() ) );
    2. connect( lineEditB, SIGNAL( textEdited() ), this, SLOT(colorbackground() ) );
    3.  
    4. ProgramMain::colorbackground() {
    5. QLineEdit* lineEdit = qobject_cast<QLineEdit*>(sender());
    6. if (lineEdit)
    7. lineEdit->setStyleSheet(...);
    8. }
    To copy to clipboard, switch view to plain text mode 

    2) Use QSignalMapper
    J-P Nurmi

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

    tpf80 (20th June 2007)

  4. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot to color background of line edit on textedited

    awesome! this was exactly what I was looking for

  5. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot to color background of line edit on textedited

    I put this code to action and I got the following error on the command line:

    no such signal QlineEdit::textEdited()
    I looked in the docs and QlineEdit should have this signal. What could be missing?

  6. #5
    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: Slot to color background of line edit on textedited

    Quote Originally Posted by tpf80 View Post
    I looked in the docs and QlineEdit should have this signal. What could be missing?
    Looks like a parameter is missing. So it should be:
    Qt Code:
    1. connect( ui.vendorname, SIGNAL( textEdited( const QString& ) ), this, SLOT( colorbackground() ) );
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    tpf80 (21st June 2007)

  8. #6
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot to color background of line edit on textedited

    yes this was the issue.. so silly of me to forget that parameter! Thanks so much for your help!

Similar Threads

  1. Replies: 8
    Last Post: 15th May 2007, 10:21
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42

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.