QT开发中文网

// 获取当前选择的树节点 QTreeWidgetItem* currentItem = this->ui->treeWidget->currentItem(); if (currentItem) { // 获取子节点列表 QList children = currentItem->takeChildren(); ...

要满足每次只添加一个 mm 文件并进行排序,你可以按照以下方式改进代码:

// 获取当前选择的树节点
QTreeWidgetItem* currentItem = this->ui->treeWidget->currentItem();
if (currentItem) {
    // 获取子节点列表
    QList<QTreeWidgetItem*> children = currentItem->takeChildren();

    // 自定义比较函数
    auto customItemSort1 = [](const QTreeWidgetItem* item1, const QTreeWidgetItem* item2) {
        // 在这里实现自定义的排序逻辑,比如按照文本升序排序
        QString text1 = item1->text(0);
        QString text2 = item2->text(0);
        double number1 = text1.split("mm")[0].toDouble();
        double number2 = text2.split("mm")[0].toDouble();
        qDebug() << __LINE__ <<" num1: "<< number1 <<" num2:" << number2;
        return number1 < number2;  // 按数值大小升序排序

    };

    std::sort(children.begin(), children.end(), customItemSort1);

    // 将排序后的子节点重新添加到树中
    for (QTreeWidgetItem* child : children) {
        currentItem->addChild(child);
        QString childText = child->text(0);  // 假设文本位于第一列
        qDebug() << "Child Text:" << childText;
    }
}

在上述改进的代码中,我们修改了自定义比较函数 customItemSort1 的逻辑。现在它返回的是 number1 < number2,表示按数值大小进行升序排序。这样,每次只添加一个 mm 文件时,将根据数值进行排序。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=8293

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?