Why doesn't this button become invisible?
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?
Code:
#ifndef SOUNDBUTTON_H
#define SOUNDBUTTON_H
#include <QPushButton>
{
public:
void click();
void clicked();
};
#endif // SOUNDBUTTON_H
// the cpp file
#include "soundbutton.h"
#include <stdio.h>
SoundButton
::SoundButton(QWidget *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);
}
Re: Why doesn't this button become invisible?
I might not understood you right, you need a button that will hide itself when clicked?
Code:
#ifndef SOUNDBUTTON_H
#define SOUNDBUTTON_H
#include <QPushButton>
{
Q_OBJECT // also don't forget Q_OBJECT macro when declare signals and slots in a class
public:
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
){
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);
}
Re: Why doesn't this button become invisible?
Ha! I finally understand slots and signals. Thanks, Zlatomir :D