EDIT: I provided an answer but I'd be happy to accept another one that provides an explanation.
I subclassed QPushButton
to add some functionality, but after connection the clicked
signal to a slot, it doesn't get called. If I use a vanilla QPushButton, and don't change anything else, it works. Here's the code:
/// --- imagebutton.h
#ifndef IMAGEBUTTON_H
#define IMAGEBUTTON_H
#include <QPushButton>
#include <QImage>
enum ButtonState
{
Normal,
MouseOver,
Pushed
};
class ImageButton : public QPushButton
{
Q_OBJECT
private:
ButtonState state;
public:
QImage *NormalImage;
QImage *MouseOverImage;
QImage *PushedImage;
public:
explicit ImageButton(QWidget *parent = 0);
virtual ~ImageButton();
void enterEvent(QEvent *e);
void leaveEvent(QEvent *e);
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void paintEvent(QPaintEvent *e);
signals:
public slots:
};
#endif // IMAGEBUTTON_H
/// --- imagebutton.cpp
#include <QPainter>
#include "imagebutton.h"
ImageButton::ImageButton(QWidget *parent) :
QPushButton(parent)
{
state = Normal;
}
void ImageButton::enterEvent(QEvent *e)
{
QPushButton::enterEvent(e);
state = MouseOver;
this->repaint();
}
void ImageButton::leaveEvent(QEvent *e)
{
QPushButton::leaveEvent(e);
state = Normal;
this->repaint();
}
void ImageButton::mousePressEvent(QMouseEvent *e)
{
QPushButton::mousePressEvent(e);
state = Pushed;
this->repaint();
}
void ImageButton::mouseReleaseEvent(QMouseEvent *e)
{
QPushButton::mousePressEvent(e);
state = MouseOver;
this->repaint();
}
void ImageButton::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QImage *pic = NULL;
switch (state)
{
case Normal:
pic = NormalImage;
break;
case MouseOver:
pic = MouseOverImage;
break;
case Pushed:
pic = PushedImage;
break;
default:
pic = NormalImage;
break;
}
painter.drawImage(0, 0, *pic);
}
ImageButton::~ImageButton()
{
delete NormalImage;
delete MouseOverImage;
delete PushedImage;
}
And this is how I connect the signal:
void MainWindow::initInterface()
{
ImageButton *btn_start = new ImageButton(ui->page);
btn_start->setText("start");
connect开发者_开发知识库(btn_start, SIGNAL(clicked()), this, SLOT(btn_clicked()));
// more buttons connected to the same slot
}
What might be the problem here?
You are calling accidentally QPushButton::mousePressEvent(e)
in your overriden MouseRelease3vent
method.
I solved it finally. The solution was to put an emit clicked();
line in the MouseReleaseEvent.
void ImageButton::mouseReleaseEvent(QMouseEvent *e)
{
QPushButton::mousePressEvent(e);
state = MouseOver;
emit clicked();
this->repaint();
}
I still don't know why the call to the superclass' mousePressEvent didn't take care of that. Anyway, it works.
精彩评论