2017-08-27 55 views
0

文本光标在资源更多的控制,我发现有相当多的一整套光标控制操作:获得了从QML

enum MoveOperation { 
    NoMove, 
    Start, 
    Up, 
    StartOfLine, 
    StartOfBlock, 
    StartOfWord, 
    PreviousBlock, 
    PreviousCharacter, 
    PreviousWord, 
    Left, 
    WordLeft, 
    End, 
    Down, 
    EndOfLine, 
    EndOfWord, 
    EndOfBlock, 
    NextBlock, 
    NextCharacter, 
    NextWord, 
    Right, 
    WordRight, 
    NextCell, 
    PreviousCell, 
    NextRow, 
    PreviousRow 
}; 

相比之下,来自QtQuick.Controls 1.4最新TextField,光标位置被暴露一个简单的整数,可以设置,但不指定任何移动操作。这就是它。

在较旧的TextEdit中有一些额外的东西,如selectWord()moveCursorSelection(int position, SelectionMode mode),但mode仅限于选择字符或单词。

更糟糕的是,稀疏的现有API并不真正提供必要的功能来手动重新实现大多数这些模式。

因此,这个问题使我想到了如何以最直接,最不突兀的方式获得QML中所有功能的问题?

+0

你也应该问问这个Qt利益邮件列表 –

+1

@ Jean-MichaëlCelerier是或是向主祷告:)我现在实际上已经到了一半,除了一个小的链接问题。 – dtech

回答

0

更新:

其实是有一个比较明显的,少侵入性的方式来获得该功能,并且它是发布虚假事件所需的文本编辑。这不得不不需要使用私有API的优势,从而避免所有潜在的构建和兼容性并发症:

void postKeyEvent(Qt::Key k, QObject * o, bool sh = false, bool ct = false, bool al = false) { 
    uint mod = Qt::NoModifier; 
    if (sh) mod |= Qt::ShiftModifier; 
    if (ct) mod |= Qt::ControlModifier; 
    if (al) mod |= Qt::AltModifier; 
    QCoreApplication::postEvent(o, new QKeyEvent(QEvent::KeyPress, k, (Qt::KeyboardModifier)mod)); 
    QTimer::singleShot(50, [=]() { QCoreApplication::postEvent(o, new QKeyEvent(QEvent::KeyRelease, k, (Qt::KeyboardModifier)mod)); }); 
} 

现在,我终于可以得到所有需要的光标控制的东西与我的自定义虚拟键盘​​上的触摸设备。


这是一个简单的解决方案,它的实际工作......如果你设法构建它,也有一些odd problems with building it

#include <QtQuick/private/qquicktextedit_p.h> 
#include <QtQuick/private/qquicktextedit_p_p.h> 
#include <QtQuick/private/qquicktextcontrol_p.h> 

class CTextEdit : public QQuickTextEdit { 
    Q_OBJECT 
    public: 
    CTextEdit(QQuickItem * p = 0) : QQuickTextEdit(p) {} 
    public slots: 
    void cursorOp(int mode) { 
     QQuickTextEditPrivate * ep = reinterpret_cast<QQuickTextEditPrivate *>(d_ptr.data()); 
     QTextCursor c = ep->control->textCursor(); 
     c.movePosition((QTextCursor::MoveOperation)mode); 
     ep->control->setTextCursor(c); 
    } 
}; 

显然它使用私有头,它有两层含义:

  • 您必须将quick-privte模块添加到PRO文件中
  • 私人资料可能会更改S,其中包括重大更改,因为这友好的消息不断提醒:

_

Project MESSAGE: This project is using private headers and will therefore be tied to this specific Qt module build version. 
Project MESSAGE: Running this project against other versions of the Qt modules may crash at any arbitrary point. 
Project MESSAGE: This is not a bug, but a result of using Qt internals. You have been warned! 

从正面看,它就像一个魅力。国际海事组织认为,这项功能应该作为公开API的一部分提供,它非常有用,当然不是有意隐藏的东西。