簡體   English   中英

如何在QML TextEdit中更改選項卡大小?

[英]How to change the tab size in QML TextEdit?

我在Qt 5.5應用程序中使用TextEdit組件作為代碼編輯器。 當按Tab鍵或粘貼其他編輯器的片段時,將應用默認選項卡大小(這是巨大的),我無法找到更改該值的方法。

我的解決方法是關鍵事件轉發到C ++控制器在那里我做的東西,如插入myCustomTabSizespaceCharacter每個Qt::Key_Tab事件。 或者在粘貼之前從剪貼板手動准備字符串。

QTextEdit類提供setTabStopWidth方法。 那是否有QML等價物?

要在QML TextEdit中更改選項卡大小,請執行以下步驟:

1)將objectName設置為TextEdit

TextEdit {
    objectName: "myTextEdit"
}

2)從c ++訪問TextEdit

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QObject *root = engine.rootObjects().at(0);
QObject *textEdit = root->findChild<QObject*>(QStringLiteral("myTextEdit"));

3)獲取與TextEdit相關聯的QTextDocument

QQuickTextDocument *quickTextDocument = textEdit->property("textDocument").value<QQuickTextDocument*>();
QTextDocument *document = quickTextDocument->textDocument();

4)獲取默認的QTextOption

QTextOption textOptions = document->defaultTextOption();

5)設置制表位之間的設備單位距離

textOptions.setTabStop(10);

6)設置文檔選項。

document->setDefaultTextOption(textOptions);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM