Results 1 to 5 of 5

Thread: Unable to get signals from QSignalMapper

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Unable to get signals from QSignalMapper

    I'm trying to create a custom tab control - A widget with QToolButtons laid in QVBoxLayout and an QStackedLayout placed adjacently. Here my QToolButtons are in contained in QList.
    I've written a function called addTab(...) which adds buttons to the vertical layout dynamically and also the widget to be show to stacked layout.

    My Question:
    Signal mapper object's connect statement is written even before the mapping is done. So is this the reason the button click is not emitting signal ?
    Qt Code:
    1. // ctabwidget.cpp
    2. #include "ctabwidget.h"
    3. #include <QHBoxLayout>
    4. #include <QVBoxLayout>
    5. #include <QToolButton>
    6. #include <QStackedLayout>
    7. #include <QDebug>
    8. #include <QSignalMapper>
    9.  
    10. CTabWidget::CTabWidget(QWidget *parent) :
    11. QWidget(parent)
    12. {
    13. mainLayout = new QHBoxLayout;
    14. mainLayout->setSpacing(0);
    15. mainLayout->setContentsMargins(0, 0, 0, 0);
    16. this->setLayout(mainLayout);
    17.  
    18. buttonLayout = new QVBoxLayout;
    19. buttonLayout->setSpacing(0);
    20.  
    21. stackLayout = new QStackedLayout;
    22. stackLayout->setContentsMargins(0, 0, 0, 0);
    23. stackLayout->setSpacing(0);
    24.  
    25. mainLayout->addLayout(buttonLayout);
    26. mainLayout->addLayout(stackLayout);
    27.  
    28. signalMapper = new QSignalMapper;
    29. connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(selectTab(int)));
    30. }
    31.  
    32. CTabWidget::~CTabWidget()
    33. {
    34. qDeleteAll(tabButtons);
    35. }
    36.  
    37. // This will be called after the constructor is executed
    38. // Is this the reason why signals aren't emitted on button click ??
    39. void CTabWidget::addTab(const QString & normalIcon, const QString & hoverIcon, const QString & selectIcon, QWidget * tabWidget)
    40. {
    41. tabButtons.append(new CTabButton());
    42. CTabButton * tabButton = tabButtons.last();
    43. tabButton->setFixedSize(196, 54);
    44. tabButton->setCheckable(true);
    45. tabButton->setFocusPolicy(Qt::NoFocus);
    46. tabButton->setStyleSheet("QToolButton { border-image: url(" + normalIcon + "); border: none; border-top-left-radius: 2px; border-bottom-left-radius: 2px; background-color: transparent; }"
    47. "QToolButton:hover { border-image: url(" + hoverIcon + "); border: none; }"
    48. "QToolButton:checked { border-image: url(" + selectIcon + "); border: none; }");
    49. buttonLayout->addWidget(tabButton);
    50. stackLayout->addWidget(tabWidget);
    51. connect(tabButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    52. int buttonId = tabButtons.size();
    53. signalMapper->setMapping(tabButton, buttonId);
    54. qDebug() << buttonId;
    55. }
    56.  
    57. void CTabWidget::doneAddingTabs(void)
    58. {
    59. buttonLayout->addStretch();
    60. }
    61.  
    62. // slot
    63. void CTabWidget::selectTab(int tabId)
    64. {
    65. tabId = tabId - 1;
    66. qDebug() << "Tab Id - " << tabId;
    67. if(tabId < 0)
    68. return;
    69.  
    70. stackLayout->setCurrentIndex(tabId);
    71. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp
    2. #include "ctabwidget.h"
    3. #include <QApplication>
    4. #include <QLabel>
    5. #include <QList>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. QList<QLabel *> labels;
    12.  
    13. for(int i = 0; i < 4; i++)
    14. {
    15. labels.append(new QLabel(QString::number(i+1)));
    16. }
    17.  
    18. CTabWidget w;
    19. w.addTab(":/tabIcons/idbak.png", ":/tabIcons/idbak_sel.png", ":/tabIcons/idbak_sel.png", labels.at(0));
    20. w.addTab(":/tabIcons/idrst.png", ":/tabIcons/idrst_sel.png", ":/tabIcons/idrst_sel.png", labels.at(1));
    21. w.addTab(":/tabIcons/idsch.png", ":/tabIcons/idsch_sel.png", ":/tabIcons/idsch_sel.png", labels.at(2));
    22. w.addTab(":/tabIcons/idlog.png", ":/tabIcons/idlog_sel.png", ":/tabIcons/idlog_sel.png", labels.at(3));
    23. w.doneAddingTabs();
    24. w.show();
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Thank you.

  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: Unable to get signals from QSignalMapper

    Quote Originally Posted by rawfool View Post
    Signal mapper object's connect statement is written even before the mapping is done. So is this the reason the button click is not emitting signal ?
    No. As long as the connection is set up before the button is clicked that should be fine.

    Have you verified that both connect statements worked? I.e. is their return value true?

    Why not use a zero based button id and connect the signal mapper directly to the stacked layout's setCurrentIndex() slot?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Unable to get signals from QSignalMapper

    Quote Originally Posted by anda_skoa View Post
    Have you verified that both connect statements worked? I.e. is their return value true?
    Yeah, it's true.

    Quote Originally Posted by anda_skoa View Post
    Why not use a zero based button id and connect the signal mapper directly to the stacked layout's setCurrentIndex() slot?
    Ok. I did that.
    Qt Code:
    1. connect(signalMapper, SIGNAL(mapped(int)), stackLayout, SLOT(setCurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void CTabWidget::addTab(const QString & normalIcon, const QString & hoverIcon, const QString & selectIcon, QWidget * tabWidget)
    2. {
    3. ...
    4. ...
    5. int buttonId = tabButtons.size() - 1;
    6. ...
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    But still I'm not able to switch between pages in the QStackedLayout on button click.

  4. #4
    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: Unable to get signals from QSignalMapper

    Do the buttons emit their signal?

    I.e. if you connect a slot to the buttons' clicked() signals, does it get invoked? Something like
    Qt Code:
    1. void CTabWidget::onButtonClicked()
    2. {
    3. qDebug() << Q_FUNC_INFO << QObject::sender();
    4. }
    To copy to clipboard, switch view to plain text mode 
    If not, try connecting to the toggled() signal

    Cheers,
    _

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

    rawfool (1st April 2014)

  6. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Unable to get signals from QSignalMapper

    Thank you very much for helping me to check for button click signals. The problem was that! My custom button was not emitting the signal due to a small mistake in void mousePressEvent(QMouseEvent * event).
    Qt Code:
    1. void mousePressEvent(QMouseEvent * event)
    2. {
    3. if(event->button() == Qt::RightButton)
    4. {
    5. return;
    6. }
    7. else if(event->button() == Qt::MiddleButton)
    8. {
    9. return;
    10. }
    11. else // I forgot to add the else case and return the necessary event
    12. {
    13. QToolButton::mousePressEvent(event);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Thank you.

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. Unable to catch QHeaderView signals
    By mule in forum Qt Programming
    Replies: 5
    Last Post: 1st February 2012, 13:28
  4. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 21:21
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

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.