QTableView - Not Getting Selection Changed Signal

  • I am fairly new to QT, and am having trouble understanding how the QTableView selection changed signal is handled. I have setup a window with an openGL widget and a QTableView. I have a data model class that is correctly populating the tableview, so I added a public slot to that class:

    @class APartsTableModel : public QAbstractTableModel
    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 the window with the table view, I allocate/initialize it like this:

    @//create the display view
    AStarModelView *displayWindow = new AStarModelView(this,

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

    //setup model and signal

    SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
    SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));

    //show the view

    When I set a breakpoint in the implementation of the slot function, I never hit it. I've also tried not allocating a new QItemSelectionModel, but that didn't work either. I'm really not sure what I'm doing wrong here.

  • (There indeed is no selectionChanged signal of a QTableView, sorry)
    -Do you really need the QItemSelectionModel? It doesn't sound like you are doing anything that complex. I think you want to connect the selectionChanged model of the VIEW to a slot.-

    -SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),-
    -SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));-

    I would also name your materialsTable materialsTableView or something like that - as to not confuse if the 'table' is a 'tableModel' or a 'tableView'.

  • Does QTableView have a selectionChanged signal? I can't find it in the class reference.

  • I see a slot for selectionChanged() in QAbstractItemView, but not a signal.

  • Yikes - please ignore everything I've said so far - I was a bit confused (clearly).

    Can you do this - handle the clicked() signal of the view (or the mouseRelease event if you're making bigger selections) and then use the view's selectedIndexes function to get the selection?

  • Gah - it is listed under public functions (http://developer.qt.nokia.com/doc/qt-4.8/qlistview.html#memberSection) even though it is protected (it even says protected right in the declaration in the doc!). Sorry, without subclassing the view this method won't do it for you.

  • As far as I can tell, using the selectionModel is the correct way to do this, I just can't figure out what I'm doing wrong.

  • What's the first thing you should check in QT when signals/slots don't seem to be working correctly? That your class has the Q_OBJECT macro in it. Added this to the APartsTable class definition, and now I'm hitting the breakpoint.

    When does Friday get here?

  • The table implicitly has a selction model, get with <code>tableView->selectionModel()</code>, you don't need to create a new one.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.