Frage

Basically I have a QTabWidget. At first it has a form widget, widget1. After that form is done being interacted with, the new widget2 form should take over that same tab.

I thought tabWidget->setCurrentWidget(new widget2()); would work, but it's basically an overload for setCurrentIndex(int).

Does anyone know of a way to do this?

War es hilfreich?

Lösung

You can use a QStackedWidget for this type of thing, in a tab or elsewhere.

Put all the widgets you'll want to display in that tab inside a single QStackedWidget, and place that stacked widget in a tab.

Here's a quick'n'dirty demo:

#include <QtGui>

class W: public QWidget
{
    Q_OBJECT

    public:
    W(QWidget *parent=0): QWidget(parent)
    {
        // stacked widget displays one of its "children" at a time
        QStackedWidget *sw = new QStackedWidget;
        QPushButton *b1 = new QPushButton("hello");
        sw->addWidget(b1);
        QPushButton *b2 = new QPushButton("world");
        sw->addWidget(b2);

        // tab widget and simplistic layout
        QTabWidget *tw = new QTabWidget(this);
        tw->addTab(sw, "tab");
        QHBoxLayout *l = new QHBoxLayout;
        l->addWidget(tw);
        setLayout(l);

        // signal mapper to demo the widget switching
        QSignalMapper *m = new QSignalMapper(this);
        connect(b1, SIGNAL(clicked()), m, SLOT(map()));
        m->setMapping(b1, 1);
        connect(b2, SIGNAL(clicked()), m, SLOT(map()));
        m->setMapping(b2, 0);
        connect(m, SIGNAL(mapped(int)), sw, SLOT(setCurrentIndex(int)));
    }
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top