C++ (Qt)qobject_cast< QStackedWidget* >( parentWidget() )->setCurrentWidget( this );
class WidgetStacked : public QStackedWidget{ Q_OBJECTpublic: explicit WidgetStacked(QWidget *parent = 0) { this->wgtOpenEnter = new WidgetOpenEnter(this); this->addWidget(this->wgtOpenEnter); //Создание и добавление кучи других виджетов... this->setCurrentIndex(0); }private: WidgetOpenEnter * wgtOpenEnter; //Куча других виджетов};
class WidgetOpenEnter : public QWidget{ Q_OBJECTpublic: explicit WidgetOpenEnter(QWidget *parent = 0) { this->btn1 = new QPushButton(QString::fromUtf8("Кнопка 1")); this->btn1->setObjectName(QString("Button1")); connect(this->btn1, SIGNAL(clicked()), this, SLOT(ButtonClick())); this->btn2 = new QPushButton(QString::fromUtf8("Кнопка 2")); this->btn2->setObjectName(QString("Button2")); connect(this->btn2, SIGNAL(clicked()), this, SLOT(ButtonClick())); this->btn3 = new QPushButton(QString::fromUtf8("Кнопка 3")); this->btn3->setObjectName(QString("Button3")); connect(this->btn3, SIGNAL(clicked()), this, SLOT(ButtonClick())); /*... Дальше помещаю в QVBoxLayout и т.д. */ }private slots: void ButtonClick() { if(sender()->objectName() == QString("Button1")) { //Вот тут и надо менять индекс текущего виджета } //else if ... }private: QPushButton * btn1, * btn2, * btn3;};
C++ (Qt)template< typename T > T QUFindParent( QObject* o ){ while( o ) { if( T po = qobject_cast< T >( o ) ) return po; o = o->parent(); } return 0;}void ButtonClick(){ if(sender()->objectName() == QString("Button1")) { //Вот тут и надо менять индекс текущего виджета if( QStackedWidget* sw = QUFindParent< QStackedWidget* >( sender() ) ) if( WidgetOpenEnter* w = QUFindParent< WidgetOpenEnter* >( sender() ) ) sw->setCurrentIndex( w ); } //else if ...}
class Widget : public QWidget{Q_OBJECTpublic: explicit Widget (QStackedWidget * sw, QWidget * parent = 0) { this->sw = sw; }private: QStackedWidget * sw;}
((QStackedWidget *)this->parentWidget())->setCurrentIndex(...);