0

所以我目前尝试制作一个益智游戏,当我单击 时会打开一个新窗口Playbutton,从按钮接收信号以打开 SLOT 功能,openNewWindow如下所示:

标题:

class PuzzleField : public QPushButton{
signals: ....
public:  ....
private: ....
public slots:
    void openNewWindow();
}

共产党:

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()));

但是单击按钮后窗口不会打开。也许我只是愚蠢,但我无法弄清楚解决方案。

当只调用没有信号/插槽的函数时,一切正常,只需按下按钮然后打开窗口就不起作用。

提前感谢您的帮助。

4

0 回答 0