So I currently try to make a puzzle game where a new window shall be opened when I click the Playbutton
, receiving a SIGNAL from the button to open the SLOT function openNewWindow
Which looks like this:
Header:
class PuzzleField : public QPushButton{
signals: ....
public: ....
private: ....
public slots:
void openNewWindow();
}
CPP:
void PuzzleField::openNewWindow(){
gui = new QWidget();
gui->resize(400,200);
std::vector<int> numbersLeft;
for (int i = 0; i<= (this->height*this->width)-1; i++){
numbersLeft.push_back(i);
}
auto engine = std::default_random_engine{};
std::shuffle(std::begin(numbersLeft), std::end(numbersLeft), engine);
QGridLayout *grid = new QGridLayout(gui);
grid->setHorizontalSpacing(0);
grid->setContentsMargins(0,0,0,0);
grid->setVerticalSpacing(0);
for (int h = 0; h < this->height; h++){
for (int wi = 0; wi <this->width; wi++){
QPushButton *btn = new
QPushButton(QString::number(numbersLeft.back()),gui);
btn->resize(50,50);
grid->addWidget(btn,h,wi);
numbersLeft.pop_back();
}
}
gui->setLayout(grid);
gui->show();
}
I am trying to open the window like this:
QWidget *game = new QWidget();
QPushButton *playButton = new QPushButton("Play");
QObject::connect(playButton,SIGNAL(clicked()),game,SLOT(PuzzleField::openNewWindow()));
But the window does not open after clicking the button. Maybe I am just dumb, but I cannot figure out the solution.
When just calling the function without Signal/slot everything works, as it should, just pressing the button then opening the window does't work.
Thanks for help in advance.