Python 如何在QSCINTILA小部件内的特定位置显示对话框?

Python 如何在QSCINTILA小部件内的特定位置显示对话框?,python,dialog,pyqt,pyqt5,qscintilla,Python,Dialog,Pyqt,Pyqt5,Qscintilla,我得到了一段简单的mcve代码: import sys import re from PyQt5 import QtGui, QtWidgets, QtCore from PyQt5.Qsci import QsciScintilla from PyQt5 import Qsci class SimpleEditor(QsciScintilla): def __init__(self, language=None, parent=None): super().__

我得到了一段简单的mcve代码:

import sys
import re

from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.Qsci import QsciScintilla
from PyQt5 import Qsci


class SimpleEditor(QsciScintilla):

    def __init__(self, language=None, parent=None):
        super().__init__(parent)

        font = QtGui.QFont()
        font.setFamily('Courier')
        font.setFixedPitch(True)
        font.setPointSize(10)
        self.setFont(font)
        self.setMarginsFont(font)
        fontmetrics = QtGui.QFontMetrics(font)
        self.setMarginsFont(font)
        self.setMarginWidth(0, fontmetrics.width("00000") + 6)
        self.setMarginLineNumbers(0, True)
        self.setMarginsBackgroundColor(QtGui.QColor("#cccccc"))
        self.setBraceMatching(QsciScintilla.SloppyBraceMatch)
        self.setCaretLineVisible(True)
        self.setCaretLineBackgroundColor(QtGui.QColor("#E8E8FF"))

        if language:
            self.lexer = getattr(Qsci, 'QsciLexer' + language)()
            self.setLexer(self.lexer)

        self.SendScintilla(QsciScintilla.SCI_FOLDALL, True)
        self.setAutoCompletionThreshold(1)
        self.setAutoCompletionSource(QsciScintilla.AcsAPIs)
        self.setFolding(QsciScintilla.BoxedTreeFoldStyle)

        # Signals/Slots
        self.cursorPositionChanged.connect(self.on_cursor_position_changed)
        self.copyAvailable.connect(self.on_copy_available)
        self.indicatorClicked.connect(self.on_indicator_clicked)
        self.indicatorReleased.connect(self.on_indicator_released)
        self.linesChanged.connect(self.on_lines_changed)
        self.marginClicked.connect(self.on_margin_clicked)
        self.modificationAttempted.connect(self.on_modification_attempted)
        self.modificationChanged.connect(self.on_modification_changed)
        self.selectionChanged.connect(self.on_selection_changed)
        self.textChanged.connect(self.on_text_changed)
        self.userListActivated.connect(self.on_user_list_activated)

    def on_cursor_position_changed(self, line, index):
        print("on_cursor_position_changed")

    def on_copy_available(self, yes):
        print('-' * 80)
        print("on_copy_available")

    def on_indicator_clicked(self, line, index, state):
        print("on_indicator_clicked")

    def on_indicator_released(self, line, index, state):
        print("on_indicator_released")

    def on_lines_changed(self):
        print("on_lines_changed")

    def on_margin_clicked(self, margin, line, state):
        print("on_margin_clicked")

    def on_modification_attempted(self):
        print("on_modification_attempted")

    def on_modification_changed(self):
        print("on_modification_changed")

    def on_selection_changed(self):
        print("on_selection_changed")

    def on_text_changed(self):
        print("on_text_changed")

    def on_user_list_activated(self, id, text):
        print("on_user_list_activated")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    ex = QtWidgets.QWidget()
    hlayout = QtWidgets.QHBoxLayout()
    ed = SimpleEditor("JavaScript")

    hlayout.addWidget(ed)

    ed.setText("""#ifdef GL_ES
precision mediump float;
#endif

#extension GL_OES_standard_derivatives : enable

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

void main( void ) {

    vec2 st = ( gl_FragCoord.xy / resolution.xy );
    vec2 lefbot = step(vec2(0.1), st);
    float pct = lefbot.x*lefbot.y;
    vec2 rigtop = step(vec2(0.1), 1.-st);
    pct *= rigtop.x*rigtop.y;
    vec3 color = vec3(pct);

    gl_FragColor = vec4( color, 1.0 );""")

    ex.setLayout(hlayout)
    ex.show()
    ex.resize(800, 600)

    sys.exit(app.exec_())
我正在尝试编写一个类似于本文提供的解决方案的代码。如您所见,在该站点上,用户只能通过单击来调整数值,当他们这样做时,将出现一个小对话框,用户将能够实时更改数值

现在,我首先要弄清楚的是,当用户点击QSCNTILLA小部件时,我需要考虑哪些信号(我正在使用这些信号)。哪一个是我真正应该关心的


在任何情况下,假设我使用right(右)信号弹出包含一些滑块的对话框,我如何确定对话框应该出现的正确位置?

只要鼠标或键盘将插入符号移动到数字内的某个位置,就可以使用
光标位置更改
信号进行检测。然后,可以使用regexp查找数字在当前行的起始和结束位置。一旦你有了它,你可以使用一些低级函数来计算数字的全局位置

下面的方法使用该方法在相关文本下方显示工具提示:

def on_cursor_position_changed(self, line, index):
    text = self.text(line)
    for match in re.finditer('(?:^|(?<=\W))\d+(?:\.\d+)?(?=$|\W)', text):
        start, end = match.span()
        if start <= index <= end:
            pos = self.positionFromLineIndex(line, start)
            x = self.SendScintilla(
                QsciScintilla.SCI_POINTXFROMPOSITION, 0, pos)
            y = self.SendScintilla(
                QsciScintilla.SCI_POINTYFROMPOSITION, 0, pos)
            point = self.mapToGlobal(QtCore.QPoint(x, y))
            num = float(match.group())
            message = 'number: %s' % num
            break
    else:
        point = QtCore.QPoint(0, 0)
        message = ''
    QtWidgets.QToolTip.showText(point, message)
def在光标上的位置已更改(自身、行、索引):
text=self.text(行)

对于re.finditer(')(?)中的匹配:^|(?目前是什么阻止了您自己开始编写代码?关于SO的问题应该集中在一个特定的编程问题上-否则看起来您只是在要求别人为您编写所有代码。@ekhumoro好的,我的手已经被任务弄脏了,我已经编辑了我的问题,试图使其更具体,您能确认我没有吗他现在的格式是更容易处理的吗?此外,我已经摆脱了我的全部主题外的评论,这样线程就不会被污染。希望问题的标题/内容不再宽泛,请让我知道。谢谢。谢谢你费心改进你的问题。非常感谢!这已经足够好开始了。下一步p将创建不同的小部件来处理glsl语言提供的所有不同类型(float、vec2、vec3)并正确解析它们。顺便说一句,还有几个快速问题,你知道为什么要使用
qtwidts.QColorDialog.getColor()
相反,QToolTip会让我崩溃吗?还有,替换qscintula上的文本的最佳方式是什么?无论如何,谢谢你的回答,非常感谢;-)@BPL。我想你必须为其他问题发布一些新问题。