【问题标题】:QScintilla how to continously get cursor position in textEdit widget?QScintilla 如何在 textEdit 小部件中连续获取光标位置?
【发布时间】:2019-05-16 21:37:03
【问题描述】:

我正在开发 C++ 源代码编辑器,使用 Qt5 和 QScintilla 作为框架。在这个项目中,我想不断地显示文本光标的行和列(光标位置),所以我需要一个在文本光标移动时发出的信号。根据 QScintilla 文档, cursorPositionChanged(int line, int index) 每当光标移动时都会发出想要的信号,所以我想这一定是我需要的方法?这是我到目前为止所做的:

// notify if cursor position changed
connect(textEdit, SIGNAL(cursorPositionChanged(int line, int index)), this, SLOT(showCurrendCursorPosition()));

我的代码编译并且编辑器窗口按需要显示,但不幸的是,我收到了警告:

QObject::connect: No such signal QsciScintilla::cursorPositionChanged(int line, int index)

谁能给我一个 QScintilla C++ 或 Python 示例,展示如何连续获取和显示当前光标位置?

完整的源代码托管在这里: https://github.com/mbergmann-sh/qAmigaED

感谢任何提示!

【问题讨论】:

    标签: c++ qt5 qscintilla


    【解决方案1】:

    这个问题是由运行时验证的旧连接语法引起的,此外旧语法还有一个必须匹配签名的问题。在您的情况下,解决方案是使用没有您提到的问题的新连接语法。

    connect(textEdit, &QTextEdit::cursorPositionChanged, this, &MainWindow::showCurrendCursorPosition);
    

    更多信息您可以查看:

    【讨论】:

      【解决方案2】:

      谢谢,eyllanesc,您的解决方案运行良好! 我自己也找到了一个可行的解决方案,只需从连接调用中删除命名的变量:

      // notify if cursor position changed
      connect(textEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(showCurrendCursorPosition()));
      

      ...

      //
      // show current cursor position and display
      // line and row in app's status bar
      //
      void MainWindow::showCurrendCursorPosition()
      {
          int line, index;
          qDebug() << "Cursor position has changed!";
          textEdit->getCursorPosition(&line, &index);
          qDebug() << "X: " << line << ", Y: " << index;
      }
      

      这个话题已经解决了。

      【讨论】:

      • 我推荐使用新语法的一个重要优点是连接是在编译时而不是在运行时验证的,因此您可以在执行程序之前看到错误。
      • 嗯,这绝对有道理。再次感谢! :)
      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      • 2023-01-11
      • 1970-01-01
      • 2011-03-14
      • 2011-02-04
      • 1970-01-01
      相关资源
      最近更新 更多