PDA

View Full Version : Why doesn't this button become invisible?



Plixil
23rd July 2010, 23:55
I want a button to become invisible when I click on it. I'm inheriting from QPushButton and then promoting. I know the button is being created properly because when I put the setVisible part into the constructor, it works. It just doesn't work when I click on the button when the program is running, despite my attempt to override the QPushButton functions of click() or clicked(). What am I doing wrong here? How can I get this to work?



#ifndef SOUNDBUTTON_H
#define SOUNDBUTTON_H

#include <QPushButton>

class SoundButton : public QPushButton
{
public:
SoundButton(QWidget *parent = 0);

void click();
void clicked();
};

#endif // SOUNDBUTTON_H

// the cpp file

#include "soundbutton.h"
#include <stdio.h>

SoundButton::SoundButton(QWidget *parent)
: QPushButton(parent)
{
printf("test me"); //doesn't output but not related to the problem
}

void SoundButton::click()
{
this->setVisible(false);
}

void SoundButton::clicked()
{
this->setVisible(false);
}

Zlatomir
24th July 2010, 00:05
I might not understood you right, you need a button that will hide itself when clicked?


#ifndef SOUNDBUTTON_H
#define SOUNDBUTTON_H

#include <QPushButton>

class SoundButton : public QPushButton
{
Q_OBJECT // also don't forget Q_OBJECT macro when declare signals and slots in a class
public:
SoundButton(QWidget *parent = 0);
public slots: // declare the slots of your class
void SoundButtonHide(); //declare a slot that will hide the object,
};

#endif // SOUNDBUTTON_H

// the cpp file

#include "soundbutton.h"
#include <stdio.h>

SoundButton::SoundButton(QWidget *parent)
: QPushButton(parent)
{
printf("test me"); //doesn't output but not related to the problem //this doesn't have where to print, because you are not making a console application
connect(this, SIGNAL(clicked()), this, SLOT(SoundButtonHide())); //connect the clicked signal with the slot that will hide the button, are you sure that you need the button to hide itself?

}

void SoundButton::SoundButtonHide(){
this->setVisible(false);
}

Plixil
24th July 2010, 00:41
Ha! I finally understand slots and signals. Thanks, Zlatomir :D