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.