Qt文档阅读笔记-TextEdit QML Type官方解析及实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/86486843

目录

 

官方解析

博主栗子


官方解析

TextEdit展示了一个可编辑的一块,是有格式的文本。
他同样能展示普通文本和富文本:

  TextEdit {
      width: 240
      text: "<b>Hello</b> <i>World!</i>"
      font.family: "Helvetica"
      font.pointSize: 20
      color: "blue"
      focus: true
  }


focus属性可以设置接收键盘的聚焦。
注意:TextEdit是不能实现滚动功能的,他是跟随光标的,或指定特定的外观或者感官,举个例子,下面的代码实现了跟随鼠标的弹动滚动的效果:

  Flickable {
       id: flick

       width: 300; height: 200;
       contentWidth: edit.paintedWidth
       contentHeight: edit.paintedHeight
       clip: true

       function ensureVisible(r)
       {
           if (contentX >= r.x)
               contentX = r.x;
           else if (contentX+width <= r.x+r.width)
               contentX = r.x+r.width-width;
           if (contentY >= r.y)
               contentY = r.y;
           else if (contentY+height <= r.y+r.height)
               contentY = r.y+r.height-height;
       }

       TextEdit {
           id: edit
           width: flick.width
           height: flick.height
           focus: true
           wrapMode: TextEdit.Wrap
           onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
       }
   }


如果要追求好的视觉比如滑动滚动(使用SmootheAnimation),这会产生一个可视化的滚动条,或者是一个褪色的滚动条。
剪切板的功能被cut(),copy()和paste()函数提供,设置selectByMouse可以鼠标选择要剪切的内容,可以使用selectionStart和selectionEnd以及selectAll()或selectWord()。
可以在光标位置上进行转换,如使用positionAt()和positionToRectangle()

博主栗子

这里把官方的2个例子补充完整,这里要注意,第一个html的富文本在Qt5以上的版本貌似不能成功!

两个源码运行截图如下:

程序结构如下:

源码如下:

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

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

    if(engine.rootObjects().isEmpty()){

        qDebug() << "engine load failed!";
        return -1;
    }

    return a.exec();
}

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {

    visible: true
    width: 800
    height: 600

    TextEdit {

        width: 240
        text: "Hello World!"
        font.family: "Helvetica"
        font.pointSize: 20
        color: "blue"
        focus: true
    }

//    Flickable {

//        id: flick
//        width: 300
//        height: 200
//        contentWidth: edit.paintedWidth
//        contentHeight: edit.paintedHeight
//        clip: true


//        function ensureVisible(r){

//            if(contentX >= r.x)
//                contentX = r.x
//            else if(contentX + width <= r.x + r.width)
//                contentX = r.x + r.width - width
//            if(contentY >= r.y)
//                contentY = r.y;
//            else if(contentY+height <= r.y+r.height)
//                 contentY = r.y+r.height - height;
//        }

//        TextEdit {

//            id: edit
//            width: flick.width
//            height: flick.height
//            focus: true
//            wrapMode: TextEdit.Wrap
//            onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
//        }

//        anchors.centerIn: parent
//    }

}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/86486843