How to check position of a wheelEvent within text of a QGraphicsTextItem? - qt

I have a QGraphicsTextItem subclass that accepts mouse events (i.e. implements wheelEvent() method.
How can I check on which position within the text the wheel event happened? I would like to get the letter that the mouse pointer pointed at when the wheel event happened.
BTW: one possible solution is to create a series of QGraphicsTextItem objects -- one for every letter. This way each letter can accept it's own events, but I loose all the kerning and other typesetting sophistication.

To get the mouse position, you can use QWheelEvent::pos.
I don't see any API to get the letter at a given QPointF in the item. you could try to get a maybe good enough approximation using QFontMetricsF though, doing something like
const int wx = wheelEvent->pos().x(); //might have to map to item coordinates
const qreal leftX = item->boundingRect().left();
const QFontMetricsF fm( item->font() );
int pos = 0;
while ( fm.width( text.left( pos ) - leftX < wx )
pos++; //could be optimized by something binary-search-like
If that doesn't work out, you could try with a custom text item where you do the painting (QPainter::drawText) yourself, so you have more control over the positioning of the text in the item's coordinate system.

Related

Shift `QGraphicsTextItem` position relative to the center of the text?

I have a number of classes that inherit from QGraphicsItem, that get to be arranged in a certain way. For simplicity of calculations, I made the scenes, and items, centered in (0, 0) (with the boundingRect() having +/- coordinates).
QGraphicsTextItem subclass defies me, its pos() is relative to top left point.
I have tried a number of things to shift it so it centers in the text center (for example, the suggested solution here - the code referenced actually cuts my text and only shows the bottom left quarter).
I imagined that the solution should be something simple, like
void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->translate( -boundingRect().width()/2.0, -boundingRect().height()/2.0 );
QGraphicsTextItem::paint(painter, option, widget );
}
the above "sort of" works - but as I increase the item scale -> increase the font, the displayed item is cut off...
I tried to set the pos() - but the problem is, I still need to track the actual position on the scene, so I cannot just replace it.
A slightly unpleasant side effect - centering the QGraphicsView on the element does not work either.
How can I make my QGraphicsTextItem show its position relative to the center of the text ?
Edit: one of the experiments of changing the boundingRect():
QRectF TextItem::boundingRect() const
{
QRectF rect = QGraphicsTextItem::boundingRect();
rect.translate(QPointF(-rect.width()/2.0, -rect.height()/2.0));
return rect;
}
I had to shift the initial position, as well as the resize, to trigger a new position - I was unable to do it in paint() because, as I thought from the start, any repaint would continuously recalculate the position.
Only the initial position needs to be adjusted - but as the font size (or style...) changes, its bounding rectangle also changes, so the position must be recalculated - based on previous position.
In the constructor,
setPos(- boundingRect().width()/2, - boundingRect().height()/2);
in the function that modifies item (font) size,
void TextItem::setSize(int s)
{
QRectF oldRect = boundingRect();
QFont f;
f.setPointSize(s);
setFont(f);
if(m_scale != s)
{
m_scale = s;
qreal x = pos().x() - boundingRect().width()/2.0 + oldRect.width()/2.0;
qreal y = pos().y() - boundingRect().height()/2.0 + oldRect.height()/2.0;
setPos(QPointF(x, y));
}
}

Preventing font scale in QGraphicsItem

I am using QGraphicsTextItem to paint the text on the scene. Text is painted along the path (QGraphicsPathItem), wich is parent of my QGraphicsTextItem - so the text rotation is changed to be along the path element and is sticked to it while zooming the view. But the font size of QGraphicsTextItem is also changing while zooming the view - this is what I am trying to avoid. Of I set QGraphicsItem::ItemIgnoresTransformations flag to the QGraphicsTextItem it stops rotating while it's parent (QGraphicsPathItem) does.
I do understand that I have to re-implement QGraphicsTextItem::paint function, but I am stuck with the coordination system. Here is the code (Label class inherits public QGraphicsTextItem):
void Label::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
// Store current position and rotation
QPointF position = pos();
qreal angle = rotation();
// Store current transformation matrix
QTransform transform = painter->worldTransform();
// Reset painter transformation
painter->setTransform( QTransform() );
// Rotate painter to the stored angle
painter->rotate( angle );
// Draw the text
painter->drawText( mapToScene( position ), toPlainText() );
// Restore transformation matrix
painter->setTransform( transform );
}
The position (and rotation) of my text on the screen is unpredictable :(
What am I doing wrong? Thank you very much in advance.
I solved a problem this way - for drawing a line/circle/rectangle/path, which I want to be transformed, I use an appropriate QGraphicsLine/Ellipse/Rect/PathItem. For drawing the text (which I do NOT want to be transformed) I use QGraphicsSimpleTextItem. I set text's flag to ignore transormations and set it's parent to Line/Ellipse/Rect/Path item. The Line/Ellipse/Rect/Path item transforms, but text does not - this is what I wanted. I can also rotate text and set it's position.
Thank you very much for answers.
The following solution worked perfectly for me:
void MyDerivedQGraphicsItem::paint(QPainter *painter, const StyleOptionGraphicsItem *option, QWidget *widget)
{
double scaleValue = scale()/painter->transform().m11();
painter->save();
painter->scale(scaleValue, scaleValue);
painter->drawText(...);
painter->restore();
...
}
We can also multiply the scaleValue by other mesures we want to keep its size constant outside the save/restore environment.
QPointF ref(500, 500);
QPointF vector = scaleValue * QPointF(100, 100);
painter->drawLine(ref+vector, ref-vector);
I had this issue once. Instead of ignoring transformations, you need to scale down the items you don't want to be zoomed in in your zoom-in function.
When you zoom in, if you change the scale by ds for example, scale the items by 1.0 / ds
You might need to change their positions though.
I hope this helps.
Edit: I hope I understood the question right.

QT - Increase Richtext size on Button click

How do i increase the size of a Rich Text on the click of a button ?
I have a QTextEdit box with Rich text pasted in it.On the click of a + [ui button] i need to increase the font size of all the text inside it. Any idea on how to do that ?
Solution
This is what you should do inside the slot :
//-------------------------desired format-------------------------------
qreal pointSize = 40; // 40 for example, you can parameterize it
QTextCharFormat format;
format.setFontPointSize(pointSize);
//----------------------------------------------------------------------
ui->textEdit->selectAll();
// ^^^^^^^^^^^ You ask for all text in the textedit
// But remember partially change with mouse selection is also doable
ui->textEdit->mergeCurrentCharFormat(format);
(P.S. ui->textEdit is a pointer to QTextEdit)
The key point is to create an instance of QTextCharFormat to set the "partial" information of the font (Ex: size information only) and use QTextEdit::mergeCurrentCharFormat to merge the original format with the new format.
For example:
After merging by the operations above, the color, font...etc except size will be retained:
You can use the QTestEdit::setCurrentFont() function. For example:
QTextEdit te;
QFont f = te.currentFont();
int oldPointSize = f.pointSize();
int newPointSize = oldPointSize + 10;
f.setPointSize(newPointSize);
te.setCurrentFont(f);
te.setText("Test");
te.show();

Setting QStyleOptionComboBox.currentText does not have any effect on the drawn widget

I want to draw a QComboBox inside a delegate, which works fine except that I can't figure out how to draw the inital text that's visible inside the combo box.
The documentation says that QStyleOptionComboBox.currentText holds: "the text for the current item of the combo box." but setting the variable does not have any effect.
This is my code:
void MyDelegate::paint(QPainter *painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyleOptionComboBox comboBoxOption;
comboBoxOption.rect = option.rect;
comboBoxOption.state = option.state;
comboBoxOption.state |= QStyle::State_Enabled;
comboBoxOption.editable = false;
comboBoxOption.currentText = "CCC"; // This doesn't show up.
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
}
Looking at qwindowsxpstyle.cpp I don't see where the text of a "real" combo box is drawn since currentText is not used inside the drawComplexControl method. The only place where it seems to be used for Windows XP style is in qcommonstyle.cpp (Line 2107, Qt 4.7.2), but I can't figure out how those two classes play together.
It seems you also need to force Qt to draw the combo box label, in addition to the complex control. Try this:
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter)
If I read the documentation, and source, correctly that might force QStyle to draw a combo box label. It seems odd that you'd have to specify both...but I don't know a whole lot about how Qt styles draw themselves, to be honest.

Zooming in/out on a mouser point ?

As seen in the pictures.
I have QWidget inside a QScrollArea.
QWidget act as a render widget for cell image and some vector based contour data.
User can performe zoom in/out and what simply happens is, it changes the QPainters scale and change the size of QWidget size accordinly.
Now I want to perform the zooming in/out on the point under the mouse. (like zooming action in GIMP).
How to calculate the new positions of the scrollbars according to the zoom level ?
Is it better to implement this using transformations without using a scrollarea?
One solution could be to derive a new class from QScrollArea and reimplementing wheelEvent for example so that zooming is performed with the mouse wheel and at the current mouse cursor position.
This method works by adjusting scroll bar positions accordingly to reflect the new zoom level. This means as long as there is no visible scroll bar, zooming does not take place under mouse cursor position. This is the behavior of most image viewer applications.
void wheelEvent(QWheelEvent* e) {
double OldScale = ... // Get old scale factor
double NewScale = ... // Set new scale, use QWheelEvent...
QPointF ScrollbarPos = QPointF(horizontalScrollBar()->value(), verticalScrollBar()->value());
QPointF DeltaToPos = e->posF() / OldScale - widget()->pos() / OldScale;
QPointF Delta = DeltaToPos * NewScale - DeltaToPos * OldScale;
widget()->resize(/* Resize according to new scale factor */);
horizontalScrollBar()->setValue(ScrollbarPos.x() + Delta.x());
verticalScrollBar()->setValue(ScrollbarPos.y() + Delta.y());
}
Will void QScrollArea::ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50) do what you need?
You need to pick up the wheelEvent() on the QWidget, get the event.pos() and pass it into the QscrollArea.ensureVisible(), right after scaling your QWidget.
def wheelEvent(self, event):
self.setFixedSize(newWidth, newHeight)
self.parent().ensureVisible(event.pos())
That should more or less produce what you want.

Resources