Reputation: 644
I am using Qt Designer for the design of my application which contains a "Load File" QPushButton
(which has been promoted to a custom loadfile.h
) as well as an QOpenGLWidget (which has been promoted to a custom OGLWidget.h
).
I wanted to have it so that when I click my "Load File" button it sends a signal to a slot in my OGLWidget
class which brings up a dialogue and loads the file.
I started by going into signal/slot edit mode in Qt Designer and connected the button to the openglwidget
with clicked()
as the signal and a newly added (from within Qt Designer) populate_mesh_from_obj()
.
I then made a function:
void OGLWidget::populate_mesh_from_obj()
{
exit(0);
}
as a test and in the header put:
public slots:
void populate_mesh_from_obj();
Now in my head I thought:
clicked()
is already implemented by Qt magicSo to me this should have worked. Any ideas why it hasn't?
Upvotes: 1
Views: 1468
Reputation: 4008
Fortunately, the accepted answer is incorrect (It's not that it doesn't work manually, but you don't have to do it that way).
In fact, QT will automatically connect the signal to all slots that follow a standard naming convention.
This naming convention for slots goes like this:
void on_<object name>_<signal name>(<signal parameters>);
In your case, this means if you created the QPushButton
in QT Designer like this:
the object name
is loadfile
and the signal name is clicked
.
Now, declare your function like this in the header:
public slots:
void on_loadfile_clicked();
and define it like this in the cpp:
void OGLWidget::on_loadfile_clicked()
{
exit(0);
}
and the connection will be automatically taken care of.
This auto-connection feature is actually implemented through the QT Meta-Object compiler. You can retrace what happens by opening the ui_<classname>.h
file generated by QT Designer. It will contain a function void setupUi(<QClassName> *<YourClassName>)
containing a line QMetaObject::connectSlotsByName(<YourClassName>);
. This is what instructs QTs Meta-Object Compiler to implement the connections for you.
Upvotes: 2
Reputation: 48288
you have to connect them manually..
connect(ui->button, &QPushButton::clicked, yourGlPointer, &OGLWidget::populate_mesh_from_obj);
Upvotes: 1