Signals and Slots Problem
I'm currently writing a small utility and I've created custom inner-widgets for my main window that will throw their events / call backs back up to the top-level controller. Unfortunately the events do not seem to be working with the signals and slots mechanism.
I suspect I've made some obvious mistake, but having spent an entire day looking at this, I'm at a loss to find any answers. A snippet of code is below:
maintabs.h
Code:
#ifndef MAINTABS_H
#define MAINTABS_H
#include "pagewidget.h"
#include "ui_maintabs.h"
{
Q_OBJECT
public:
void addPage(PageWidget *page, const char *text);
public slots:
void removePage(PageWidget *page);
private:
Ui::MainTabs ui;
};
#endif
maintabs.cpp
Code:
#include <QtGui>
#include "maintabs.h"
MainTabs
::MainTabs(QWidget *parent
){
ui.setupUi(this);
}
void MainTabs::addPage(PageWidget *page, const char *text)
{
connect(page, SIGNAL(pageClosed(PageWidget)), this, SLOT(removePage(PageWidget)));
}
void MainTabs::removePage(PageWidget *page)
{
this->removeTab(this->indexOf(page));
}
pagewidget.h
Code:
#ifndef PAGEWIDGET_H
#define PAGEWIDGET_H
#include "ui_pagewidget.h"
{
Q_OBJECT
public:
PageWidget();
public slots:
void closeClicked();
signals:
void pageClosed(PageWidget*);
private:
Ui::PageWidget ui;
};
#endif
pagewidget.cpp
Code:
#include <QtGui>
#include "pagewidget.h"
PageWidget::PageWidget()
{
ui.setupUi(this);
}
void PageWidget
::setPage(QWidget* page
) {
ui.verticalLayout->removeWidget(ui.mainWidget);
ui.verticalLayout->removeWidget(ui.buttonBox);
ui.verticalLayout->addWidget(page);
ui.verticalLayout->addWidget(ui.buttonBox);
connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(closeClicked()));
}
void PageWidget::closeClicked()
{
emit pageClosed(this);
}
Now the PageWidget is a custom page object that all tab pages will inherit from. The MainTabs is my QTabWidget object. The idea is for each page to have a 'Close' button and when that is clicked an event is shot up to the main controller of the QMainWindow object. Unfortunately, although the PageWidget::closeClicked() method is invoked, the pageClosed(PageWidget*) method does not seem to be getting hit within the MainTabs class.
I hope I've provided enough information, and that someone is inclined to provide me with an answer. I'm assuming it's simply a lack of knowledge on my part of the SLOTS and SIGNALS mechanism within Qt.
And of course, any tips on how to improve my approach would be welcome.
Regards,
P.
Re: Signals and Slots Problem
(remark: you should probably be getting error messages about not found signals/slots on your console.)
fix your signal/slots in the connect statement to contain the "*".
Code:
connect(page, SIGNAL(pageClosed(PageWidget*)), SLOT(removePage(PageWidget*)));
Signals and Slots Problem (Solved)
Well that was simple. There were no errors interestingly enough, but your solution worked perfectly. Appreciated.
P.
Re: Signals and Slots Problem (Solved)
Quote:
Originally Posted by
GenericProdigy
There were no errors interestingly enough
Signal-slot connections are established at run time, not at compile time. Signal and slot signatures are just strings for compliler, thus no errors. However, as caduel already pointed out, QObject::connect() will output a detailed warning when a connection fails.
Re: Signals and Slots Problem
Stepping through the code I saw nothing. Perhaps I missed something. It looked as though the connect worked, but nothing recieved it, but I'm using a new IDE - perhaps it was hidden somewhere.