qml 实现文本编辑器

时间:2024-10-13 20:02:15
  • import QtQuick 2.2
  • import 1.1
  • import 1.4
  • import 1.3
  • import FileIO 1.0
  • ApplicationWindow {
  • id: q
  • visible: true
  • title: "文件编辑器"
  • width: 640
  • height: 480
  • color: "white"
  • menuBar: MenuBar {
  • Menu {
  • title: "文件"
  • MenuItem {
  • id: open
  • text: "打开..."
  • iconName: "open"
  • shortcut:
  • onTriggered: open.open()
  • function open(){
  • fileDialog.open()
  • = true
  • =true
  • }
  • }
  • MenuItem {
  • text: "新建"
  • shortcut:
  • onTriggered: fileDialog2.open()
  • }
  • MenuItem {
  • text: "保存"
  • shortcut:
  • onTriggered: console.log( "WRITE"+ myFile.write())
  • }
  • MenuItem {
  • text: "保存为"
  • shortcut:
  • onTriggered: fileDialog1.open()
  • }
  • }
  • Menu {
  • title: "编辑"
  • MenuItem {
  • text: "复制"
  • shortcut:
  • onTriggered: edit.copy()
  • }
  • MenuItem {
  • text: "剪切"
  • shortcut:
  • onTriggered: edit.cut()
  • }
  • MenuItem {
  • text: "粘贴"
  • shortcut:
  • onTriggered: edit.paste()
  • }
  • }
  • Menu {
  • title: "帮助"
  • MenuItem {
  • text: "About..."
  • shortcut:
  • onTriggered: aboutDialog.open()
  • }
  • }
  • }
  • Flickable
  • {
  • id: flick
  • :parent
  • width: 300; height: 200;
  • contentWidth:
  • contentHeight:
  • clip: true
  • visible: false
  • function ensureVisible(r)
  • {
  • if (contentX >= )
  • contentX = ;
  • else if (contentX+width <= +)
  • contentX = +-width;
  • if (contentY >= )
  • contentY = ;
  • else if (contentY+height <= +)
  • contentY = +-height;
  • }
  • TextEdit
  • {
  • id: edit
  • width:
  • height:
  • focus: true
  • wrapMode:
  • selectByMouse: true
  • onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  • }
  • }
  • Rectangle
  • {
  • id: scrollbar
  • visible: false
  • :
  • x: 350
  • y: 0
  • width: 10
  • height:
  • color: "white"
  • Rectangle {
  • id: button
  • x: 0
  • y: *
  • width: 10
  • height: * ;
  • color: "grey"
  • // 鼠标区域
  • MouseArea {
  • id: mouseArea
  • : button
  • : button
  • :
  • : 0
  • : - // 拖动
  • onMouseYChanged: {
  • = / *
  • }
  • }
  • }
  • }
  • FileDialog {
  • id: fileDialog
  • title: "请选择一个文件"
  • onAccepted: {
  • var path = .toString();
  • path = path.replace(/^(file:\/{3})/,"");
  • path = decodeURIComponent(path);
  • =path;
  • = myFile.read();
  • // ( "WRITE"+ ("TEST TEST file is OK"));
  • console.log("You chose: " + );
  • fileDialog.close()
  • }
  • onRejected: {
  • console.log("Canceled")
  • fileDialog.close()
  • }
  • }
  • FileDialog {
  • id: fileDialog1
  • title: "请输入文件名"
  • selectExisting: false
  • onAccepted: {
  • var p=myFile.read();
  • var path = .toString();
  • path = path.replace(/^(file:\/{3})/,"");
  • path = decodeURIComponent(path);
  • =path;
  • console.log( "WRITE"+ myFile.write(p));
  • = myFile.read();
  • console.log("You chose: " + );
  • fileDialog.close();
  • }
  • onRejected: {
  • console.log("Canceled")
  • fileDialog.close()
  • }
  • }
  • FileDialog {
  • id: fileDialog2
  • title: "请输入文件名"
  • selectExisting: false
  • onAccepted: {
  • var path = .toString();
  • path = path.replace(/^(file:\/{3})/,"");
  • path = decodeURIComponent(path);
  • =path;
  • console.log( "WRITE"+ myFile.write(""));
  • = myFile.read();
  • console.log("You chose: " + );
  • fileDialog.close();
  • }
  • onRejected: {
  • console.log("Canceled")
  • fileDialog.close()
  • }
  • }
  • FileIO {
  • id: myFile
  • onError: console.log(msg)
  • }
  • }