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
#ifndef RYSUJ_PLANSZE_H
#define RYSUJ_PLANSZE_H
#include <QPushButton>
#include <QGridLayout>
class rysuj_plansze
{
public:
void rysuj();
private:
};
#ifndef RYSUJ_PLANSZE_H
#define RYSUJ_PLANSZE_H
#include <QPushButton>
#include <QGridLayout>
class rysuj_plansze
{
public:
void rysuj();
private:
QPushButton *button;
};
To copy to clipboard, switch view to plain text mode
rysuj_plansze.cpp
#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();
}
#include "rysuj_plansze.h"
#include <QApplication>
#include <QLabel>
#include <QtWidgets>
#include <QtCore>
#include <QMessageBox>
#include <QDialog>
void rysuj_plansze::rysuj()
{
QWidget *window = new QWidget;
QGridLayout *layout = new QGridLayout;
window->setWindowTitle("TIc-Tac-Toe");
QMessageBox msgBox;
msgBox.addButton("3na3", QMessageBox::YesRole);
msgBox.addButton("5na5", QMessageBox::NoRole);
msgBox.setIcon(QMessageBox::Question);
msgBox.setText("Which board do you chose?");
msgBox.exec();
if(msgBox.exec() == QMessageBox::Yes)
{
int width = 3;
int height = 3;
for(int i = 0; i<height; i++)
{
for(int j = 0; j<width; j++)
{
QPushButton *button = new QPushButton;
layout->addWidget(button,i,j);
}
}
}
else if(msgBox.exec() == QMessageBox::No)
{
int width = 20;
int height = 20;
for(int i = 0; i<height; i++)
{
for(int j = 0; j<width; j++)
{
QPushButton *button = new QPushButton;
layout->addWidget(button,i,j);
}
}
}
window->setLayout(layout);
window->show();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#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();
}
#include "rysuj_plansze.h"
#include <QApplication>
#include <QLabel>
#include <QtWidgets>
#include <QtCore>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
rysuj_plansze R;
R.rysuj();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
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)
Bookmarks