Foreground of qt5.textedit.

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
seany
Posts: 32
Joined: Friday 6th December 2019 3:09pm

Foreground of qt5.textedit.

Post by seany »

Hello

I am trying to work with a q5 textedit. The one highlighted in picture:

Image

I have the following code:

Code: Select all

Public Sub Form_Resize()

  ToolBar1.Width = Me.W
  TextEdit1.W = Me.W / 2 - 10
  TextEdit1.Background = Color.RGB(190, 190, 190)
  TextEdit1.Top = ToolBar1.H + 10
  TextEdit1.Height = Me.H - 10 - TextEdit1.Top - 50
  TextEdit1.Foreground = Color.Red
  

End
I expect it to be triggered when the form is loaded, as resize is called in the beginning. Everything works, but

Code: Select all

TextEdit1.Foreground = Color.Red
The result is:

Image

As you can see, the foreground color did not take effect.

I am on Artix Linux, KDE. Output of uname -a :
Linux glassplanet 5.17.4-artix1-1 #1 SMP PREEMPT Thu, 21 Apr 2022 06:59:38 +0000 x86_64 GNU/Linux

I attach the project. what can I do so that the color will take effect? Thank you.
Attachments
src.zip
(1.93 KiB) Downloaded 106 times
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground of qt5.textedit.

Post by BruceSteers »

It's RichText so it's HTML based.
the Foreground does not seem to do anything.

But code like this will work.....

Public Sub ToolButton1_Click()

   TextEdit1.RichText = ("New String <font color=red>This is a red part </font> and this is not")


End
Notes:
RichText is not full HTML only basic.
Setting TextEdit1.Text or using TextEdit1.Insert() will also not work for colors , use TextEdit.RichText
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Foreground of qt5.textedit.

Post by cogier »

Tip: - use the gb button for better code display.
Image

I discovered that Background does not work, but a workaround is to place the TextEdit in a Panel then you can change the background of the panel.
To change the font you need, as Bruce says, to use Richtext.

Have a look at the code in the attached program: -
Image
TestOnly-0.0.1.tar.gz
(12.22 KiB) Downloaded 129 times
You might like to have a look at how to create expanding forms in Gambas here.
seany
Posts: 32
Joined: Friday 6th December 2019 3:09pm

Re: Foreground of qt5.textedit.

Post by seany »

Excellent.
Thank you
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground of qt5.textedit.

Post by BruceSteers »

.

See my post here Seany

viewtopic.php?f=15&t=1338

I discovered the TextEdit1.Selection.RichText property that would be like having a TextEdit1.InsertRichText() method to insert html code at the cursor position.

PS.
I found you can not just insert unfilled html code for example..
TextEdit1.Selection.RichText = "<font color=red></font>"
Because there is no text to show just the code.

Using this code however in the KeyPress handler...
TextEdit1.Selection.RichText = "<font color=red>" & Key.Text & "</font>"
Stop Event
will work just fine and the cursor is inside the font definition so more text added at that place will be the same color :)

Best of luck
Bruce
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Foreground of qt5.textedit.

Post by BruceSteers »

I just discovered another magic property TextEdit1.Format http://gambaswiki.org/wiki/comp/gb.qt4. ... dit.format

Thanks to James on the gambas mailing list https://lists.gambas-basic.org/pipermai ... 76305.html

With that you can quickly get or set various things at current cursor position or for selected text, very handy.
Has the following properties...
  • Alignment
  • Background
  • Color
  • Font
If at first you don't succeed , try doing something differently.
BruceS
seany
Posts: 32
Joined: Friday 6th December 2019 3:09pm

Re: Foreground of qt5.textedit.

Post by seany »

This is very nice, thank you
Post Reply