Results 1 to 2 of 2

Thread: QSignalMapper problems

  1. #1
    Join Date
    May 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QSignalMapper problems

    Hi,

    I am having problems with slots getting called multiple times. Each time I add another mapping, the slot gets called n+1 times - where n is the previous number of times called. Made a very simple demo program - can someone tell me what I'm doing wrong here?

    Project is a qtreewidget that starts with one button to add a top level item. Each top level item adds a button to add a child. Adding a top level always just adds one (not part of the mapping), but adding a child has exponential growth. Assuming I'm somehow inserting it into the mapping in a looping manner, but just can't seem to figure out where.

    testwidget.h
    Qt Code:
    1. #pragma once
    2. #include "qwidget.h"
    3. #include "qsignalmapper.h"
    4.  
    5. class FilterGroup;
    6. class FilterGroupWidget;
    7.  
    8. class TestWidget: public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. TestWidget(QWidget* pParent);
    14.  
    15. private Q_SLOTS:
    16. void addNewFilter();
    17. void addChildFilter(QWidget *pItem);
    18.  
    19. private:
    20. void updateRow(QTreeWidgetItem *pRow);
    21.  
    22. QTreeWidget *m_pTree;
    23. QSignalMapper m_Mapper;
    24. };
    To copy to clipboard, switch view to plain text mode 

    testwidget.cpp
    Qt Code:
    1. #include "stdafx.h"
    2. #include "TestWidget.h"
    3. #include "QtWidgets\qboxlayout.h"
    4. #include "FilterColumns.h"
    5. #include "qtreewidget.h"
    6. #include "qheaderview.h"
    7. #include "FilterTypeWidgets.h"
    8. #include "FilterWidget.h"
    9.  
    10. TestWidget::TestWidget(QWidget* pParent):QWidget(pParent)
    11. {
    12. auto pMainLayout=new QVBoxLayout();
    13. m_pTree=new QTreeWidget();
    14. m_pTree->setColumnCount(1);
    15.  
    16. auto pRow=new QTreeWidgetItem();
    17. m_pTree->insertTopLevelItem(0, pRow);
    18.  
    19. auto pAdd=new QPushButton();
    20. pAdd->setText("Add top level");
    21. m_pTree->setItemWidget(pRow, 0, pAdd);
    22.  
    23. connect(pAdd, &QPushButton::clicked, this, &TestWidget::addNewFilter);
    24.  
    25. setLayout(pMainLayout);
    26. pMainLayout->setMargin(0);
    27. pMainLayout->addWidget(m_pTree);
    28. }
    29.  
    30.  
    31. void TestWidget::addNewFilter()
    32. {
    33. auto pRow=new QTreeWidgetItem();
    34. m_pTree->insertTopLevelItem(m_pTree->topLevelItemCount() - 1, pRow);
    35.  
    36. updateRow(pRow);
    37. }
    38.  
    39. void TestWidget::addChildFilter(QWidget *pItem)
    40. {
    41. auto pParent=(QTreeWidgetItem *)pItem;
    42. auto pRow=new QTreeWidgetItem();
    43.  
    44. pParent->addChild(pRow);
    45. pParent->setExpanded(true);
    46.  
    47. updateRow(pRow);
    48. }
    49.  
    50. void TestWidget::updateRow(QTreeWidgetItem *pRow)
    51. {
    52. auto pWidget=new QPushButton("add child");
    53. auto test=connect(pWidget, SIGNAL(clicked()), &m_Mapper, SLOT(map()));
    54. m_Mapper.setMapping(pWidget, (QWidget *)pRow);
    55.  
    56. connect(&m_Mapper, SIGNAL(mapped(QWidget *)), this, SLOT(addChildFilter(QWidget *)));
    57.  
    58. m_pTree->setItemWidget(pRow, 0, pWidget);
    59. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper problems

    In line 56 you call connect from the mapper to a slot.
    Every time updateRow() is called.
    So obviously you get as many connections as updateRow() calls.

    You probably want to connect only once, e.g. in the constructor.

    Cheers,
    _

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

    ChiBriTri (16th September 2014)

Similar Threads

  1. QSignalMapper
    By Cremers in forum Newbie
    Replies: 5
    Last Post: 25th July 2013, 20:54
  2. QSignalMapper
    By Ali Reza in forum Newbie
    Replies: 35
    Last Post: 30th November 2012, 09:12
  3. QSignalMapper
    By axisdj in forum Newbie
    Replies: 6
    Last Post: 16th September 2010, 01:52
  4. QSignalMapper and argument problems
    By harmodrew in forum Newbie
    Replies: 14
    Last Post: 7th August 2010, 19:20
  5. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 21:21

Tags for this Thread

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.