c++ - Qt QTreeview currentChange 没有发出
问题描述
当我在 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
我很困惑,因为这似乎是正确连接 treeview 选择索引更改的方法。我做错了什么?
解决方案
每次为 设置新模型时,selectionModelQTreeView
都会被替换。
无效 QAbstractItemView::setModel(QAbstractItemModel *model):
此函数将创建并设置一个新的选择模型,替换之前使用setSelectionModel()设置的任何模型。
&QItemSelectionModel::currentChanged
这意味着每次设置新模型时都需要重新连接信号。
推荐阅读
- javascript - 如何为属性创建 javascript eventlistner
- python - 在 Python 中读取 .json 文件时的预期对象或值
- mysql - 如何使用计数创建 laravel 多个连接并显示所有数据
- react-native - 无法找到由 `FlipperKit/FlipperKitLayoutPlugin` 依赖的`YogaKit (~> 1.18)` 的规范
- python - 如何在 python 中使用多处理来减少循环执行时间
- javascript - 在 react/react native (redux) 中使用扩展运算符
- javascript - Google 相册应用的“自动”过滤器如何工作?
- php - 尝试创建laravel查询以选择然后如果记录不存在则插入,如果存在于另一个表中则更新
- javascript - js arcgis地图编程
- selenium - 如何使用 ng-click 点击启用 angularjs 的元素