Link to home
Create AccountLog in
Avatar of techbie
techbie

asked on

Hide a button on click event : Qt

Hi experts,

I was developing a graphics application in C++ Qt.  I need to create a screen with an image button which will disappear when I click on it.

I wrote the code. Button is shown in the screen, but the click event doesnt hide the button. Can any one have a look on it and see where I am making the mistake.

Also I tried to load an image icon, but the image is not being shown. Is my syntax correct.

I am using qt 4.0

thanks very much
#include <QApplication>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtGui>
#include <QDialog>
 
int main( int argc, char* argv[] )
{
 
	QApplication app(argc, argv);
 
    QHBoxLayout *mainLayout = new QHBoxLayout;
	QVBoxLayout *layout = new QVBoxLayout;
	
	QWidget *window = new QWidget;
	
	QPushButton *p1_b1 = new QPushButton();
	p1_b1->setObjectName(QString::fromUtf8("pushButton"));
    p1_b1->setGeometry(QRect(100, 50, 146, 108));
    p1_b1->setIcon(QIcon(QString::fromUtf8("../Documents/projects/qtprojects/club_five.jpg")));
    p1_b1->setIconSize(QSize(100, 80));
 
	QObject::connect(p1_b1, SIGNAL(clicked()),&app, SLOT(hideButton(p1_b1)));
	
	layout->addWidget(p1_b1);
	
	mainLayout->addLayout(layout);
	 
	 //Now adding all the layouts to the window object
	window->setLayout(mainLayout);
	window->setWindowTitle("My window title");
    window->setFixedHeight(200);
	
	window->show();
	
    return app.exec();
	
}
 
void hideButton(QPushButton *b)
{
b->hide();
}

Open in new window

Avatar of jkr
jkr
Flag of Germany image

Have you tried the following instead?
void hideButton(QPushButton *b)
{
  b->close();
}

Open in new window

Avatar of techbie
techbie

ASKER

I tried that now.. doesnt seem to work...
ASKER CERTIFIED SOLUTION
Avatar of ikework
ikework
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
have a look at this:

http://doc.trolltech.com/3.3/signalsandslots.html

particulary how the slot Foo::setValue is declared

dont forget to run "moc" on that header ..
Avatar of techbie

ASKER

this might be the problem...let me read the tutorial and try to implement it...will get back on my results

thanks
Avatar of techbie

ASKER

Hi....I worked on it and solved the problem...the problem was that the hideButton was not declared
as a slot. So the signal was not able to recognize...i was compiling the application using xcode
and it itself created me the moc files..

now the program runs fine and the hide option is enabled..

thanks very much
great .. you're welcome :)