#include <QApplication>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QAbstractTextDocumentLayout>
#include <QBasicTimer>
{
Q_OBJECT
public:
TextDia
::TextDia( const QString txt ,
cursorTime
(false),BackHightlight
(QColor("#a6ffc7")) { setBlinkingCursorEnabled(true);
BackHightlight.setAlpha(80);
}
{
return document()->documentLayout()->frameBoundingRect(document()->rootFrame());
}
void setBlinkingCursorEnabled(bool enable)
{
cursorTime = false; /* leave cursor not paint */
cursorTimeLine.
start( QApplication::cursorFlashTime() / 2,
this);
setTextInteractionFlags ( Qt::TextEditorInteraction );
} else {
cursorTimeLine.stop();
setTextInteractionFlags ( Qt::NoTextInteraction );
}
}
{
if (event->timerId() == cursorTimeLine.timerId()) {
if (!cursorTime) {
cursorTime = true;
} else {
cursorTime = false;
}
update();
}
}
{
Q_UNUSED(widget);
const int text_cursor_position = textCursor().position();
painter->save();
CTX.selections;
painter->setClipRect(boundingRect(), Qt::IntersectClip);
if (cursorTime) {
CTX.cursorPosition = text_cursor_position;
} else {
CTX.cursorPosition = -1;
}
painter->setPen(Qt::NoPen);
CTX.clip = boundingRect();
painter->setBrush(BackHightlight);
painter->drawRect(boundingRect());
document()->documentLayout()->draw(painter,CTX);
painter->setPen(Qt::NoPen);
painter->restore();
}
bool cursorTime;
};
#include "main.moc"
int main(int argc, char *argv[])
{
rect->acceptHoverEvents();
TextDia* text = new TextDia("ABCDEFGHIJKLMNOP",rect,&scene);
view.setScene( &scene );
view.show();
return a.exec();
}
#include <QApplication>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QAbstractTextDocumentLayout>
#include <QBasicTimer>
class TextDia : public QGraphicsTextItem
{
Q_OBJECT
public:
TextDia::TextDia( const QString txt ,
QGraphicsItem *parent ,
QGraphicsScene *scene )
: QGraphicsTextItem(txt,parent,scene),
cursorTime(false),BackHightlight(QColor("#a6ffc7")) {
setBlinkingCursorEnabled(true);
BackHightlight.setAlpha(80);
}
QRectF boundingRect() const
{
return document()->documentLayout()->frameBoundingRect(document()->rootFrame());
}
void setBlinkingCursorEnabled(bool enable)
{
cursorTime = false; /* leave cursor not paint */
if (enable && QApplication::cursorFlashTime() > 0) {
cursorTimeLine.start( QApplication::cursorFlashTime() / 2,this);
setTextInteractionFlags ( Qt::TextEditorInteraction );
} else {
cursorTimeLine.stop();
setTextInteractionFlags ( Qt::NoTextInteraction );
}
}
void timerEvent(QTimerEvent *event)
{
if (event->timerId() == cursorTimeLine.timerId()) {
if (!cursorTime) {
cursorTime = true;
} else {
cursorTime = false;
}
update();
}
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(widget);
const int text_cursor_position = textCursor().position();
painter->save();
QAbstractTextDocumentLayout::PaintContext CTX;
CTX.selections;
painter->setClipRect(boundingRect(), Qt::IntersectClip);
if (cursorTime) {
CTX.cursorPosition = text_cursor_position;
} else {
CTX.cursorPosition = -1;
}
painter->setPen(Qt::NoPen);
CTX.clip = boundingRect();
painter->setBrush(BackHightlight);
painter->drawRect(boundingRect());
document()->documentLayout()->draw(painter,CTX);
painter->setPen(Qt::NoPen);
painter->restore();
}
QColor BackHightlight;
QBasicTimer cursorTimeLine;
bool cursorTime;
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsRectItem* rect = new QGraphicsRectItem(QRectF(25,25,120,80),0,&scene);
rect->setFlag(QGraphicsItem::ItemIsMovable, true);
rect->setFlag(QGraphicsItem::ItemIsSelectable, true);
rect->acceptHoverEvents();
TextDia* text = new TextDia("ABCDEFGHIJKLMNOP",rect,&scene);
QGraphicsView view;
view.setScene( &scene );
view.setDragMode( QGraphicsView::RubberBandDrag );
view.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks