0

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Do you not get any warnings as you shouldn't have the class name in the `SIGNAL` definition -- try `SIGNAL(openNewWindow())`. If at all possible use the [Qt5 connect syntax](https://wiki.qt.io/New_Signal_Slot_Syntax). – G.M. May 14 '17 at 15:40
  • No i did not get a warning. Tried both of those, did not work. What do you mean by if at all possible? Do you know any other way to open a window on QPushButton.clicked()? @G.M. – Icefrighter May 14 '17 at 15:56
  • If you're using Qt5 try `QObject::connect(playButton, &QPushButton::clicked, game, &PuzzleField::openNewWindow)`. – G.M. May 14 '17 at 16:13
  • Does your `PuzzleField` class definition include the mandatory `Q_OBJECT` macro? Please should the class definition for `PuzzleField`. – G.M. May 14 '17 at 16:24
  • Tried your first suggestion, didn't work even though i'm using QT5.9.0. As soon as i do include the Q_OBJECT it does say: "main.obj not found" – Icefrighter May 14 '17 at 17:06
  • Please edit your question to include a [MCVE]. – G.M. May 14 '17 at 17:33

0 Answers0