我的 QML 文件中有一个 TextEdit,我有一个 QSyntaxHighlighter C++ 类。我想在C++类中指定高亮逻辑并将其应用于TextEdit,但我不确定如何在QML对象和C++类之间建立连接。您还可以提供一些示例代码吗?我找不到如何使用 Qt 文档来实现它。
我有一个列表视图。如果我单击一个单元格,该特定单元格需要通过一个小的滚动动画移动到中心。使用我的代码,单击的单元格将在没有任何动画的情况下到达中心。
是否可以为此添加动画?
我把我的代码放在下面:
ListView {
id: source_list
width: 1080
height: 480
spacing: 50
model: mediaSongsModel
delegate: mediaSongsDelegate
focus: true
interactive: true
clip: true
highlightMoveDuration: 50
snapMode: ListView.SnapToItem
boundsBehavior:Flickable.StopAtBounds
preferredHighlightBegin: 260/scaleFactor
preferredHighlightEnd: 260/scaleFactor
highlightRangeMode: ListView.StrictlyEnforceRange
}
Component {
id: mediaSongsDelegate
Item {
id: wrapper
width: 1080
height: 200
MouseArea {
anchors.fill: parent
onClicked: {
source_list.currentIndex = index
source_list.positionViewAtIndex(index,ListView.Center)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个基于 qml 的应用程序,其中有一个搜索字段。该字段还有一个图标,表明它是一个搜索框。问题是,当我输入文本时,它会与图标重叠,我想基本上将文本输入区域限制为不包含图像的文本字段。
qml代码如下:
TextField {
id: searchBox
font.pixelSize: 18
background: Rectangle {
radius: 6
border.color: "#707070"
border.width: 1
Image {
source: "../images/search.png"
anchors.left: parent.left
anchors.leftMargin: 12
anchors.verticalCenter: parent.verticalCenter
}
}
}
Run Code Online (Sandbox Code Playgroud)
生成的组件呈现如下:
如您所见,文本输入区域与图像重叠。有没有办法确保文本输入区域被剪切或在这种情况下向右移动并且用户无法在呈现图像的位置添加文本?
任何人都知道如何更改控件“ RoundButton”的颜色,该控件自2.1开始就存在于Qt控件中。
我尝试更改背景,但是如果我在该项目上放置“矩形”,则RoundButton会变成矩形,而我不知道要放置什么。
RoundButton {
id: roundButton
x: 243
y: 244
width: 20
height: 20
text: "ah"
wheelEnabled: false
background: Rectangle {
color: "yellow"
height: 50
width: 50
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个堆叠的物品。这两个项目都有一个半透明的背景。圆圈现在显示下面的圆形矩形。
有什么办法可以隐藏与圆圈重叠的长圆形矩形的部分吗?也许改变父,圆的背景是从祖先向上拉,因此忽略它下面的矩形?
这是代码:
Item
{
id: choice1
width: 300
height: 100
Rectangle
{
id: choiceLabel1
width: 0
height: parent.height / 1.5
radius: parent.height * 0.5
color: "#88808080"
anchors
{
verticalCenter: choice1.verticalCenter
left: choice1.left
leftMargin: choiceIcon1.width / 2
}
border
{
width: 2
color: "red"
}
}
Rectangle
{
id: choiceIcon1
width: choice1.height
height: choice1.height
radius: width * 0.5
color: "#88808080"
anchors
{
verticalCenter: choice1.verticalCenter
left: choice1.left
}
border
{
width: 2
color: "red"
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个90%不透明度的灰色矩形,我正在尝试使用动画更改此不透明度.我尝试过使用OpacityAnimator但是,正如文档所说,在动画结束后,Item :: opacity的值会更新.
这不是我的预期,我想减少动画期间的不透明度,而不是在2秒后立即将其从90改为0.
这就是我的意思:
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("title")
Rectangle{
width: parent.width
height: parent.height
opacity: 90
color: "gray"
id: recLoading
Text {
id: txtLoading
text: qsTr("Loading")
font.bold: true
}
OpacityAnimator on opacity{
from: 90
to:0
duration:2000
target: parent
running:true
}
}
}
Run Code Online (Sandbox Code Playgroud) 就像网页一样,当内容超出矩形时,会有一个滚动条.有没有其他人可以帮助我?我尝试过listview,但我不能在矩形中使用它
比方说,SomeClass
有成员Object1
和Object2
与之间有一个连接Object1
和Object2
这样的:
connect(Object1, signal1, Object2, slot1)
Run Code Online (Sandbox Code Playgroud)
经过一番重构Object3
加入SomeClass
和Object2
被转移到成为其成员Object3
,但仍存在之间的连接的需求Object1
和Object2
.
现在Object1
和之间的沟通Object2
必须经历Object3
.这意味着Object3
需要修改,添加一对信号/插槽只是为了实现Object1
和之间的通信Object2
.
这意味着Object3
将修改.h和.cpp,添加许多代码行来完成之前在一行中完成的操作.
我懒惰的一面是说这个故事有些奇怪.有没有办法让这种联系更直接?
我已经看过关于QML中内容余量的大量问题,但所有答案都指向缺少的spacing: 0
属性。我已经完成了所有这些操作,但是仍然得到了无法消除的怪异空间。谁能解释为什么这个QML代码:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Test")
RowLayout {
spacing: 0
anchors.margins: 0, 0, 0, 0
anchors.fill: parent;
Pane {
anchors.margins: 0, 0, 0, 0
id: menuPane
anchors.top: parent.top;
anchors.bottom: parent.bottom;
width: 200
ColumnLayout {
spacing: 0
anchors.fill: parent
anchors.margins: 0, 0, 0, 0
Rectangle {
id: testRect
Layout.fillWidth: true
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
height: 20
color: "green"
}
}
}
Pane …
Run Code Online (Sandbox Code Playgroud)