代码之家  ›  专栏  ›  技术社区  ›  Tamás Szelei

Qt信号遗传?

  •  3
  • Tamás Szelei  · 技术社区  · 14 年前

    编辑

    我是亚类的 QPushButton 添加一些功能,但是在连接之后 clicked

    /// --- 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;
    }
    

    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
    }
    

    这里可能有什么问题?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Patrick D'Souza ob1    11 年前

    你不小心打电话来了 QPushButton::mousePressEvent(e) MouseRelease3vent 方法。

        2
  •  0
  •   Tamás Szelei    14 年前

    emit clicked(); MouseReleaseEvent中的行。

    void ImageButton::mouseReleaseEvent(QMouseEvent *e)
    {
        QPushButton::mousePressEvent(e);
        state = MouseOver;
        emit clicked();
        this->repaint();
    }
    

    我还是不知道为什么给超级班级的mousePressEvent打电话没能解决这个问题。不管怎样,它是有效的。