[solved] qtoolbar and qstackwidget

  • I have a toolbar with icons at the left. I'd like to change the "perspective" on the mainwindow when I click on a toolbar icon.
    toolbar icon has only signal triggered or triggered(bool) and qstackwidget has only slot setCurrentIndex(int).
    Of course this is not working :)

    Any idea for replacement of qstackwidget? Or some solution?

  • You should use a "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html

  • [quote author="rcari" date="1344942978"]You should use a "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html[/quote]

    Can you send me a code snippet how can I solve this?
    In this case the QSignalMapper is not clear me.

  • This should do:
    @#include <QtGui/QtGui>

    int main(int argc, char** argv)
    QApplication app(argc, argv);

    QMainWindow mw;

    QSignalMapper mapper;

    QToolBar* tb = new QToolBar();

    QAction* tab;
    tab = tb->addAction(QObject::tr("Tab 1"));
    mapper.setMapping(tab, 0);
    mapper.connect(tab, SIGNAL(triggered(bool)), SLOT(map()));

    tab = tb->addAction(QObject::tr("Tab 2"));
    mapper.setMapping(tab, 1);
    mapper.connect(tab, SIGNAL(triggered(bool)), SLOT(map()));

    tab = tb->addAction(QObject::tr("Tab 3"));
    mapper.setMapping(tab, 2);
    mapper.connect(tab, SIGNAL(triggered(bool)), SLOT(map()));

    mw.addToolBar(Qt::TopToolBarArea, tb);

    QStackedWidget* stack = new QStackedWidget;
    QLabel* lbl;
    lbl = new QLabel(QObject::tr("TAB 1"));
    stack->addWidget(lbl); // index: 0
    lbl = new QLabel(QObject::tr("TAB 2"));
    stack->addWidget(lbl); // index: 1
    lbl = new QLabel(QObject::tr("TAB 3"));
    stack->addWidget(lbl); // index: 2
    // Connect the mapper
    stack->connect(&mapper, SIGNAL(mapped(int)), SLOT(setCurrentIndex(int)));


    return app.exec();

  • Ohhh! I big thank for you!!! :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.