How to call a slot of button class when a window is closed ?



  • I have a window class and a child button class. I am making custom button.I am using mouse release and press event in button class.
    I am changing button image at each mouse_hover and press or release event.

    I want when i press a button a new window should open and as long as the window is opened the button should be in pressed state
    and when i close that window, button should change to released state. I can't use checkable state property because my button is custom type. So how i will know the window is closed or not and how to change the button state at the same time.
    My code is pasted below please tell me the change i have to do in that

    main.cpp
    @#include <QtGui/QApplication>
    #include "button.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QMainWindow window;
    QMainWindow window1;

    float button_width  = 47;
    float button_height = 47;
    int   button_states = 1;
    
    //window.setStyleSheet("background-image: url(:/Sunset.jpg);");
    
    QPixmap pixmap(":/endcall.png");    
    Button *button1  = new Button(button_width, button_height, button_states, pixmap, &window);
    button1->setGeometry(60, 70, 80, 90);
    

    QObject::connect(button1,SIGNAL(clicked()),window1,SLOT(open()));

    window.show();
    
    return a.exec&#40;&#41;;
    

    }

    button.cpp

    #include "mainwindow.h"

    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;

    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::mousePressEvent (QMouseEvent *e)
    {
    ....................

    ...........................
    this->repaint();
    QPushButton::mousePressEvent(e);
    }

    void Button::mouseReleaseEvent (QMouseEvent *e)
    {.................

    .........................

            this->repaint();
        
    QPushButton::mouseReleaseEvent(e);
    

    }

    void Button::paintEvent (QPaintEvent *e)
    {
    Q_UNUSED(e);

    .............

    .......................
    this->setMask(region);
    painter.drawPixmap(target, pixmap, source);

    switch (num_of_states) {
    
    case 1:
    
        if (this->isEnabled()) {
    
            if(num_of_images == 4) {
    
                painter.drawPixmap(target, pixmap, source);
    
            } else {
    
                if(!m_pressed) {
    
                    source = QRectF(0.0, 0.0, button_width, button_height);
                }
    
                painter.drawPixmap(target, pixmap, source);
    
                if (m_hovered) {
                    source = QRectF(button_width, 0.0 ,button_width, button_height);
                    painter.drawPixmap(target, pixmap, source);
    
                }
            }
    
        } else {
    
            painter.drawPixmap(target, pixmap, source);
        }
    
        break;
    
    
    case 2:
    
        if (this->isEnabled()) {
    
            if (press_count % 2 != 0 || press_count == 0) {
                if(!m_pressed) {
                    source = QRectF(0, 0, button_width, button_height);
               }
    
            } else {
                if(m_pressed) {
                  source = QRectF(0, 0 , button_width, button_height);
                }
            }
    
            painter.drawPixmap(target,pixmap,source);
    
            if (press_count % 2 != 0 || press_count == 0) {
                if (m_hovered && m_pressed == false) {
                    source = QRectF(button_width , 0, button_width, button_height);
    
                }
            } else {
                if (m_hovered && m_pressed == true){
                    source = QRectF(button_width , 0, button_width, button_height);
                }
            }
            painter.drawPixmap(target, pixmap, source);
    
            break;
        }
    }
    

    }

    button.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui>

    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);
    
    void check_condition(int,QPainter &);
    
    ~Button();
    
    void enterEvent(QEvent *);
    
    void leaveEvent(QEvent *);
    
    void mousePressEvent(QMouseEvent *e);
    
    void mouseReleaseEvent(QMouseEvent *e);
    
    void paintEvent(QPaintEvent *);
    

    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;
    
    QRectF target;
    QRectF source;
    
    QPixmap pixmap;
    

    };

    #endif // MAINWINDOW_H

    @



  • Is the second window a modal dialog? If yes, the exec call returns only after the second window has been closed. The first window has blocked user input though.



  • Sorry, but i didn't get the answer. Please specify it in details



  • [[Doc:QDialog]] has the explanations about modal and modeless dialogs. A modal dialog spins its own [[Doc:QEventLoop]], you should know about both as a Qt developer. Maybe using a modal dialog solves your problem.



  • ok i will try that


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.