QTreeView slot on selection change



  • Hiho everybody,
    I want to connect a QTreeView to a slot, so that I can call a function every time I click on a row of this treeview.
    The problem is that it doesn't work and I dont know why.
    Here is what I have:
    @QStandardItemModel* model = new QStandardItemModel(frame);
    QStandardItem* terrain = new QStandardItem(QIcon("../media/icon_crosshairs16x16bw1.gif"), "Terrain");
    QStandardItem* ground = new QStandardItem(QIcon("../media/icon_crosshairs16x16bw1.gif"), "Ground");
    QStandardItem* water = new QStandardItem(QIcon("../media/icon_crosshairs16x16bw1.gif"), "Water");
    //some more items
    terrain->setFlags(terrain->flags() & ~Qt::ItemIsEditable);
    ground->setFlags(ground->flags() & ~Qt::ItemIsEditable);
    water->setFlags(water->flags() & ~Qt::ItemIsEditable); //to prevent the user from modifying the text of the selection

    model->appendRow(terrain);
    terrain->appendRow(ground);
    terrain->appendRow(water);

    QTreeView * treeView = new QTreeView(frame);
    treeView->setModel(model);
    @

    I dont know if that is the right way to create an expandable treeview but at least this works :)

    And now the part where I think the problem is:
    @QObject::connect(treeView->selectionModel(), SIGNAL(itemChanged ( QStandardItem * item )), MainWindow, SLOT(slot_selectionChanged()));@

    I hope somebody can help be.
    Thanks in advance and best regards,
    Ritschratsch



  • According to this page: http://qt-project.org/doc/qt-5.0/qtcore/qitemselectionmodel.html QItemSelectionModel doesn't have a signal called "itemChanged"... use currentChanged instead



  • Hi Mr.Universe,
    thank you for your answer, I just tried it, but without success :(
    slot_selectionChanged() is not called


  • Moderators

    are you sure you just didn't rename you signal in your code to currentChanged. The correct connect statement should look like this:

    @
    QObject::connect(treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), MainWindow, SLOT(slot_selectionChanged()));
    @

    When your connected slot isn't called by the signal you can also check the console for a meaningful error message in case the connection couldn't be made.


Log in to reply
 

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