qt - 如何获取文件名并将其放入 TextEdit
问题描述
在我的示例中,我有一个文本编辑和一个按钮。我可以使用文件对话框获取文件。我不知道如何将文件名或文件路径放入文本编辑中,我尝试使用substring
但效果不佳,我该怎么做?
这是我的代码:
import QtQuick.Dialogs 1.0
import QtQuick.Controls 2.2
import QtQuick 2.6
// This is rectangle parent
Rectangle {
id: overlay
width: parent.width
height: parent.height
color: "#000000"
// EditText into Rectangle
// rec edit tex
Rectangle{
id:rectextbox
width: 150
height: 22
anchors.top: recmidle.top
anchors.topMargin: 39
anchors.left: recmidle.left
anchors.leftMargin: 24
TextEdit{
id: txtedit
width: rectextbox.width
height: rectextbox.height
anchors.verticalCenter: rectextbox.verticalCenter
anchors.horizontalCenter: rectextbox.horizontalCenter
}
}
// Using FileDialog to get file
FileDialog {
id: file
title: "Choose an image"
}
// Button to find and search file
Button{
id: btnput
width: 50
height: 22
background: Rectangle{
id: btnrtg1
color: "#737373"
}
anchors.top: recmidle.top
anchors.topMargin: 39
anchors.right: recmidle.right
anchors.rightMargin: 76
Text {
id: textname1
anchors.verticalCenter: recbutton1.verticalCenter
anchors.horizontalCenter: recbutton1.horizontalCenter
text: qsTr("get image")
color: "white"
}
// MouseArea to set event Onclick for FileDialog
MouseArea{
anchors.fill: parent
onClicked: file.visible = true
}
}
解决方案
您只需将处理程序添加到您的FileDialog
:
FileDialog {
id: file
title: "Choose an image"
onAccepted: txtedit.text = fileUrl.toString().replace('file:///','')
}
推荐阅读
- php - 将多个数据集作为矩阵输入的 HTML/PHP 解决方案,如电子表格
- python - 如何在 conda 中安装包的扩展?
- elasticsearch - Kibana UI 无法为 v7.0.0 加载,并在 s390x 上出现错误“Kibana 未正确加载。检查服务器输出以获取更多信息”
- android - 这段代码有什么问题?为什么不工作,它告诉我“非法开始表达”,“预期;” 它指向我(视图 V))
- javascript - 如何在我的 YouTube MP4 和 MP3 下载器中编写 JavaScript 下载函数
- django - 如何在 Django 中使用过滤器迭代所有列值
- python - 我有一个动态系统的相空间数据集存储在一个文件中,并且想要它在 python 中的二维动画
- javascript - console.log 奇怪的数字行为
- java - 如何在不改变键盘视图的情况下使用 ScrollView?
- tensorflow - Tensorflow 会自动使用多个 CPU 吗?