在这个例子中我有两个静态的ListModel,实际上我使用LocalStorage来填充ListModel,但为了保持简单,我添加了两个按钮来更改模型,但我想将它绑定到TableView的标题列单击事件,并且无法从尝试排序的其他示例中找出如何做到这一点,我不知道是否可以使用ListModel进行排序,我找不到任何示例,所以有人能解释一下这一点或展示一个示例吗,如何用列单击事件替换按钮。然后,我可以使用它将sort by参数传递给我的LocalStorage sql语句,以更新ListModel。
更新:我忘记提到我正在寻找一个Qml / Qml JavaScript解决方案,出于某种原因,我想如果我不使用C++标签,我会避免这个问题,我将使用这个方法作为最后的手段,因为我决定只使用Qml编写这个应用程序,没有C++后端,但我现在有那个版本,因为我有问题,因为我如何导入为网络编写的JavaScript,而不是Qml JavaScript,这是不一样的。
要清楚的是,我试图改变模型,不对行进行排序,这些不是同一个问题,不同的是点击事件是如何使用的,我想做的就是在后端更改查询的名称,这是Qml JavaScript,我不想要C++解决方案的原因是因为我在Felgo中这样做,但这不是Felgo问题,它与C++一起工作很好,但你必须设置Live来使用它,实际上这将是我向github开放的源代码,并希望它能够在没有C++的情况下工作。似乎应该有一种方法来连接这个,鼠标确实对我起作用了,甚至在标题中捕获它,因为它在开始时挂起并等待输入,但如果我必须这样做,我相信你的解决方案会起作用,然后我会接受它,抱歉,我对使用什么标签感到困惑,所以最初我只包含了qml,并在其中添加了Qt,我认为这是一个很好的想法,因为这真的是一个qt问题,只与Qml有关,而不是C++,这是另一个标签,这是Felgo正在推动的趋势,他们有很好的理由,对于JavaScript或C/C++程序员来说更容易使用,当没有C++后端使用时,实时调试工作更快,所以现在我给出了更多的信息,当我最初认为这是一个简单的问题,只与C++有关,如果不是,那么答案已经给了C++,除非有更好的方法,看到我想做的所有事情就是点击标题列,就像我喜欢的按钮一样,我可以将按钮嵌入到列中吗?如果是这样的话,是怎么做的?我找不到这样的例子,只有那些会影响文本属性的例子,并且会对行进行排序,这并不是我想要做的,只是更新模型。
import QtQuick 2.11
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.3
import QtQuick.Window 2.11
Window {
visible: true
width: 640
height: 480
title: qsTr("TableView Sort")
Column {
id: column
spacing: 9
anchors.fill: parent
TableView {
id: tableView
anchors.left: column.left
anchors.leftMargin: 6
anchors.right: column.right
anchors.rightMargin: 273
highlightOnFocus: true
model: myListModel1
sortIndicatorVisible: true
TableViewColumn {
role: "title"
title: "Column 1"
//width: 133
}
TableViewColumn {
role: "description"
title: "Column 2"
//width: 166
}
}
Button {
id: button1
text: qsTr("Model 1")
anchors.left: column.left
anchors.leftMargin: 6
onClicked: {
tableView.model = myListModel1
}
}
Button {
id: button2
text: qsTr("Model 2")
anchors.left: column.left
anchors.leftMargin: 6
onClicked: {
tableView.model = myListModel2
}
}
}
ListModel {
id: myListModel1
ListElement {
title: "Orange"
description: "Orange is Orange"
}
ListElement {
title: "Banana"
description: "Yellow"
}
ListElement {
title: "Apple"
description: "Red"
}
}
ListModel {
id: myListModel2
ListElement {
title: "Apple"
description: "Red"
}
ListElement {
title: "Banana"
description: "Yellow"
}
ListElement {
title: "Orange"
description: "Orange is Orange"
}
}
}
更新:这是可行的
onSortIndicatorColumnChanged: tableView.model = (sortIndicatorColumn == 0) ? myListModel1 : myListModel2
onSortIndicatorOrderChanged: tableView.model = (sortIndicatorColumn == 0) ? myListModel1 : myListModel2
谢谢你的帮助。
发布于 2019-04-07 07:26:23
您可以使用代理模型对模型进行排序。但是,没有QML组件,您必须使用QSortFilterProxyModel
。
这很容易做到。但是,QSortFilterProxyModel
不能与QML表视图一起使用(您的表使用角色名来显示列,并且代理模型将尝试按索引排序)。
一个简单的例子:
class SortProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
SortProxyModel(): QSortFilterProxyModel ()
{
}
// Define the way you want to sort data
bool lessThan(const QModelIndex& left, const QModelIndex& right) const
{
int role = sourceModel()->roleNames().key(roleName.toLocal8Bit(), 0);
return left.data(role) < right.data(role);
}
Q_INVOKABLE void setSortRole(QString const& roleName) // Used to select the sort role
{
this->roleName = roleName;
}
Q_INVOKABLE virtual void sort(int /*column*/, Qt::SortOrder order = Qt::AscendingOrder)
{
QSortFilterProxyModel::sort(0, order); // Always use the first column.
}
private:
QString roleName; // Role used to sort the model
};
// main.cpp
// Declare your type to use it in QML
qmlRegisterType<SortProxyModel>("SortProxyModel", 0, 1, "SortProxyModel");
// Main.qml
import SortFilterProxyModel 0.1;
TableView {
id: tableView
model: proxy // Use the proxy model rather than the model itself
sortIndicatorVisible: true
onSortIndicatorColumnChanged: { // Called when you click on the header
if (sortIndicatorColumn == 0) // Set the role used to sort data
model.setSortRole("title");
else
model.setSortRole("description");
model.sort(sortIndicatorColumn, sortIndicatorOrder)
}
onSortIndicatorOrderChanged: { // Called when you click on the header
if (sortIndicatorColumn == 0) // Set the role used to sort data
model.setSortRole("title");
else
model.setSortRole("description");
model.sort(sortIndicatorColumn, sortIndicatorOrder)
}
SortProxyModel {
id: proxy
objectName: "proxy"
sourceModel: myListModel1
}
这只是一个快速示例,您应该改进代码。但是,我认为这将是一个很好的开始。
https://stackoverflow.com/questions/55553586
复制相似问题