简体   繁体   English

Qt QTreeview currentChange 未发出

[英]Qt QTreeview currentChange is not emitted

I am trying to run some code when I select a new index in a QTreeView当我 select 在 QTreeView 中创建一个新索引时,我正在尝试运行一些代码

In RoverPlanner.h在 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;
};

In RoverPlanner.cpp在 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

When I click on the items, it does not output anything in the console当我单击项目时,控制台中没有 output 任何内容在此处输入图像描述

I'm confused because this seems to be the way to correctly connect the treeview selected index changed.我很困惑,因为这似乎是正确连接 treeview 所选索引更改的方法。 What did I do wrong?我做错了什么?

selectionModel gets replaced each time a new model is set for QTreeView .每次为QTreeView设置新的 model 时都会替换selectionModel

void QAbstractItemView::setModel(QAbstractItemModel *model) :无效 QAbstractItemView::setModel(QAbstractItemModel *model)

This function will create and set a new selection model, replacing any model that was previously set withsetSelectionModel() .这个 function 将创建并设置一个新的选择 model,替换之前使用setSelectionModel()设置的任何 model。

That means you need to reconnect the &QItemSelectionModel::currentChanged signal each time you set a new model.这意味着每次设置新的 model 时都需要重新连接&QItemSelectionModel::currentChanged信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM