QTextEdit question



  • Hello everyone!!

    I'm using Qt 3.7 .
    I have a class that inherits QTextEdit and I would like to get the position of the text cursor (x() and y()).
    I can't use textCursor() function because it's protected.
    Do you have any suggestions?


  • Moderators

    If you're inheriting from QTextEdit, why don't you have access to the protected members?

    As an aside, Qt 3 is very old. If possible, you're much better off using Qt 4 (if circumstances allow it.)



  • In your subclass you have access to the protected method textCursor().



  • I'M CORRECTING MY PREVIOUS MESSAGE

    Hello everyone!!

    I'm using Qt 3.7 .
    I have a class that gets a QTextEdit as a parameter and I would like to get the position of the text cursor (x() and y()).
    I can't use textCursor() function because it's protected. (The class inherits a class that inherits QObject)
    Do you have any suggestions?



  • Edit QTextEdit's header file and change textCursor() to public. :-)

    Just kidding. Qt 3 has a function called getCursorPosition that can get the current cursor position. You can consult the "online documentation":http://doc.qt.nokia.com/3.3/qtextedit.html for more information. (The doc is for Qt 3.3, but it's more or less the same. Does Qt 3.7 even exist? I didn't know that.)

    If you need information that are not accessible with existing functions, you can always subclass QTextEdit and write a custom public function that returns the current cursor, and use it in your project instead of QTextEdit.



  • Thank you all for your answers!! I will try it tomorrow



  • Hello again,
    unluckily I couldn't made it.
    I've made a subclass that inherits QTextEdit and a public function:
    @
    getCursorCoordinates() {
    QTextCursor *tc = textCursor();
    int x = tc->globalX();
    int y = tc->globalY();
    }
    @
    and I had the error:
    request for member 'globalX' in 'tc', which is non-class type 'QTextCursor'.

    I think because QTextCursor is a private class.

    Any other help? I can't upgrade to Qt4..It's not up to me...

    Edit: Fixed code formatting. Please wrap code in @ tags; mlong



  • The class QTextCursor is not know to the compiler, which is ok as it is not in the public API and therefore not in the header files.



  • Hi, I understand that I can't use QTextCursor :)
    so is there something that you propose about finding cursor's position?



  • From what I remember of Qt3 days, no there is no such means. The only means that comes into mind would be to extract a class declaration (header file) out of the QTextCursor code in the Qt sources and include that into your own source files. But be aware that this might induce some license issues (speak of "derived work" in GPL). If you hold a commercial license, you're on the safe side.



  • Hello,
    I added this header:
    #include <private/qrrichtext_p.h>
    and it works. Many thanks again!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.