
Originally Posted by
wysota
Not really. I want to understand why don't you just store the selection and then apply your formatting on that selection. You're still confusing focus and selection... Only ONE item can have focus at the same time. If you want it to go to the editable combobox for changing the font size, the text item WILL lose it. The bright side is YOU DON'T NEED IT.
Seems like we don't understand each other. What I need is visual keeping the text selection inside QGraphicsTextItem while the focus is in the combobox on the toolbar. And this selection should be displayed in "active" colors. That's all I need. Remembering selected text in a string or remembering its starting and ending positions can be used internally but visual representation is absolutely necessary anyway. The user should see the selected text as selected while he changes font size or family. I can not get this behavior by reimplementing formatting slots using QTextCursor. As I've already mentioned in my first post here this approach works with simple buttons but doesn't work with combos that steal focus. Using NoFocus for the combos solves the problem with text selection visual representation but disables manual value change - combos become non-editable and it's not appropriate.
And my question was very simple: what should I implement/reimplemet/override or what option(s) I should use to get the desired visual effect of keeping text selection in a QGraphicsTextItem?
And if you still want to see some code, than this setFont method would be fully enough to format only selected text in diagramscene example, but it won't work for font family and size:
//! [4]
void DiagramScene
::setFont(const QFont &font
) {
myFont = font;
if (isItemChange(DiagramTextItem::Type)) {
qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first());
//At this point the selection can change so the first selected item might not be a DiagramTextItem
if (item)
{
QTextCharFormat fmt
= cursor.
charFormat();
// may be unnecessary assignment
fmt.setFont(font);
cursor.setCharFormat(fmt);
item->setTextCursor(cursor);
}
}
}
//! [4]
//! [4]
void DiagramScene::setFont(const QFont &font)
{
myFont = font;
if (isItemChange(DiagramTextItem::Type)) {
QGraphicsTextItem *item =
qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first());
//At this point the selection can change so the first selected item might not be a DiagramTextItem
if (item)
{
QTextCursor cursor = item->textCursor();
QTextCharFormat fmt = cursor.charFormat(); // may be unnecessary assignment
fmt.setFont(font);
cursor.setCharFormat(fmt);
item->setTextCursor(cursor);
}
}
}
//! [4]
To copy to clipboard, switch view to plain text mode
Bookmarks