Results 1 to 14 of 14

Thread: QListView Event - help needed

  1. #1
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4

    Unhappy QListView Event - help needed

    Hi,

    I am a newbie to QT, and trying to learn it myself. I am currently try to capture signal of a list view (selectionchanged or clicked). I have been trying awhile with these calls now but it does not seem to do anything. I debugged and found out that "err_method_notfound(sender, signal_arg, "connect")"

    list = new QListView();
    list->setViewMode(QListView::IconMode);
    list->setFlow(QListView::TopToBottom);
    list->setFixedWidth(100);
    list->setWrapping(FALSE);

    list->setModel(new ListModel(rscPath));

    connect(list, SIGNAL(selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )), this,
    SLOT(onCurrentChanged(const QItemSelection & selected, const QItemSelection & deselected )));


    void TransFuncEditorIntensityTexture:nCurrentChanged(const QItemSelection & selected, const QItemSelection & deselected )
    {
    list->setDisabled(true);
    }

    Could anyone let me know what have I done wrong? Thanks alot

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListView Event - help needed

    The correct signature for signal and slot is (simple example: a and b are objects, T is a type , slot/signal with one parameter only)
    Qt Code:
    1. QObject::connect( &a, SIGNAL( valueChanged(T) ), &b, SLOT( setValue(T) ) );
    To copy to clipboard, switch view to plain text mode 
    It is an error to add selected and deselected in it. Just use the type.

    Slot and signal can be provided by Qt or you can add your own signal and slot (i.e you have to provide a declaration and a definition in your class).
    Have look at http://doc.trolltech.com/4.6/signalsandslots.html#slots
    Last edited by toutarrive; 3rd April 2010 at 13:00. Reason: Adding link

  3. #3
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4

    Default Re: QListView Event - help needed

    Thanks for replying!

    I have changed my code to
    Qt Code:
    1. void TransFuncEditorIntensityTexture::createConnections()
    2. {
    3.  
    4. connect(list, SIGNAL(selectionChanged ( QItemSelection & , QItemSelection & )), this, SLOT(ListSelectionChanged(QItemSelection &, QItemSelection & )));
    5.  
    6. }
    7.  
    8. void TransFuncEditorIntensityTexture::ListSelectionChanged (QItemSelection &selected, QItemSelection &deselected )
    9. {
    10. list->setDisabled(true);
    11. }
    To copy to clipboard, switch view to plain text mode 
    It still doesnt work thought. Is there anything I missed?

    Thanks

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListView Event - help needed

    The signal selectionChanged implemented by Qt takes no parameter or a QListViewItem * as unique parameter:
    Qt Code:
    1. void selectionChanged ()
    2. void selectionChanged ( QListViewItem * )
    To copy to clipboard, switch view to plain text mode 
    If you want to use a signal/slot wich takes two QItemSelection as arguments, you've to implemented them in your class:
    SIGNAL(selectionChanged ( QItemSelection & , QItemSelection & ) )
    SLOT(selectionChanged ( QItemSelection & , QItemSelection & ) )
    BTW, QItemSelection class manages information about selected items in a model (it is basically a list of selection ranges, see QItemSelectionRange Class Reference), what for do you use this class in your signal?
    Last edited by toutarrive; 3rd April 2010 at 14:09.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QListView Event - help needed

    Quote Originally Posted by wererabit View Post
    Is there anything I missed?
    The references are const => SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&))

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QListView Event - help needed

    Quote Originally Posted by toutarrive View Post
    The signal selectionChanged implemented by Qt takes no parameter or a QListViewItem * as unique parameter:
    Qt Code:
    1. void selectionChanged ()
    2. void selectionChanged ( QListViewItem * )
    To copy to clipboard, switch view to plain text mode 
    You are talking of itemSelectionChanged, not selectionChanged!

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView Event - help needed

    First of all, there is no selectionChanged() signal in QListView - it's reimplemented protected slot - take a look at docs in Qt Assistant (which you should have open constantly to look for such things).
    Second of all, keep your eye on console output - there is a warning (for sure) saying that there is no such signal or no such slot.
    Third of all, in signal/slot signatures in connect statement use only types, not argument variable names. And only basic type is enough, so when argument is const QItemSelection &selected you can safely type QItemSelection in signal/slot signature in connect.
    Fourth of all, do not forget Q_OBJECT macro in your class where your slot is (and of course this class has to inherit QObject or any of its subclasses)
    Finally, about selections it is better (in my opinion) to use QItemSelectionModel and it's signals. You can get its instance from view after setting model to this view, just like that:
    Qt Code:
    1. QItemSelectionModel *sm = list->selectionModel();
    To copy to clipboard, switch view to plain text mode 
    It has interesting signals for you -> check them in QtAssistant!
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. The following user says thank you to faldzip for this useful post:

    igorbrp (25th August 2010)

  9. #8
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListView Event - help needed

    I was refering, by mystake, to Qt3.3 documentation instead of the latest one. Sorry for the confusion

  10. #9
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4

    Default Re: QListView Event - help needed

    Thank so much for all the replies. and sadly I lost again at the "Fourth of all".

    I am completely new to Qt, and have been trying to get this working for the last two days. Lets me explain what I want to do.

    What I have is a class in which I added a QListview and populate it with images

    The ListModel I created, in case you need to have a look
    Qt Code:
    1. //List Model
    2. class ListModel : public QAbstractListModel
    3. {
    4. public:
    5. ListModel(QString srcPath) : QAbstractListModel()
    6. {
    7. size = 72;
    8. QDirIterator it(srcPath);
    9. while (it.hasNext()) {
    10. QString file = it.next();
    11. if (file.endsWith(".jpg"))
    12. {
    13. m_values << file;
    14. }
    15. }
    16. }
    17. int rowCount(QModelIndex const & index) const
    18. {
    19. return m_values.count();
    20. }
    21. QVariant data(QModelIndex const & index, int role) const
    22. {
    23. if (role == Qt::DecorationRole)
    24. {
    25. QPixmap pixmap = QPixmap(m_values.at(index.row()));
    26. pixmap = pixmap.scaled( size, size, Qt::AspectRatioMode::KeepAspectRatio);
    27. return pixmap;
    28. }
    29.  
    30. if( role != Qt::DisplayRole && role != Qt::EditRole )
    31. return QVariant();
    32.  
    33. if( index.column() == 0 && index.row() < m_values.count() )
    34. return ""; //Text here
    35. else
    36. return QVariant();
    37. }
    38.  
    39. private:
    40. QList<QString > m_values;
    41. int size;
    42. };
    To copy to clipboard, switch view to plain text mode 

    Then creating the list
    Qt Code:
    1. QLayout* TransFuncEditorIntensityTexture::createColorLayout() {
    2.  
    3. const QString rscPath = "../../data/3DTextures/";
    4.  
    5. list = new QListView();
    6. list->setViewMode(QListView::IconMode);
    7. list->setFlow(QListView::TopToBottom);
    8. list->setFixedWidth(100);
    9. list->setWrapping(FALSE);
    10.  
    11. list->setModel(new ListModel(rscPath));
    12.  
    13. QHBoxLayout* hBox = new QHBoxLayout();
    14. hBox->addWidget(list);
    15.  
    16. if (orientation_ == Qt::Vertical)
    17. return hBox;
    18. else {
    19. QVBoxLayout* vbox = new QVBoxLayout();
    20. vbox->addLayout(hBox, 1);
    21. vbox->addStretch();
    22. return vbox;
    23. }
    24.  
    25. return hBox;
    26. }
    To copy to clipboard, switch view to plain text mode 

    And this is what I got
    List.png

    Now all what I want is when the user click on one of the image, I am notified (any particular method is called) . That is all, really. I guess its probably simpler than I expected but still. I have been trying all kinds of thing and currently. This is what I have

    Qt Code:
    1. connect(list, SIGNAL(selectionChanged ( QListViewItem* )), this, SLOT(selectionList( QListViewItem* )));
    2.  
    3. void TransFuncEditorIntensityTexture::selectionList(QListViewItem* item)
    4. {
    5. //Just make sure this method is called. but it never happens
    6. list->setDisabled(true);
    7. }
    To copy to clipboard, switch view to plain text mode 

    within TransFuncEditorIntensityTexture class definition, I have
    Qt Code:
    1. public slots:
    2. void selectionList(QListViewItem*);
    To copy to clipboard, switch view to plain text mode 

    I have been reading Slot and Signal article, running back and forth on the net. Could you guys please let me know what I am missing? or point me to where I can find the information.

    Really appriciate it

  11. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QListView Event - help needed

    Quote Originally Posted by faldżip View Post
    First of all, there is no selectionChanged() signal in QListView - it's reimplemented protected slot
    missed that...
    And only basic type is enough, so when argument is const QItemSelection &selected you can safely type QItemSelection in signal/slot signature in connect.
    Crazy, after reading "Signals and Slots" in the docs again there are two options: I am to stupid to read or they don't tell it there. But I works. Learned something more. Thanks.

    Quote Originally Posted by wererabit View Post
    Qt Code:
    1. connect(list, SIGNAL(selectionChanged ( QListViewItem* )), this, SLOT(selectionList( QListViewItem* )));
    2.  
    3. void TransFuncEditorIntensityTexture::selectionList(QListViewItem* item)
    4. {
    5. //Just make sure this method is called. but it never happens
    6. list->setDisabled(true);
    7. }
    To copy to clipboard, switch view to plain text mode 
    As faldżip said: use the selection model:
    Qt Code:
    1. connect(list->selectionModel(), /*...*/);
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4

    Default Re: QListView Event - help needed

    First of all, there is no selectionChanged() signal in QListView - it's reimplemented protected slot
    So where could I find the list of signal that are available?

    Thanks

  13. #12
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListView Event - help needed

    So where could I find the list of signal that are available?
    Here, look at signals http://doc.qt.nokia.com/4.6/qlistview.html

  14. The following user says thank you to toutarrive for this useful post:

    wererabit (5th April 2010)

  15. #13
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListView Event - help needed

    In Qt documentation. If you have Qt installed on your system then you also have Qt Assistant (QTDIR/bin/assistant.exe) which is the help browser. There you can find QListView docs where all of its functions are documented (don't forget it is derived from QAbstractView so check it's methods also). What you are looking for is rather QItemSelectionModel which is special class for handling item selections. You can find documentation also by clicking on class names in my post (the same docs as in QtAssistant).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  16. The following user says thank you to faldzip for this useful post:

    wererabit (5th April 2010)

  17. #14
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4

    Default Re: QListView Event - help needed

    Thank you!
    Last edited by wererabit; 5th April 2010 at 01:23.

Similar Threads

  1. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40
  2. Replies: 10
    Last Post: 15th January 2010, 14:35
  3. how to combine keypress event and mousebuttonpress event?
    By hildebrand in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2009, 23:08
  4. [Qt4.5] event(QEvent * event) freeze application
    By czlowiekcien in forum Newbie
    Replies: 2
    Last Post: 25th May 2009, 20:25
  5. Replies: 0
    Last Post: 23rd October 2008, 12:43

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.