[Interest] Preventing ^2 from being converted to a superscript 2

Ben Haller bhaller at mac.com
Fri Jul 24 21:45:07 CEST 2026


Hi all!  I've got a Qt project that contains a code editor.  The code 
editor is based on QPlainTextEdit, with a bit of subclassing for syntax 
highlighting and things of that sort -- code editor-y functionality.  I 
doubt any of that subclassing is relevant to the question at hand.

In the language being edited, ^ is the exponentiation operator, so the 
user wants to type "x^2" and have that mean "x squared" in the language 
on question.  An issue recently arose where some of my users on Linux 
would type the "^2" and it would be turned into a superscripted 2 (!).  
I guess this is some Linux feature to make it easier to type exponents 
and such.  Which is fine, but obviously inappropriate for a code editor; 
the user needs to be able to type "^2" and have it stay "^2" even if, in 
other apps on that platform, it appropriately gets changed to a 
superscipted 2.

Note that the QPlainTextEdit is plain text, of course; I don't think 
this is rich text somehow creeping into the plain text view.  I think, 
instead, that this Linux feature is turning "^2" into the Unicode 
superscripted 2 character (https://unicode-explorer.com/c/00B2).

My question: how can I customize my QPlainTextEdit subclass to prevent 
this?  Is there a hook or a setting somewhere?  I assume that, e.g., Qt 
Creator manages to prevent these kinds of shenanigans on all platforms, 
so maybe I'm asking somebody who knows the Qt Creator code well to 
advise on what they did there.  It is quite possible that these users 
could turn off this feature in a Linux prefs panel somewhere, but I 
don't want to ask them to have to do that; they should be able to have 
the OS feature enabled, and yet I should be able to turn it off in my 
code view.  I'd also like to not interfere with typing of accented 
characters, emojis, etc.; I just want to prevent this specific feature 
from activating in my code view, because the '^' character is 
significant and I don't want it to be re-interpreted by the OS

Thanks for any tips!

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University



More information about the Interest mailing list