#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#include <QtGui>
{
public:
static qreal size() {return 20;}
{
qreal penWidth = 1;
return QRectF(-penWidth
/2,
-penWidth
/2,size
() + penWidth,size
() + penWidth
);
}
{
painter->drawRoundedRect(0,0,size(),size(), 5, 5);
}
};
{
public:
{
m_n_x = n_x;m_n_y = n_y;
for (int i = 0;i < n_x;++i)
{
for (int j = 0;j < n_y;++j)
{
CheckerBoardSQ* sq = new CheckerBoardSQ(this);
sq->setPos(i*CheckerBoardSQ::size(),j*CheckerBoardSQ::size());
}
}
}
{
qreal penWidth = 1;
return QRectF(-penWidth
/2,
-penWidth
/2,m_n_x
*CheckerBoardSQ
::size() + penWidth,m_n_y
*CheckerBoardSQ
::size() + penWidth
);
}
{
painter->drawRect(0,0,m_n_x*CheckerBoardSQ::size(),m_n_y*CheckerBoardSQ::size());
}
private:
int m_n_x;
int m_n_y;
};
{
public:
MainWidget() {
scene->addItem(new CheckerBoard(8,8));
vl->addWidget(view);
setLayout(vl);
}
{
if (event->text() == "+") {
view->scale(1.1,1.1);
event->accept();
}
if (event->text() == "-") {
view->scale(0.90,0.90);
event->accept();
}
if (event->text() == "#") {
view->resetTransform();
event->accept();
}
}
private:
};
#endif // GAMEBOARD_H
--------------------
#include <QtCore>
#include <QtGui>
#include "gameboard.h"
int main(int argc, char *argv[])
{
MainWidget wdg;
wdg.show();
QObject::connect(&a,
SIGNAL(lastWindowClosed
()),
&a,
SLOT(quit
()));
return a.exec();
}
#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#include <QtGui>
class CheckerBoardSQ : public QGraphicsItem
{
public:
CheckerBoardSQ(QGraphicsItem *parent = 0,QGraphicsScene *scene = 0) : QGraphicsItem(parent,scene) {}
static qreal size() {return 20;}
QRectF boundingRect() const
{
qreal penWidth = 1;
return QRectF(-penWidth/2,-penWidth/2,size() + penWidth,size() + penWidth);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRoundedRect(0,0,size(),size(), 5, 5);
}
};
class CheckerBoard : public QGraphicsItem
{
public:
CheckerBoard(int n_x,int n_y,QGraphicsItem *parent = 0,QGraphicsScene *scene = 0) : QGraphicsItem(parent,scene)
{
m_n_x = n_x;m_n_y = n_y;
for (int i = 0;i < n_x;++i)
{
for (int j = 0;j < n_y;++j)
{
CheckerBoardSQ* sq = new CheckerBoardSQ(this);
sq->setPos(i*CheckerBoardSQ::size(),j*CheckerBoardSQ::size());
}
}
}
QRectF boundingRect() const
{
qreal penWidth = 1;
return QRectF(-penWidth/2,-penWidth/2,m_n_x*CheckerBoardSQ::size() + penWidth,m_n_y*CheckerBoardSQ::size() + penWidth);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(0,0,m_n_x*CheckerBoardSQ::size(),m_n_y*CheckerBoardSQ::size());
}
private:
int m_n_x;
int m_n_y;
};
class MainWidget : public QWidget
{
public:
MainWidget() {
scene = new QGraphicsScene();
scene->addItem(new CheckerBoard(8,8));
view = new QGraphicsView(scene);
QVBoxLayout* vl = new QVBoxLayout();
vl->addWidget(view);
setLayout(vl);
}
void keyPressEvent (QKeyEvent * event)
{
if (event->text() == "+") {
view->scale(1.1,1.1);
event->accept();
}
if (event->text() == "-") {
view->scale(0.90,0.90);
event->accept();
}
if (event->text() == "#") {
view->resetTransform();
event->accept();
}
}
private:
QGraphicsScene* scene;
QGraphicsView* view;
};
#endif // GAMEBOARD_H
--------------------
#include <QtCore>
#include <QtGui>
#include "gameboard.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget wdg;
wdg.show();
QObject::connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks