Results 1 to 7 of 7

Thread: Help please - what am I doing wrong?

  1. #1
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Help please - what am I doing wrong?

    Hi there - I have created a class that extends QTableView. When the user changes which items are selected in the view, I would expect that the class would send a selectionChanged signal. However, I cannot seem to connect that signal to a slot. I am pretty sure that my syntax is correct, but I must be doing something wrong since it's not working.
    Qt Code:
    1. // gridview.h
    2. #ifndef GRIDVIEW_H
    3. #define GRIDVIEW_H
    4.  
    5. #include <QTableView>
    6. #include <QHeaderView>
    7. #include <QItemSelection>
    8. #include <QItemSelectionModel>
    9.  
    10. class GridView : public QTableView
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. GridView(QWidget *parent = 0);
    16. ~GridView();
    17. private slots:
    18. void testSlot(const QItemSelection &selected, const QItemSelection &deselected);
    19. };
    20.  
    21. #endif // GRIDVIEW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // gridview.cpp
    2. #include "gridview.h"
    3.  
    4. GridView::GridView(QWidget *parent)
    5. : QTableView(parent)
    6. {
    7. connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)), this, SLOT(testSlot(const QItemSelection &selected, const QItemSelection &deselected)));
    8. }
    9.  
    10. GridView::~GridView() { }
    11.  
    12. void GridView::testSlot(const QItemSelection &selected, const QItemSelection &deselected)
    13. {
    14. // my code here never gets called...
    15. }
    To copy to clipboard, switch view to plain text mode 

    Any help is appreciated. Thanks in advance.

    Jimmy

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help please - what am I doing wrong?

    I'm not sure, but you're probably getting some info on the console (remember about CONFIG+= console debug) that Qt (or QMetaType) can't serialize QItemSelection or something like that. If so, then you have to register QItemSelection with Qt Metadata system using Q_DECLARE_METATYPE(QItemSelection).

  3. #3
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help please - what am I doing wrong?

    The problem is that you are listing the variable names in the connect statement.

    connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)), this, SLOT(testSlot(const QItemSelection &selected, const QItemSelection &deselected)));


    should be

    connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(testSlot(const QItemSelection &, const QItemSelection &)));

  4. #4
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help please - what am I doing wrong?

    I eliminated the variable names, but the problem remains. Following wysota's advice I was able to trap this error message:

    Object::connect: Cannot connect (null)::selectionChanged(const QItemSelection, const QItemSelection) to GridView::testSlot(const QItemSelection, const QItemSelection)


    This leads me to believe that perhaps the QItemSelectionModel has not been initialized (since it's referred to as 'null'). Is there some kind of initialization I need to do for the selection model before I can access it in the grid's constructor?

  5. #5
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help please - what am I doing wrong?

    Yes that was the problem. I created a new QItemSelectionModel and passed it in to the view's setSelectionModel() method. Now it works as expected.

    I had assumed that creating a new QTableView object would implicitly create the selection model object as well, but I guess I assumed wrong.

    Thanks for your help anyway.

    Jimmy

  6. #6
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help please - what am I doing wrong?

    Quote Originally Posted by Jimmy2775
    Yes that was the problem. I created a new QItemSelectionModel and passed it in to the view's setSelectionModel() method. Now it works as expected.

    I had assumed that creating a new QTableView object would implicitly create the selection model object as well, but I guess I assumed wrong.

    Thanks for your help anyway.

    Jimmy
    Once you set a model for the view with setModel(), a new QItemSelectionModel will be created. You don't have to create one.
    Save yourself some pain. Learn C++ before learning Qt.

  7. The following user says thank you to Chicken Blood Machine for this useful post:

    Jimmy2775 (6th March 2006)

  8. #7
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help please - what am I doing wrong?

    So then the problem was that I was trying to connect the selection model's signal in the constructor of the QTableView, before the view's model was set. Makes sense.

    Thanks

Similar Threads

  1. Replies: 7
    Last Post: 17th July 2009, 09:40
  2. mouseMoveEvent contains wrong position?
    By draftpunk in forum Qt Programming
    Replies: 10
    Last Post: 12th September 2008, 01:59
  3. something wrong about KolourPaint
    By laozi888 in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2008, 11:42
  4. Replies: 2
    Last Post: 2nd June 2008, 08:45
  5. QListWidget...what's wrong
    By nupul in forum Newbie
    Replies: 16
    Last Post: 4th April 2006, 12:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.