[QScintilla] showing ToolTips dinamically

Baz Walter bazwal at ftml.net
Fri Nov 7 18:32:10 GMT 2008


Andrea Battisti wrote:
> Hi All,
> 
> is it possible to pop-up a tooltip within a QSciScintilla editor widget?
> 
> I was thinking to use the QToolTip class from Qt to draw a tooltip
> dinamically, based on the current mouse position.
> 
> The problem is that the ToolTip needs to be associated to a rectangle
> within the widget, and I am not seeing a way to go from the mouse
> position to the word under the cursor.
> 
> I checked the QScintilla API docs and I only found the lineAt( QPoint& )
> method that gives back the line where the cursor is pointing at.
> Is there a way to find also the word? (the column positions?)
> And is there a way then to get the QRect corresponding to that specific
> word?
> 
> For example, a use case would be the following: when the mouse is held
> for a second or two over a word, then a pop-up could appear showing
> context-specific information on that word.

Here's a working example in python:

import sys, os
from PyQt4.QtCore import SIGNAL, SLOT, QEvent, QRect
from PyQt4.QtGui import QApplication, QToolTip
from PyQt4.Qsci import QsciScintilla, QsciScintillaBase as QSB


class Editor(QsciScintilla):
     def __init__(self):
         QsciScintilla.__init__(self)
         self.resize(640,480)
         self.setUtf8(True)
         try:
             self.setText(open(os.path.join(
                          os.getcwd(), __file__)).read())
         except EnvironmentError:
             pass

     def event(self, event):
         if (event.type() == QEvent.ToolTip and
             hasattr(QToolTip, 'isVisible') and
             not QToolTip.isVisible()):
             send = self.SendScintilla
             point = event.pos()
             pos = send(QSB.SCI_POSITIONFROMPOINTCLOSE,
                        point.x(), point.y())
             if pos >= 0:
                 start = send(QSB.SCI_WORDSTARTPOSITION, pos, True)
                 end = send(QSB.SCI_WORDENDPOSITION, pos, True)
                 if start != end:
                     bytes = '\1' * (end - start)
                     send(QSB.SCI_GETTEXTRANGE, start, end, bytes)
                     word = bytes.decode('utf8')
                     if word.startswith('Q'):
                         x_start = send(QSB.SCI_POINTXFROMPOSITION,
                                        0, start)
                         y_start = send(QSB.SCI_POINTYFROMPOSITION,
                                        0, start)
                         x_end = send(QSB.SCI_POINTXFROMPOSITION,
                                      0, end)
                         line = send(QSB.SCI_LINEFROMPOSITION, start)
                         height = send(QSB.SCI_TEXTHEIGHT, line)
                         rect = QRect(x_start, y_start,
                                      x_end - x_start, height)
                         QToolTip.showText(event.globalPos(), word,
                                           self.viewport(), rect)
         return QsciScintilla.event(self, event)


if __name__ == "__main__":
     app = QApplication(sys.argv)
     app.connect(app, SIGNAL('lastWindowClosed()'),
                 app, SLOT('quit()'))
     editor = Editor()
     editor.show()
     app.exec_()


-
Regards
Baz Walter



More information about the QScintilla mailing list