Qt PushButton Signals and Slots

13,489

QWidget has a public slot called show(). You can connect your button's clicked() signal to your widget's show() slot. Read more about signals and slots here.

Example:

QPushButton *button = new QPushButton(this);
QWidget *widget = new QWidget(this);
widget->setWindowFlags(Qt::Window);
connect(button, SIGNAL(clicked()), widget, SLOT(show()));

You could also create your own slot and call widget->show() from there. Then connect the button's clicked() signal to your slot.

Example:

//myclass.h
...
public:
   QWidget *myWidget;

public slots:
   void mySlot();

 

//myclass.cpp
...
   connect(button, SIGNAL(clicked()), this, SLOT(mySlot()));
...

void MyClass::mySlot()
{
   myWidget->show();
}
Share:
13,489
Connor M
Author by

Connor M

Updated on June 04, 2022

Comments

  • Connor M
    Connor M almost 2 years

    I'm pretty much a beginner at Qt. Anyway, I'm trying to use signals and slots to show a widget once the button is pressed. I created the widget, and have the connect() thing all done, but what do I put in the SLOT() thing? I've tried show(widget), but to be honest I have no clue what to put there.