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. QTableView - Not Getting Selection Changed Signal
Forum Updated to NodeBB v4.3 + New Features

QTableView - Not Getting Selection Changed Signal

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 16.2k 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.
  • J Offline
    J Offline
    jwomble
    wrote on last edited by
    #1

    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
    {
    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 the window with the table view, I allocate/initialize it like this:

    @//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 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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      daviddoria
      wrote on last edited by
      #2

      (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.-

      -connect(materialsTable,-
      -SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),-
      -dataModel,-
      -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'.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jwomble
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          daviddoria
          wrote on last edited by
          #4

          Yep, it's in the superclass: http://developer.qt.nokia.com/doc/qt-4.8/qabstractitemview.html#signalSection

          I was just complaining about that here :) : http://developer.qt.nokia.com/forums/viewthread/12604/

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jwomble
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              daviddoria
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                daviddoria
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jwomble
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jwomble
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      daviddoria
                      wrote on last edited by
                      #10

                      Here is a compilable example:

                      http://programmingexamples.net/wiki/Qt/ModelView/ItemSelectionModel

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

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

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        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