Results 1 to 13 of 13

Thread: QListWidget clicked signal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default QListWidget clicked signal

    Hello,
    I want to know if is it possible to know when a QListWidget has been clicked, i can receive the envent when a QListWidgetItem has been clicked, but no when the QListWidget itself has been.

    Thank you,

    Asier

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget clicked signal

    Reimplement mousePressEvent() for the widget.
    Remember to call QListWidget::mousePressEvent at the end of it, since all item clicked signals are based on it.

    Regards

  3. The following user says thank you to marcel for this useful post:

    asieriko (8th August 2007)

  4. #3
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: QListWidget clicked signal

    Hello, i've tried this, but i get the following error.

    Qt Code:
    1. class MyListWidget : public QListWidget
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. protected:
    7. void mousePressEvent(QMouseEvent * e)
    8. {
    9. qDebug("mouse press event MyListWidget");
    10. emit clicked(e->pos());
    11. QListWidget::mousePressEvent();
    12. }
    13.  
    14. public: signals:
    15. void clicked(const QPoint & pos);
    16. };
    To copy to clipboard, switch view to plain text mode 

    error: no matching function for call to ‘MyListWidget::mousePressEvent()’

    If i remove the line
    Qt Code:
    1. QListWidget::mousePressEvent();
    To copy to clipboard, switch view to plain text mode 
    , it works (i get the press events) but this way i can't get the events made to each item of the list, it's also dificult to select the items.

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget clicked signal

    You forgot to pass the QMOuseEvent parameter for the base class version.
    It is:
    QListWidget::mousePressEvent(e);

    Regards

  6. The following user says thank you to marcel for this useful post:

    asieriko (8th August 2007)

  7. #5
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: QListWidget clicked signal

    OK, now it works.
    But, i want to be able to recieve such signals when this widget is part of a bigger one. At this point i can read the qDebug text, but i can't use it from an external widget. I mean this widget is in a Layout, and there are various of them.

    What shoul i do to receive now, this events?

  8. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget clicked signal

    What do you mean?
    Do you want something like a signal?
    You can define a clicked() signal in MyListWidget and emit it in mousePressEvent.

    Then you can connect this signal wherever you want.
    Be careful with what you do in the slot connected to it, though. Don't do anything that will take a long time, since you will block the event for too long.
    However, if you do something that takes a long time, then use Qt::QueuedConnection when connecting to clicked(). This way the slot will execute asynchronously.

    Regards

  9. The following user says thank you to marcel for this useful post:

    asieriko (8th August 2007)

  10. #7
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: QListWidget clicked signal

    Here it is my final code:
    Qt Code:
    1. class MyListWidget : public QListWidget
    2. {
    3. Q_OBJECT
    4. protected:
    5. void mousePressEvent(QMouseEvent * e)
    6. {
    7. qDebug("mouse press event MyListWidget");
    8. emit click();
    9. QListWidget::mousePressEvent(e);
    10. }
    11. public: signals:
    12. void click();
    13. };
    To copy to clipboard, switch view to plain text mode 

    Then i've created another widget which includes this one and i've defined another signal to provide the container of the second widget receive it.
    Qt Code:
    1. class dayCell : public QWidget{
    2. Q_OBJECT
    3. public:
    4. dayCell(QWidget *parent=0, char *name=0 );
    5. ~dayCell();
    6. public slots:
    7. void clicked2();
    8. public: signals:
    9. void clock();
    10. private:
    11. MyListWidget *listWidget;
    12. };
    To copy to clipboard, switch view to plain text mode 

    and in the cpp
    Qt Code:
    1. void dayCell::clicked2()
    2. {
    3. emit clock();
    4. }
    To copy to clipboard, switch view to plain text mode 

    And now if i use a dayCell widget i can get the clock() signal that comes here since the MyListWidget.

  11. #8
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: QListWidget clicked signal

    Ok,
    And now, how can i know what widget have i clicked if they are created in a loop?

    Qt Code:
    1. for (i=0;i<5;i++){
    2. dayCell *wi = new dayCell();
    3. connect(wi,SIGNAL(clock()),this,SLOT(aclear()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    I mean, if i want to add an item or those things. How can i know what wiget is it? i've tried with Object::sender(), but it doesn't work.
    Qt Code:
    1. void monthView::aclear()
    2. {
    3. qDebug(qPrintable(QObject::sender()->objectName()));
    4. // addItem("a");
    5. }
    To copy to clipboard, switch view to plain text mode 

    I don't get any name printed.

    Thanks

  12. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget clicked signal

    Should work.
    try:
    dayCell *c = qobject_cast<dayCell*>(sender());
    Remember that QObject::sender() is not static. it is only protected.

    Otherwise try(but as a last solution):
    Add a QObject* parameter to the signal:
    BTW, remove public: in front of signals:. Signals are always protected, they don't need visibility modifiers/
    Qt Code:
    1. signals:
    2. void clock(QObject*);
    To copy to clipboard, switch view to plain text mode 

    When you emit the signal, pass this as parameter:
    Qt Code:
    1. void dayCell::clicked2()
    2. {
    3. emit clock(this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    And the slot:
    Qt Code:
    1. for (i=0;i<5;i++)
    2. {
    3. dayCell *wi = new dayCell();
    4. connect(wi,SIGNAL(clock(QObject*)),this,SLOT(aclear(QObject*)));
    5. }
    6.  
    7. void monthView::aclear(QObject* o)
    8. {
    9. dayCell *c = qobject_cast<dayCell*>(o);
    10. if(c)
    11. {
    12. //add items, etc
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Regards

  13. The following user says thank you to marcel for this useful post:

    asieriko (10th August 2007)

  14. #10
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: QListWidget clicked signal

    Thank you, again, marcel.
    Now I've a final question. When a QListWidgetItem is clicked, i get 2 signals, one for the item itself (itemClicked), and the other from the whole widget (mousePressEvent). How can I know what of them is it?
    Thanks.

  15. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget clicked signal

    Well, in mousePressEvent, you can use QListWidget::itemAt(const QPoint&) to see if any item is at the click position.
    If you have an item there, don't emit the click signal.

    So, the list widget should emit a clicked signal only if you click outside any item.
    Otherwise you can't figure out what where the signal is coming from.

    Another way is to connect to different slots - one for the list widget and one for the items.

    Regards

Similar Threads

  1. QTreeWidget clicked signal
    By ^NyAw^ in forum Qt Programming
    Replies: 41
    Last Post: 30th January 2010, 12:42
  2. Replies: 13
    Last Post: 15th December 2006, 12:52
  3. Custom Widget - clicked() signal
    By ak in forum Newbie
    Replies: 3
    Last Post: 13th November 2006, 09:35
  4. Replies: 2
    Last Post: 17th May 2006, 22:01
  5. Replies: 10
    Last Post: 27th January 2006, 19:12

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.