I have a main window class which contains a QSplitter widget (among other widgets). The contents of this splitter are populated by the widgets that are in another class.
Inside this other Widgwt I have a layout with a QPushButton called test_btn added I also have a function defined in the header file under public slots:
called test_function which has the corresponding code in the cpp file:
cout << "Test!" << endl;
I try and call this function using the following code:
connect(test_btn, SIGNAL(clicked()), SLOT(test_function()));
The widgets and buttons appear as expected in the application but when I click it nothing happens.
If I add the same connect
code to the main window it works (for calling a test function from the main window) i.e.
connect(cc_point.test_btn, SIGNAL(clicked()), SLOT(main_win_test_function()));
I cant call the test function from the other class unless I call it from the main_win_test_function()
I know I'm missing the 'receiver widget' from the connect statement, although it does work without it in the main window class and I don't get any errors when compiling.
Do I need to define the parent or something in the other_class
?
main_window.cpp:
main_window::main_window()
{
add_splitter();
QGridLayout *window_layout = new QGridLayout;
window_layout->setSpacing(0);
window_layout->setContentsMargins(0, 0, 0, 0);
window_layout->addWidget(splitter1, 0, 0);
set_layout(window_layout);
}
void main_window::add_splitter()
{
other_class oc_point;
splitter1 = new QSplitter(Qt::Horizontal);
splitter1->addWidget(oc_point.oc_widget);
}
other_class.cpp:
other_class:other_class()
{
oc_widget = new QWidget;
QGridLayout *other_layout = new QGridLayout(oc_widget);
test_btn = new QPushButton;
test_btn->setText("Test Button");
connect(test_btn, SIGNAL(clicked()), test_btn->parent(), SLOT(test_function());
other_layout->addWidget(test_btn);
}
void other_class::test_function()
{
cout << "Test output" << endl;
}
main.cpp:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
main_window window;
window.resize(1100, 700);
window.setWindowTitle("Qt Test");
window.setWindowIcon(QIcon (":/images/icon_1.png"));
window.setMinimumHeight(500);
window.show();
return app.exec();
}
As I mentioned above, this all works fine as far as the window getting created and the widgets being displayed goes, but the test_function doesn't get called when I click the button. Let me know if I need to include my header files as well.
After your objects are initialized, you should connected them to each other. Explicitly naming whose slots are whose and whose signals are whose, will help a lot.
Sometimes I'll make a helper function
void Widget::connectTo__Class__(Class * class_ptr);
Then in that function call I'll connect the slots that cross the class/parent/inheritance boundaries.
Here is an example implementation:
Also keep in mind that the runtime will complain when you are doing something wrong, such as if the
class_ptr
isnull
, or ifmyClassSignal
doesn't exist. You can see these errors in the application output when you run it in Debug mode.And give
QDebug
a try sometime.http://qt-project.org/doc/qt-4.8/debug.html
EDIT: To fix any header class recursive definition problems, do the following:
In your header file for one of the classes only prototype the class, don't include it:
in widget.h:
Then in the cpp file, you do the include.
Another way around this issue is to use
QObject *
instead ofClass *
.Hope that helps.