Python Forum
[PyQt] QLineEdit Caret (Text Cursor) Transparency
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QLineEdit Caret (Text Cursor) Transparency
#1
I noticed that if I set the background color of a QLineEdit, the caret (text cursor) becomes invisible. Setting the foreground color does nothing for the caret. So, this:
setStyleSheet("QLineEdit{background-color: Gray; color: Black}")
just sets the background color and the text color, the caret is invisible/transparent. I haven't tested yet, but I'm pretty sure I can work around this with setTextColor(), so it's not the end of the world, I'd just like to do it via a style sheet. I am on PyQt6 v6.4.0.

Any ideas how to set caret and text color via a style sheet?
Reply
#2
does it have to be that gray? using the word Gray uses #808080

using #707070 the cursor is visible

setStyleSheet("background-color: #707070; color: black;")
Reply
#3
Interesting, @Axel_Erfurt. That's all well and good (and no, I'm not married to the color), but why doesn't the cursor adhere to the foreground color? Black text shows up just fine with gray. So, I guess my next question is how do you change cursor color via a stylesheet?
Reply
#4
Okaay... I've been reading (yesterday's troubleshooting involved glossing over) and it looks like in versions of Qt since at least 5.8 the text cursor is related to the background color (which my experience finds). It seems Qt inverts the cursor based on background color. So, a white background will give you a black cursor, black gives white, red gives blue, etc. setTextColor() is said not to change cursor color being it's tied to the background. The solution I found is a QPaintEvent. So, that's my findings.
Reply
#5
The only text widget in PyQt where you can easily change the cursor color is probably QScintilla.

setCaretForegroundColor()
Reply
#6
That and a QTextEdit may be the ONLY widgets that can have their text cursor changed. Turns out I was wrong before. I have been searching all day, and the best I can do is change the rect that contains the cursor. The cursor on a QLineEdit may just be hardcoded. Stylesheets change the color, but there's no clear way to change it via code. So, I must admit defeat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to accept only float number in QLineEdit input ism 5 20,528 Jul-06-2021, 05:23 PM
Last Post: deanhystad
  Simple printing the text for a QLineEdit thewolf 16 5,050 Mar-06-2021, 11:37 PM
Last Post: deanhystad
  PyQt5: How to retrieve a QLineEdit by its name ? arbiel 4 4,940 Oct-21-2020, 02:35 PM
Last Post: arbiel
  Two QlineEdit box, which interrelated GMCobraz 1 1,703 Aug-14-2020, 07:15 PM
Last Post: deanhystad
  [PyQt] Dynamically add and remove QLineEdit's GMCobraz 3 5,638 Jun-23-2020, 07:01 PM
Last Post: Yoriz
  prompt for input in qlineedit GMCobraz 3 2,369 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 1,589 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 4,989 Apr-03-2020, 12:15 AM
Last Post: JayCee
  [Tkinter] Finding out what's under the cursor sabresong 3 1,678 Mar-28-2020, 02:52 PM
Last Post: sabresong
  [PyQt] How to clear multiple Qlineedit in a loop mart79 6 5,961 Aug-15-2019, 02:37 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020