Qt PushButton Signals and Slots

Connor M picture Connor M · Aug 22, 2013 · Viewed 13.3k times · Source

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.

Answer

thuga picture thuga · Aug 22, 2013

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();
}