Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView slot on selection change
Forum Updated to NodeBB v4.3 + New Features

QTreeView slot on selection change

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 7.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ritschratsch
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mario84
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ritschratsch
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved