QTableView - not getting a selection. Modified signal

I'm new to QT, and it's hard for me to understand how the QTableView change signal is handled. I have a window with openGL and QTableView widgets. I have a data model class that populates the table view correctly, so I added a public slot to this class:

 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); }; 

When I am ready to show a window with a table view, I select / initialize it as follows:

 //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(); 

When I set a breakpoint when implementing a slot function, I never hit it. I also tried not to allocate a new QItemSelectionModel , but that didn't work either. I'm really not sure what I'm doing wrong here.

+4
source share
4 answers

Just to get the answer out of the discussion:

What is the first thing you should check in QT when the signals / slots don't seem to work correctly? That your class has a Q_OBJECT macro in this. Added this definition of the APartsTable class, and now I hit the breakpoint

.

0
source

When you call setModel () on the view, your locally distributed QItemSelectionModel is replaced with the one created by the view. In any case, you do not have to create your own model of choice. Just change the connection to

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

What is the first thing you should check in QT when the signals / slots do not seem to work correctly? That your class has a Q_OBJECT macro in it. This APartsTable class definition has been added, and now I'm at a breakpoint.

When does Friday come?

0
source

virtual Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const should return Qt::ItemIsSelectable | otherFlags Qt::ItemIsSelectable | otherFlags

-1
source

Source: https://habr.com/ru/post/1387418/


All Articles