背景:使用QT开发的程序,打包后在有些ubuntu环境可以执行成功,但是在某些ubuntu环境中不能启动,报:“segmentation fault (core dumped)。
定位方法:因为自己搭建的系统不能复现问题,因此通过systemback将系统还原到虚拟机中,在虚拟机中进行复现。因此代码中主要是qml,因此通过将代码中相关一点点注释掉的方法进行排除是哪一个控件导致。
原因:在QT环境中使用了致自定义的ComboBox控件,使用的有问题导发生异常。下面说下ComboBox使用的错误点
(1)官方QT帮助文档给出了ComboBox的自定义实例-Customizing ComboBox:
import QtQuick 2.12
import QtQuick.Controls 2.12
ComboBox {
id: control
model: ["First", "Second", "Third"]
delegate: ItemDelegate {
width: control.width
contentItem: Text {
text: modelData
color: "#21be2b"
font: control.font
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}
highlighted: control.highlightedIndex === index
}
indicator: Canvas {
id: canvas
x: control.width - width - control.rightPadding
y: control.topPadding + (control.availableHeight - height) / 2
width: 12
height: 8
contextType: "2d"
Connections {
target: control
onPressedChanged: canvas.requestPaint()
}
onPaint: {
context.reset();
context.moveTo(0, 0);
context.lineTo(width, 0);
context.lineTo(width / 2, height);
context.closePath();
context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";
context.fill();
}
}
contentItem: Text {
leftPadding: 0
rightPadding: control.indicator.width + control.spacing
text: control.displayText
font: control.font
color: control.pressed ? "#17a81a" : "#21be2b"
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
}
background: Rectangle {
implicitWidth: 120
implicitHeight: 40
border.color: control.pressed ? "#17a81a" : "#21be2b"
border.width: control.visualFocus ? 2 : 1
radius: 2
}
popup: Popup {
y: control.height - 1
width: control.width
implicitHeight: contentItem.implicitHeight
padding: 1
contentItem: ListView {
clip: true
implicitHeight: contentHeight
model: control.popup.visible ? control.delegateModel : null
currentIndex: control.highlightedIndex
ScrollIndicator.vertical: ScrollIndicator { }
}
background: Rectangle {
border.color: "#21be2b"
radius: 2
}
}
}
(2)自己在使用该例子的时候:
(2.1)将popup中的定义修改了成了如下(闲的):
popup: Popup {
y: control.height - 1
width: control.width
implicitHeight: listview.contentHeight
padding: 1
contentItem: ListView {
id: listview
clip: true
model: control.popup.visible ? control.delegateModel : null
currentIndex: control.highlightedIndex
ScrollIndicator.vertical: ScrollIndicator { }
}
background: Rectangle {
border.color: "#21be2b"
radius: 2
}
}
定义了listview的id,问题出在了id的listview上,只要定义id就会出问题
(2.2)按照帮助文档修改了之后,异常还未消失,原因是在调用该自定义控件CustomComboBox的时候,使用了属性visible:false,因此导致异常。
因为程序在功能上不同的场景,下拉框的显示是不一样的,但是使用model的时候,每一次赋值都会将reset currentIndex to 0,将数值重置。因此想到了定义两个下拉框通过visible来控制。导致问题。
所以下拉框不要使用visible,对于上面的功能场景,通过信号activated(int index)来处理,可以避免数值重置问题。不要使用currentIndex。
void activated(int index)
This signal is emitted when the item at index is activated by the user.
An item is activated when it is selected while the popup is open, causing the popup to close (and currentIndex to change), or while the popup is closed and the combo box is navigated via keyboard, causing the currentIndex to change. The currentIndex property is set to index.
See also currentIndex.