2d text on screen

Hi how can I put text on occ screen? i am using sample qt-occ widget and I tried using QPainter (in paintEvent) to draw text. At first its fine, it draws the text but any change on screen (any dialog opened or any shape drawn) erases the text and I couldn't find a way to draw it back. Thanks for replies

Roman Lygin's picture


Are you talking about drawing the text in the OpenGl 3D viewer ? If so, I'm not sure if Qt's QPainter can be of any help here - you need to draw *within* the OpenGl context, not *on top* of it.
You can have a look at how Open CASCADE uses fonts to display text.

A few examples:
- AIS_Trihedron that displays X,Y,Z along the axes;
- XCAFPrs_AISObject that displays names of the entities stored in OCAF Document

Good luck.

Maili's picture

Thanks, now I can handle texts with Prs3d_Presentation,_TextAspect and _Text classes. But what I want to do is to write something while a shape is highlighted i.e. while cursor is over the shape. How can I do that? Thanks again

Svetlozar Kostadinov's picture

Handle(Graphic3d_Structure) hStruct = new Graphic3d_Structure(m_pAISContext->CurrentViewer()->Viewer());

Handle(Graphic3d_Group) pGr1 = new Graphic3d_Group(hStruct);
hGr1->Text(TextBuffer, Graphic3d_Vertex(m_RotPoint.X(), m_RotPoint.Y(), m_RotPoint.Z()), 0.07, 0, Graphic3d_TP_RIGHT, Graphic3d_HTA_CENTER,Graphic3d_VTA_HALF) ;

Visual3d_TransientManager::BeginDraw(m_pV3dView->View(), Standard_False, Standard_True);

What about this? Sorry if there are some mistakes in the code, but it can give you the idea.

Mathieu Grasset's picture

I know this topic is very old but that's exactly what I'm trying to do.
Did you find a way to achieve it?
If you have an article or a post on the subject to advise me, I will be grateful to you.
Thanks a lot.

Kirill Gavrilov's picture


Have you tried displaying AIS_TextLabel?

Mathieu Grasset's picture

Yes, I am currently trying to work with it.
I can easily play on its position, color, etc...
But at the moment I don't see how I can modify the text when another of my objects is hovered.
I guess there is something to do around the message (win api), maybe with WM_MOUSEMOVE and cursor position. But if I have to test every point of each object to know if the text has to be set, I think that I lose too many performance.

Sathiya nathan's picture

Create one single static object of AIS_Text label. While hovering on any shape set label location nearest to shape through SetLocation function and set the data you want display on the Ais_label. If no shape under the mouse. Remove the Ais_Text label from the context. To make it invisible.

siruvinf's picture

Thanks to your advice, I finally get what I want.
The way I used:
As you said, only one object of AIS_Textlabel.
I initialize the text with "".
When WM_MOUSEMOVE is triggered, I test if an object is hovered : m_Context->hasDetected().
If so, I get the currently detected object: m_Context->DetectedInteractive();
I look for it in my list object and change my label text accordingly.
Thank you all for your help.

Francois Lauzon's picture

We use Qt with OCC and what we are doing is using QToolTip in the 3d view directly to show temporary info about highlighted objects like this:

QToolTip::showText(QCursor::pos(),"some text to display",view3d_widget_pointer);

The result looks like the attached screenshot.