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.
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.