Hi, people!
I just wanted to make my text item clear the selection when I take out the focus, but it doesn't work.
// main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QTextCursor>
{
public:
{
}
protected:
setTextInteractionFlags(Qt::TextEditorInteraction);
setFocus();
}
{
t.clearSelection(); // why this doesn't work?
// t.removeSelectedText(); // but this works !!!
}
};
int main(int argc, char** argv)
{
view.show();
return app.exec();
}
// main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QTextCursor>
class MyTextItem : public QGraphicsTextItem
{
public:
MyTextItem(const QString& name, QGraphicsItem *parent, QGraphicsScene *scene)
:QGraphicsTextItem(name, parent, scene)
{
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
}
protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e){
setTextInteractionFlags(Qt::TextEditorInteraction);
setFocus();
QGraphicsTextItem::mouseDoubleClickEvent(e);
}
void focusOutEvent(QFocusEvent *e)
{
QTextCursor t = textCursor();
t.clearSelection(); // why this doesn't work?
// t.removeSelectedText(); // but this works !!!
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
QGraphicsTextItem::focusOutEvent(e);
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsTextItem *text = new MyTextItem("Hello", 0, &scene);
QGraphicsView view(&scene);
view.setDragMode(QGraphicsView::RubberBandDrag);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Have you ever met a problem like this?
Bookmarks