PDA

View Full Version : problem with slot



as001622
23rd May 2008, 09:23
Hello!
I've created my own button. But when I try to connect it with SLOT(quit()) this slot dosn't work. Could anybody find a problem?

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QtGui>

class MYButton : public QAbstractButton
{
Q_OBJECT

public:
MYButton(QWidget * parent = 0);
~MYButton();

void setDefaulImageFileName(QString &filename);
void setActionImageFileName(QString &filename);

protected:
void paintEvent(QPaintEvent * pe);
QSize sizeHint();

void enterEvent(QEvent * e);
void leaveEvent(QEvent * e);
void mousePressEvent(QMouseEvent * e);
void mouseReleaseEvent(QMouseEvent * e);

private:
QString defaultImageFileName;
QString actionImageFileName;
QImage *defaultImage;
QImage *actionImage;
bool m_enter;
bool m_leave;
bool m_mousePressEvent;
bool m_mouseReleaseEvent;
};

#endif


#include <QtGui>
#include "MYbutton.h"
static const int buttonWidth=233;
static const int buttonHeight=55;
static const QSize buttonSize(buttonWidth, buttonHeight);



MYButton::MYButton(QWidget *parent) : QAbstractButton(parent)
{
QSize frameSize(buttonWidth+3, buttonHeight+6);
setPalette(QPalette(QColor(0, 0, 0)));
setAutoFillBackground(true);
setMaximumSize(frameSize);
setMinimumSize(frameSize);
m_leave=true;
m_enter=false;
m_mousePressEvent=false;
m_mouseReleaseEvent=false;
}

MYButton::~MYButton()
{
}

void MYButton::paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);
QImage *defaultImage = new QImage;
QImage *actionImage = new QImage;
defaultImage->load(defaultImageFileName);
actionImage->load(actionImageFileName);
QImage *image = new QImage;
if (!m_enter)
{
image=defaultImage;
}
else
{
image=actionImage;
};


QPainter painter(this);
QPointF point;
if(!m_mousePressEvent)
{
point.setX(1);
point.setY(1);
}
else
{
point.setX(3);
point.setY(3);
}

painter.drawImage(point, *image);

}

QSize MYButton::sizeHint()
{
return buttonSize;
}

void MYButton::enterEvent(QEvent *e)
{
m_enter=true;
this->repaint();
}

void MYButton::leaveEvent(QEvent *e)
{
m_enter=false;
m_mousePressEvent=false;
this->repaint();
}

void MYButton::mousePressEvent(QMouseEvent *e)
{
m_mousePressEvent=true;
this->repaint();
}

void MYButton::mouseReleaseEvent(QMouseEvent *e)
{
m_mousePressEvent=false;
this->repaint();
}

void MYButton::setActionImageFileName(QString &filename)
{
actionImageFileName=filename;
}

void MYButton::setDefaulImageFileName(QString &filename)
{
defaultImageFileName=filename;
}


#include <QtGui/QApplication>
#include <QtGui/QVBoxLayout>
#include "MYbutton.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MYButton *button1=new MYButton;
button1->setDefaulImageFileName(QString("images/default_button.png"));
button1->setActionImageFileName(QString("images/action_button.png"));
QObject::connect(button1,SIGNAL(clicked()),&a, SLOT(quit()));
button1->show();
return a.exec();
}

wysota
23rd May 2008, 10:29
You have to call the base class implementation of mouse events.

as001622
23rd May 2008, 11:43
Thanks, but could you explain me where I have to do this, please?

jpn
23rd May 2008, 12:05
void SubClass::function()
{
BaseClass::function();
...
}

santhoshv84
24th May 2008, 08:02
Hi,

I think you must set setMouseTracking(true).
Try this.

Thanks and regards.
V. Santhosh