[Solved]If i am using mouse press event or release event then signals and slot is not responding in my program. what is the reason behind it?
-
In my program i have used mouse press event and release event in button class. I have declared one slot in button class. When i clicked
the mouse only the mouse only mouse press event and released event is called. I am trying to call the slot also while pressing the mouse
by using
@QObject::connect (button1, SIGNAL(pressed()), button1, SLOT(slotdeclared()));@
but the slot is not called while pressing the mouse. so what would be the reason ?Edit: unfortunately, on the forum, code sections can not be inline. Fixed your formatting.; Andre
-
Below is my program. Please check it why the signal and slot is not working
@
window.cpp
#include <QtGui>
#include <QObject>class MyWindow: public QObject, public QWidget
{Q_OBJECT
public:
MyWindow(QWidget *parent = 0):QWidget(parent)
{
qDebug ()<<"window constructor";
float button_width = 47;
float button_height = 47;
int button_states = 1;QPixmap pixmap(":/endcall.png"); Button *button1 = new Button(button_width, button_height, button_states, pixmap, this); button1->setGeometry(60, 70, 80, 90); QObject::connect (button1, SIGNAL(clicked()), button1, SLOT(release())); pixmap.load(":/und_speaker.png"); button_width = 17; button_height = 14; Button *button2 = new Button(button_width, button_height, 1, pixmap, this); button2->setGeometry(100,110,120,130); pixmap.load(":/audio.png"); button_width = 17; button_height = 16; Button *button3 = new Button(button_width, button_height, 2, pixmap, this); button3->setGeometry(130,140,150,160); } ~ MyWindow(){} void mousePressEvent(QMouseEvent *k) { qDebug ()<<"window mouse press event"; QMainWindow window; window.show (); QWidget::mousePressEvent(k); } void mouseReleaseEvent(QMouseEvent *h) { qDebug ()<<"window mouse release event"; QWidget::mouseReleaseEvent(h); }
};
button.h
#include <QtGui>
#include <QObject>
#include <QEvent>class Button : public QPushButton
{Q_OBJECT
public:
Button(QWidget *parent = 0); Button(float button_width1, float button_height1, int button_states, QPixmap &, QWidget *parent = 0); ~Button(); void enterEvent(QEvent *); void leaveEvent(QEvent *);
public:
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void paintEvent(QPaintEvent *);public slots:
void press();
void release();private:
bool m_hovered; bool m_pressed; int num_of_states; int press_count; int count; int num_of_images; float button_width; float button_height; bool flag; QRectF target; QRectF source; QPixmap pixmap;
};
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWindow window;
window.show();
return a.exec();
}button.cpp
#include <QEvent>
Button::Button(QWidget *parent):
QPushButton(parent),
m_hovered(false),
m_pressed(false)
{}Button::Button (float button_width1,
float button_height1,
int button_states,
QPixmap &pixmap1,
QWidget *parent):
QPushButton( parent),
m_hovered(false),
m_pressed(false)
{
pixmap = pixmap1;
button_width = button_width1;
button_height = button_height1;
num_of_states = button_states;
num_of_images = pixmap.width()/button_width;
press_count = 0;
count = 0;
flag = true;
qDebug ()<<"button constructor"<<flag;
this->installEventFilter (parent);
target = QRectF(0.0, 0.0, button_width, button_height);
source = QRectF(0.0, 0.0, button_width, button_height);}
Button::~Button ()
{}void Button::enterEvent (QEvent *e)
{
m_hovered = true;
this->repaint();
QPushButton::enterEvent(e);
}void Button::leaveEvent (QEvent *e)
{
m_hovered = false;
this->repaint();
QPushButton::leaveEvent(e);
}void Button::release ()
{
flag = false;
qDebug ()<<"FLag"<<flag;
}void Button::press ()
{
flag = false;
qDebug ()<<"FLag"<<flag;
}void Button::mousePressEvent (QMouseEvent *e)
{qDebug ()<<"button mouse press event"; if(e->button() == Qt::LeftButton) { m_pressed = true; m_hovered = false; if(num_of_states == 2) { press_count++; } source = QRectF((button_width) * 2.0, 0.0, button_width, button_height); if(num_of_images == 4) { count++; source = QRectF(0.0, 0.0, button_width, button_height); } } this->repaint();
}
void Button::mouseReleaseEvent (QMouseEvent *e)
{
if(flag == true) {
qDebug ()<<"button mouse release event"<<flag;
if (num_of_states == 1) {m_pressed = false; m_hovered = true; source = QRectF(button_width, 0, button_width, button_height); if(num_of_images == 4 && count % 2 != 0) { source =QRectF(button_width * 3, 0.0, button_width, button_height); } this->repaint(); } else { if (press_count % 2 == 0) { source = QRectF(0.0, 0.0, button_width, button_height); this->repaint(); } } } QPushButton::mouseReleaseEvent(e);
}
void Button::paintEvent (QPaintEvent *e)
{
Q_UNUSED(e);QPainter painter(this); painter.setRenderHint(QPainter::HighQualityAntialiasing); this->resize(pixmap.width(), pixmap.height());
.....................
................
..................QRegion region(0,0,0,0); QImage image = pixmap.toImage(); QRgb rgb; int left = 0; int height = image.height(); this->setMask(region); painter.drawPixmap(target, pixmap, source);
..........
}.
}
...........@
-
I have not read all your code yet, but at line 6 there is already a big mistake. Do not inherit from both QWidget and QObject. QWidget is already inheriting from QObject.
Edit:
Your Button::mousePressEvent is missing a call to QPushButton::mousePressEvent, just like Button::mouseReleaseEventThat is: exactly what I thought above.
-
Thank's a lot for the suggestion. Now it is working. Can you explain the reason behind it.
I have another issue-
Do you know how we can control mouse press event and release event from the main function. I want that when i click a button a new window should open and as long as the window is open the button should be in pressed state and when i close the window the button should be in released state. For each state of button i am using different image. So how i should change the state of button or image of button to released state when the window is closed. -
[quote author="pratik041" date="1316424817"]Thank's a lot for the suggestion. Now it is working. Can you explain the reason behind it.[/quote]
Your implementation overrides the implementation of the event handler in the QPushButton (and underlying classes, even). The code there is responsible for actually sending the signal (and other things, like making your button appear different when pressed). By calling the base class implementation, you give that code the chance to run properly.[quote]
I have another issue-
Do you know how we can control mouse press event and release event from the main function. I want that when i click a button a new window should open and as long as the window is open the button should be in pressed state and when i close the window the button should be in released state. For each state of button i am using different image. So how i should change the state of button or image of button to released state when the window is closed. [/quote]
You already asked that question elsewhere on this forum.