Przepraszam za brzydki kod...
Ważne pliki:
obraz.cpp
#include "obraz.h" #include <QImage> #include <QMessageBox> #include <QPoint> obraz::obraz(QWidget *parent) : QGraphicsView(parent) { //QImage *image2 = new QImage(300, 300, QImage::Format_RGB32); this->setMouseTracking(true); this->setAlignment(Qt::AlignCenter); this->setVisible(true); this->setGeometry(0, 80, 303, 503); this->gra=false; image.load("c:/j.bmp"); this->generujObraz(); } void obraz::mouseMoveEvent(QMouseEvent *event) { this->piksel = this->image.pixel(event->x(), event->y()); QColor kolor(this->piksel); int r, g, b, a; kolor.getRgb(&r, &g, &b, &a); if (r==0 && g==255 && b==0) { if (gra==false) { gra=true; emit naStarcie(); } } else if (r==255 && g==0 && b==0 && gra==true) { gra=false; this->koniecGry(); emit koniec(); } else if (!(r==255 && g==255 && b==255 && gra==true)) { gra=false; emit pozaMapa(); } } void obraz::koniecGry() { QMessageBox *wiadomosc = new QMessageBox; wiadomosc->setText("Wygrałeś"); } void obraz::generujObraz() { //QRgb kolor; //kolor = qRgb(0, 255, 0); //QPoint punkt; //punkt.setX(5); //punkt.setY(5); //this->image.setPixel(punkt, kolor); scena.addPixmap(QPixmap::fromImage(image)); this->setScene(&scena); }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "obraz.h" #include "wynik.h" #include <QTimer> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); wynik *Wynik = new wynik(this); obraz *Obraz = new obraz(this); QTimer *timer = new QTimer(this); timer->setInterval(1); QObject::connect(Obraz, SIGNAL(pozaMapa()), timer, SLOT(stop())); QObject::connect(Obraz, SIGNAL(koniec()), timer, SLOT(stop())); QObject::connect(Obraz, SIGNAL(naStarcie()), Wynik, SLOT(wynikStartowy())); QObject::connect(Obraz, SIGNAL(naStarcie()), timer, SLOT(start())); QObject::connect(timer, SIGNAL(timeout()), Wynik, SLOT(zmniejszCzas())); //timer->start(1); } MainWindow::~MainWindow() { delete ui; }
