PDA

View Full Version : QFont and baseline to draw a custom cursor indicator



RiccardoZack
19th April 2020, 15:54
Hi everyone, I'm stuck with trying to draw a custom cursor, let's say it is a "|", in a certain position that depends on cursor position and size. Here what I am trying to obtain: I want to have this "|" exactly of the same dimension and same position of the normal blinking cursor. The visual appearance of the blinking cursor changes with the actual font size, but the rectangle in with it is placed not, so I cannot simply take the top() and botto() of the rectangle and use them as coordinates.

I tried to use labels and somehow it is acceptable when in the same line there are characters with different size but same (or similar) font (see figureS):
13401
13402
13403

Problems come when in the same line different font families are used: for example the following images include some characters with "Purisa" font:
13404
13405

Here there is the code I used for drawing that labels:


static double minsize=1000;
// Obtain size of original cursor
const QRect qRect = this->cursorRect();
int x=qRect.height();

// Obtain size of current font
QFont font=this->font();
font.setPointSize(this->fontPointSize());
QFontMetrics fm(font);
int y=fm.height();

//Correction for small characters
minsize= y<minsize? y:minsize;

//Top edge where to start drawing the cursor
int ty=qRect.top()+(x-y)/1.18-minsize/7.0;

labelName->show();
labelCursor->show();

labelCursor->move(qRect.topLeft().x()-1, ty);
labelName->move(qRect.topLeft().x()+5, ty);


There must be something more "plain" I can do, insted of that magic parameters I found after trying.

I also tried to start drawing the label from a position obtained like:


ty=qRect.bottom()-fm.height();

but it's worse than the previous solution.

Hoping someone can help,
kind regards to all.