I have been searching for tow days, but nothing that could help me.
I want to disconnect all signals while functions are running. The main trick is the class that emits signal and the class that receives it are both different classes. I have a QPushButton
in class that emits signals, and my custom class Screen
which receives signals, they are both connected.
The class that manages events (class sender)
Game::Game(QWidget *parent)
: QGraphicsView(parent)
{
//.................................//some declaration
//add the main screen
Screen * screen = new Screen();
screen->SetImageOnTheScreen();
scene->addItem(screen);
//make the lunch work
QPushButton * GO = new QPushButton;
GO->setText("GO!!!");
GO->setGeometry(134,-98,134,99);
scene->addWidget(GO);
QObject::connect(GO, SIGNAL(clicked(bool)), screen, SLOT(generate()));
//want to disconnect this connection while generate() doing its job
//.................................//some declaration
}
Class receiver
void Screen::SetImageOnTheScreen()
{
//.................................//some declaration
}
void Screen::setWinningIcon()
{
//.................................//some declaration
}
void Screen::generate()
{
line = 0;
std::random_shuffle(mas, mas + 27);
SetImageOnTheScreen();
if(mas[0] == mas[1] || mas[0] == mas[2] || mas[1] == mas[2])
{
line = 1;
delay();
setWinningIcon();
delay();
SetImageOnTheScreen();
}
if(mas[3] == mas[4] || mas[3] == mas[5] || mas[4] == mas[5])
{
line = 2;
delay();
setWinningIcon();
delay();
SetImageOnTheScreen();
}
if(mas[6] == mas[7] || mas[6] == mas[8] || mas[7] == mas[8])
{
line = 3;
delay();
setWinningIcon();
delay();
SetImageOnTheScreen();
}
}
I was trying to use disconnect
, and nothing. Was trying to use
QObject::blockSignals()
the same story.
Please any help would be great!!
UPDATE
game.h
#ifndef GAME_H
#define GAME_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPushButton>
#include "screen.h"
class Game : public QGraphicsView
{
private:
QGraphicsScene * scene;
QPushButton * GO;
Screen * screen;
protected:
virtual void wheelEvent (QWheelEvent * event);
public:
Game(QWidget *parent = 0);
~Game();
};
#endif // GAME_H
game.cpp
#include "game.h"
#include <QGraphicsGridLayout>
#include <QDebug>
#include <QPushButton>
#include "screen.h"
#include <QStyle>
Game::Game(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,400,200);
screen = new Screen;
screen->SetImageOnTheScreen();
scene->addItem(screen);
GO = new QPushButton;
GO->setGeometry(0,0,100,50);
GO->setText("GO");
QObject::connect(GO, SIGNAL(clicked(bool)), screen, SLOT(generate()));
//when generate is processing i want to disconnect the button "GO" and the "screen"
scene->addWidget(GO);
setScene(scene);
show();
}
void Game::wheelEvent(QWheelEvent * event)
{
if (event->type() == QEvent::Wheel)
return;
return;
}
Game::~Game()
{
}
screen.h
#ifndef SCREEN_H
#define SCREEN_H
#include <QGraphicsPixmapItem>
#include <QGraphicsTextItem>
class Screen : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
private:
int mas[27];
int line;
QGraphicsTextItem * text;
public:
Screen(QGraphicsPixmapItem * parent = 0);
void delay(int msecs);
void setWinningIcon();
void SetImageOnTheScreen();
public slots:
void generate();
};
#endif // SCREEN_H
screen.cpp
#include "screen.h"
#include <QDebug>
#include <QTime>
#include <QCoreApplication>
#include "game.h"
Screen::Screen(QGraphicsPixmapItem * parent)
: QObject(), QGraphicsPixmapItem(parent)
{
for(int i = 0, j =1; i < 27; i++, j++)
{
if(j == 10)
j = 1;
mas[i] = j;
}
line = 0;
text = new QGraphicsTextItem(this);
}
void Screen::delay(int msecs)
{
QTime dieTime= QTime::currentTime().addMSecs(msecs);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void Screen::setWinningIcon()
{
switch(line)
{
case 1:
text->setPlainText("TEST_#2 I'm NOT allowed to press /GO/ \nfor 3 seconds but i can");
text->setDefaultTextColor(Qt::red);
break;
case 2:
text->setPlainText("TEST_#3 I'm NOT allowed to press /GO/ \nfor 3 seconds but i can");
text->setDefaultTextColor(Qt::red);
break;
case 3:
text->setPlainText("TEST_#4 I'm NOT allowed to press /GO/ \nfor 3 seconds but i can");
text->setDefaultTextColor(Qt::red);
break;
}
}
void Screen::SetImageOnTheScreen()
{
text->setPlainText("TEST_#1 I'm ALLOWED to press /GO/");
text->setPos(0,50);
text->setDefaultTextColor(Qt::green);
}
void Screen::generate()
{
//In here i want to prevent to press "GO" while "generate()" is processing
//Screen::grabMouse(); //here it is my solution
std::random_shuffle(mas, mas + 27);
SetImageOnTheScreen();
if(mas[0] == mas[1] || mas[0] == mas[2] || mas[1] == mas[2])
{
line = 1;
delay(500);
setWinningIcon();
delay(3000);
SetImageOnTheScreen();
}
if(mas[3] == mas[4] || mas[3] == mas[5] || mas[4] == mas[5])
{
line = 2;
delay(500);
setWinningIcon();
delay(3000);
SetImageOnTheScreen();
}
if(mas[6] == mas[7] || mas[6] == mas[8] || mas[7] == mas[8])
{
line = 3;
delay(500);
setWinningIcon();
delay(3000);
SetImageOnTheScreen();
}
//Screen::ungrabMouse(); //here it is my solution
}
main.cpp
#include "game.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Game w;
w.show();
return a.exec();
}