QTableView -没有得到选择改变信号



我对QT相当陌生,并且很难理解QTableView选择改变信号的处理方式。我已经设置了一个带有openGL小部件和QTableView的窗口。我有一个正确填充tableview的数据模型类,所以我给这个类添加了一个公共槽:

class APartsTableModel : public QAbstractTableModel
{
public:
    AVehicleModel *vehicle;
    explicit APartsTableModel(QObject *parent = 0);
    //MVC functions
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &paret) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
public slots:
    void selectionChangedSlot(const QItemSelection &newSelection,
                              const QItemSelection &oldSelection);
};

当我准备用表视图显示窗口时,我像这样分配/初始化它:

//create the display view
AStarModelView *displayWindow = new AStarModelView(this,
                                                   starModel->vehicle);
//create the datamodel for the table view
APartsTableModel *dataModel = new APartsTableModel(displayWindow);
dataModel->vehicle = starModel->vehicle;
//create selection model for table view
QItemSelectionModel *selModel = new QItemSelectionModel(dataModel);
displayWindow->materialsTable->setSelectionModel(selModel);
//setup model and signal
displayWindow->materialsTable->setModel(dataModel);
connect(selModel,
        SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
        dataModel,
        SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
//show the view
displayWindow->show();

当我在slot函数的实现中设置断点时,我从未碰到它。我也试过不分配新的QItemSelectionModel,但这也不起作用。我真不知道我哪里做错了。

当您在视图上调用setModel()时,您本地分配的QItemSelectionModel将被视图创建的QItemSelectionModel所取代。无论如何,您不必创建自己的选择模型。只需更改连接到

connect(displayWindow->materialsTable->selectionModel(),
        SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
        dataModel,
        SLOT(selectionChangedSlot(const QItemSelection&, const QItemSelection&)));

当信号/插槽似乎不能正常工作时,您应该在QT中检查的第一件事是什么?你的类中有Q_OBJECT宏。将它添加到APartsTable类定义中,现在我遇到了断点。

星期五什么时候到这儿?

把答案从讨论中拉出来:

当信号/插槽没有时,你应该在QT中检查的第一件事是什么看起来工作正常吗?你的类有Q_OBJECT宏在里面。将它添加到APartsTable类定义中,现在我到达断点

virtual Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const返回Qt::ItemIsSelectable | otherFlags

最新更新