"Change widget class on button click" problem
Hi, I'm making qt gui application, and in mainwindow I have QWidget, which should become one of my custom widgets..
here's a code
Code:
MainWindow
::MainWindow(QWidget *parent
) :{
setMinimumSize(800,600);
QFormLayout *mainLayout=new QFormLayout();
widget->setStyleSheet("height: 400px; width: 500px;");
connect(onlineGameButton, SIGNAL(clicked()), this, SLOT(newOnlineGame()));
connect(exitButton, SIGNAL(clicked()), this, SLOT(exitProgram()));
btnsLayout->addWidget(onlineGameButton);
btnsLayout->addWidget(offlineGameButton);
btnsLayout->addWidget(simulateGameButton);
btnsLayout->addWidget(optionsButton);
btnsLayout->addWidget(exitButton);
btnsLayout->addStretch();
btns->setLayout(btnsLayout);
mainLayout->addRow(btns, widget);
...
tempWidget->setLayout(mainLayout);
setCentralWidget(tempWidget);
}
Code:
void MainWindow::newOnlineGame()
{
widget=new GameWidgetView;
}
i used debug to view if event is fired, and it is, but nothing happend, widget didn't become GameWidgetView...
widget is defined as private member of MainWindow, so when I add it to centralwidget, when i change widget, it should be changed and "that widget" in centralwidget, too, right(because of reference)?
sorry for my english
Re: "Change widget class on button click" problem
You only create a new widget and assign it to some variable. There is no "replacement" going on. If you want to replace one widget with another then either use QStackedWidget or delete the old widget and place the new one in a layout where the old widget was.
Re: "Change widget class on button click" problem
great! thanks lot, it works.
these are correction
//first change stupid name widget to less stupid name _widget :)
in mainwindow.h
in mainwindow.cpp
Code:
_widget->addWidget(new GameWidgetView);
_widget->setStyleSheet("height: 400px; width: 500px;");
...
}
void MainWindow::newOnlineGame()
{
_widget->setCurrentIndex(1);
}
this is exactle what i need because i have a lot of widget to put on this stack :)
Re: "Change widget class on button click" problem
There are several other ways you could do it. Simplest would be to parent the new widget with the old one and set the new widget's position to be (0,0) relative to the parent and its size to be the same as the parent. And, of course, don't forget to show() it.