My class isnt working properly and i dont know why (Newbie)
Hi!
Im new to Qt and im trying to make a class, that will create a board of buttons, depending on a user's choice but it doesnt work properly and i dont know why...
here is my code
rysuj_plansze.h
Code:
#ifndef RYSUJ_PLANSZE_H
#define RYSUJ_PLANSZE_H
#include <QPushButton>
#include <QGridLayout>
class rysuj_plansze
{
public:
void rysuj();
private:
};
rysuj_plansze.cpp
Code:
#include "rysuj_plansze.h"
#include <QApplication>
#include <QLabel>
#include <QtWidgets>
#include <QtCore>
#include <QMessageBox>
#include <QDialog>
void rysuj_plansze::rysuj()
{
window->setWindowTitle("TIc-Tac-Toe");
msgBox.setText("Which board do you chose?");
msgBox.exec();
{
int width = 3;
int height = 3;
for(int i = 0; i<height; i++)
{
for(int j = 0; j<width; j++)
{
layout->addWidget(button,i,j);
}
}
}
{
int width = 20;
int height = 20;
for(int i = 0; i<height; i++)
{
for(int j = 0; j<width; j++)
{
layout->addWidget(button,i,j);
}
}
}
window->setLayout(layout);
window->show();
}
main.cpp
Code:
#include "rysuj_plansze.h"
#include <QApplication>
#include <QLabel>
#include <QtWidgets>
#include <QtCore>
#include <QDialog>
int main(int argc, char *argv[])
{
rysuj_plansze R;
R.rysuj();
return app.exec();
}
When i try to run it in main.cpp the messagebox is displayed multiple times (instead of being displayed once) and in the end the window with the board is empty(instead of being 3on3 or 20on20)
Re: My class isnt working properly and i dont know why (Newbie)
Hi, you call QMessageBox::exec() multiple times:
line 23: msgBox.exec();
line 28: if(msgBox.exec() == QMessageBox::Yes)
line 45: else if(msgBox.exec() == QMessageBox::No)
Each time the messagebox is shown. Call exec() only once and store the return value for use in the if() and else if() lines, or use switch-case.
Ginsengelf
Re: My class isnt working properly and i dont know why (Newbie)
Thank You! its working rn!!!