PDA

View Full Version : Signals and Slots Problem



GenericProdigy
1st February 2009, 17:52
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


#ifndef MAINTABS_H
#define MAINTABS_H

#include "pagewidget.h"

#include "ui_maintabs.h"

class MainTabs : public QTabWidget
{
Q_OBJECT

public:
MainTabs(QWidget *parent = 0);
void addPage(PageWidget *page, const char *text);

public slots:
void removePage(PageWidget *page);

private:
Ui::MainTabs ui;
};

#endif

maintabs.cpp


#include <QtGui>

#include "maintabs.h"

MainTabs::MainTabs(QWidget *parent)
: QTabWidget(parent)
{
ui.setupUi(this);
}

void MainTabs::addPage(PageWidget *page, const char *text)
{
connect(page, SIGNAL(pageClosed(PageWidget)), this, SLOT(removePage(PageWidget)));
this->addTab(page, QString());
this->setTabText(this->indexOf(page), QApplication::translate("MainWindow", text, 0, QApplication::UnicodeUTF8));
}

void MainTabs::removePage(PageWidget *page)
{
this->removeTab(this->indexOf(page));
}

pagewidget.h


#ifndef PAGEWIDGET_H
#define PAGEWIDGET_H

#include "ui_pagewidget.h"

class PageWidget : public QWidget
{
Q_OBJECT

public:
PageWidget();
void setPage(QWidget* page);

public slots:
void closeClicked();

signals:
void pageClosed(PageWidget*);

private:
Ui::PageWidget ui;
};

#endif

pagewidget.cpp


#include <QtGui>

#include "pagewidget.h"

PageWidget::PageWidget()
: QWidget()
{
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.

caduel
1st February 2009, 19:07
(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 "*".

connect(page, SIGNAL(pageClosed(PageWidget*)), SLOT(removePage(PageWidget*)));

GenericProdigy
1st February 2009, 19:40
Well that was simple. There were no errors interestingly enough, but your solution worked perfectly. Appreciated.

P.

jpn
1st February 2009, 19:50
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.

GenericProdigy
2nd February 2009, 09:06
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.