Qt QTreeview currentChange未发出

问题描述 投票:0回答:1

当我在QTreeView中选择新索引时,我试图运行一些代码

在RoverPlanner.h

namespace Ui {
    class RoverPlanner;
}

class RoverPlanner : public QWidget
{
Q_OBJECT

public:
    explicit RoverPlanner(QWidget *parent = nullptr);
    void save_paths_from_tree(QTreeView* treeView);
    void load_paths_into_tree(QTreeView* treeView);
    std::vector<cuarl_path::Path> get_paths(const char* filename) const;
    void update_segment_editor();
    cuarl_path::Segment* addSegment();
    ~RoverPlanner();

private Q_SLOTS:
    void treeSelectionChanged(const QModelIndex& prevIndex, const QModelIndex& nextIndex);

private:
    Ui::RoverPlanner *ui;
};

在RoverPlanner.cpp


RoverPlanner::RoverPlanner(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::RoverPlanner)
{
    ui->setupUi(this);

    QPushButton* btnLoadPaths = this->findChild<QPushButton*>("btn_load_paths");
    QPushButton* btnSavePaths = this->findChild<QPushButton*>("btn_save_paths");
    QPushButton* btnExecutePath = this->findChild<QPushButton*>("btn_execute_path"  );
    QPushButton* btnAddSegment = this->findChild<QPushButton*>("btn_add_to_path");

    QTreeView* treeView = this->findChild<QTreeView*>("tree_paths");

    connect(btnLoadPaths, &QPushButton::clicked, this, [=]() { load_paths_into_tree(treeView); });

 connect(treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &RoverPlanner::treeSelectionChanged); // This does not seem to properly bind the index chan

}

void RoverPlanner::treeSelectionChanged(const QModelIndex &prevIndex, const QModelIndex &nextIndex) {
    std::cout << "Test" << std::endl;
}

//other functions

当我单击项目时,它不会在控制台中输出任何内容enter image description here

我很困惑,因为这似乎是正确连接树状视图所选索引的方法。我做错了什么?

c++ qt qtreeview
1个回答
1
投票

selectionModel每次为QTreeView设置新模型时都会被替换。

void QAbstractItemView::setModel(QAbstractItemModel *model)

此功能将创建并设置一个新的选择模型,用setSelectionModel()替换以前设置的任何模型。

这意味着您每次设置新型号时都需要重新连接&QItemSelectionModel::currentChanged信号。

© www.soinside.com 2019 - 2024. All rights reserved.