Regarding QTabWidget and QSignalMapper

  • hello all, i want to fire a command which is a QString when a particular tab is selected using QSignalMapper depending on the tab_index i have selected.
    connect(tab_widget, SIGNAL(currentChanged(int)), signal_mapper, SLOT(map()));
    This doesn't work for me as map() slot don't take any arguments.
    Please help me with the same or show me other way round to do this stuff.

  • QSignapMapper is not the class you are looking for. QTabWidget already has a signal that contains an index for the selected tab. You can use that to make a mapping to a string yourself. Simply use a QStringList to store the strings, and lookup the corresponding string for each index, and then re-emit the signal with the string.

  • thanks andre for your suggestion but i want to make it work according to my design::-
    i want to store the strings as a property for each child widgets of QTabWidget by using setProperty() in a function set_command() where i pass qtabwidget, index of tab, string as an argument.
    So that when currentChanged() signal is emitted, it inquires the property of its current active widget and fires command.
    Also i dont want to store the strings as member variable in QStringList.
    Please do suggest, how to use signal slot in this case.

  • That was not the design you described in your first post, but ok. It is still simple. You can indeed also store the strings as a property in the tab widgets themselves. I think I would do something like this:


    class MyTabWidget: public QTabWidget {

    MyTabWidget(QWidget* parent=0);

    void currentChanged(QString);

    private slots:
    void onCurrentChanged(int index);

    // implementation
    MyTabWidget::MyTabWidget(QWidget* parent):
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(onCurrentChanged(int)));

    void MyTabWidget::onCurrentChanged(int index)
    QWidget* w = widget(index);
    if (w) {
    QString s = w->property("theStringProperty").toString();
    if (!s.isEmpty()) emit currentChanged(s);

    This should (not tested, brain to forum) emit a currentChanged(QString) signal if they have a QString property theStringProperty set.

  • thanks Andre, it really helped me.
    sorry for not posting details previous only.

Log in to reply

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