c++ - is it possible to "update" a QTextCursor? -


in qtextedit object, let's want know character's position under mouse cursor.

i can write...

void myqtexteditobject::mousepressevent(qmouseevent* mouse_event) {   mycursor = this->textcursor();   qdebug() << "pos=" << mycursor.position(); } 

... works (the mouse position changes 0 last index of last character) mousepressevent() method creates new cursor every time event occurs. bothers me since don't know "cost" of such creation.

so, why not create cursor attribute , use in mousepressevent() ?

something :

class myqtexteditobject : public qtextedit {     q_object public:     // [...]     qtextcursor cursor; }  myqtexteditobject::myqtexteditobject(qwidget* parent) : qtextedit(parent) {  // [...]  this->cursor = this->textcursor(); }  void myqtexteditobject::mousepressevent(qmouseevent* mouse_event) {   qdebug() << "pos=" << this->cursor.position(); } 

but position doesn't change anymore, if fixed. so, there way somehow update cursor ? or cost of repeated creation of qtextcursor insignificant ?

update : writing like...

mycursor= this->cursorforposition(mouse_event->pos()); 

... creates new cursor , seems equivalent :

mycursor= this->textcursor(); 

in first example, instead of

    mycursor = this->textcursor();     qdebug() << "pos=" << mycursor.position(); 

why not call directly as?

    qdebug() << "pos=" << this->textcursor().position(); 

because in python

    self.textcursor().position()  

works.

also, not sure in second example maybe need set "cursor" "textcursor" again settextcursor().

    void myqtexteditobject::mousepressevent(qmouseevent* mouse_event) {            this->settextcursor(cursor)            qdebug() << "pos=" << this->cursor.position();     } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -