所以我现在尝试制作一个益智游戏,当我点击Playbutton
时会打开一个新窗口,从按钮接收SIGNAL打开SLOT函数openNewWindow
如下所示:
部首:
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();
}
我试图像这样打开窗口:
QWidget *game = new QWidget();
QPushButton *playButton = new QPushButton("Play");
QObject::connect(playButton,SIGNAL(clicked()),game,SLOT(PuzzleField::openNewWindow()));
但单击按钮后窗口无法打开。也许我只是愚蠢,但我无法找到解决方案。
当只调用没有Signal / slot的功能时,一切正常,只需按下按钮然后打开窗口就不起作用了。
提前感谢您的帮助。