You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, November 19th 2005, 2:12pm

custom QLineEdit selection color

Hi,

When we select some text in QLineEdit and QTextEdit the background of the selection becomes blue and the font color is white. Can i change these two colors? How can i do it?

Thanx a lot
Mithin
http://mithin.blogspot.com

Latem

Intermediate

  • "Latem" is male

Posts: 278

Location: New Brunswick, Canada

Occupation: Student/Programmer

  • Send private message

2

Saturday, November 19th 2005, 11:43pm

I think you can:

1. Create a new pallete from the widget's pallete()
2. Create a new QColorGroup from this palette's active() ColorGroup
3. Change this ColorGroup's highlightedText and highlight Colors
4. Set this new ColorGroup as the pallet's active ColorGroup.
5. Set the modified palette as the widget's palette

I think this should work in Qt4 as well. I am not sure, but there may be a more straight-forward way to do it in Qt3, or Qt4.

Latem
The march of progress:
C:
printf("%10.2f", x);
C++:
cout << setw(10) << setprecision(2) << showpoint << x;
Java:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

MarkoSan

Intermediate

  • "MarkoSan" is male

Posts: 230

Location: Ljubljana, SI-EU

Occupation: Embedded Systems Engineer I

  • Send private message

3

Friday, December 30th 2005, 1:54pm

Is there any example?
there is no home like 127.0.0.1 ;)
MS Winidows XP SP2; MS Visual Studio 2005 SP1 with that regresion patch; Qt 4.2.1 Commercial

Latem

Intermediate

  • "Latem" is male

Posts: 278

Location: New Brunswick, Canada

Occupation: Student/Programmer

  • Send private message

4

Friday, December 30th 2005, 11:23pm

Quoted

Is there any example?


Sure. This works with Qt4, as I just tried it. It's better and simpler than what I suggested in my previous post.

Let say you have a QLineEdit called myEdit.

Source code

1
2
3
4
5
6
QPalette p = myEdit->palette();
QBrush redBrush(Qt::red);
QBrush yellowBrush(Qt::yellow);
p.setBrush(QPalette::Highlight, redBrush);
p.setBrush(QPalette::HighlightedText, yellowBrush);
myEdit->setPalette(p);


It works the same for QTextEdit. At least with plain text.

Just a warning that this isn't a nice practice usability wise. At least, on Linux, and to a limited extend on Windows, the desktop environment's style and colour schemes dictate how the widgets look. By doing this, you are changing the app's look and feel from the rest of the desktop. Users may not be too fond of it.

Latem
The march of progress:
C:
printf("%10.2f", x);
C++:
cout << setw(10) << setprecision(2) << showpoint << x;
Java:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

This post has been edited 4 times, last edit by "Latem" (Dec 30th 2005, 11:28pm)


jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

5

Friday, December 30th 2005, 11:28pm

Quoted

Originally posted by Latem
QPalette p = myEdit->palette();
QBrush redBrush(Qt::red);
QBrush yellowBrush(Qt::yellow);
p.setBrush(QPalette::Highlight, redBrush);
p.setBrush(QPalette::HighlightedText, yellowBrush);
myEdit->setPalette(p);

Or:

Source code

1
2
3
4
QPalette p = myEdit->palette();
p.setColor( QPalette::Highlight, Qt::red );
p.setColor( QPalette::HighlightedText, Qt::yellow );
myEdit->setPalette( p );