Results 1 to 5 of 5

Thread: Signals and Slots Problem

  1. #1
    Join Date
    Feb 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

    Qt Code:
    1. #ifndef MAINTABS_H
    2. #define MAINTABS_H
    3.  
    4. #include "pagewidget.h"
    5.  
    6. #include "ui_maintabs.h"
    7.  
    8. class MainTabs : public QTabWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainTabs(QWidget *parent = 0);
    14. void addPage(PageWidget *page, const char *text);
    15.  
    16. public slots:
    17. void removePage(PageWidget *page);
    18.  
    19. private:
    20. Ui::MainTabs ui;
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    maintabs.cpp

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "maintabs.h"
    4.  
    5. MainTabs::MainTabs(QWidget *parent)
    6. : QTabWidget(parent)
    7. {
    8. ui.setupUi(this);
    9. }
    10.  
    11. void MainTabs::addPage(PageWidget *page, const char *text)
    12. {
    13. connect(page, SIGNAL(pageClosed(PageWidget)), this, SLOT(removePage(PageWidget)));
    14. this->addTab(page, QString());
    15. this->setTabText(this->indexOf(page), QApplication::translate("MainWindow", text, 0, QApplication::UnicodeUTF8));
    16. }
    17.  
    18. void MainTabs::removePage(PageWidget *page)
    19. {
    20. this->removeTab(this->indexOf(page));
    21. }
    To copy to clipboard, switch view to plain text mode 

    pagewidget.h

    Qt Code:
    1. #ifndef PAGEWIDGET_H
    2. #define PAGEWIDGET_H
    3.  
    4. #include "ui_pagewidget.h"
    5.  
    6. class PageWidget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. PageWidget();
    12. void setPage(QWidget* page);
    13.  
    14. public slots:
    15. void closeClicked();
    16.  
    17. signals:
    18. void pageClosed(PageWidget*);
    19.  
    20. private:
    21. Ui::PageWidget ui;
    22. };
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    pagewidget.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "pagewidget.h"
    4.  
    5. PageWidget::PageWidget()
    6. : QWidget()
    7. {
    8. ui.setupUi(this);
    9. }
    10.  
    11. void PageWidget::setPage(QWidget* page)
    12. {
    13. ui.verticalLayout->removeWidget(ui.mainWidget);
    14. ui.verticalLayout->removeWidget(ui.buttonBox);
    15. ui.verticalLayout->addWidget(page);
    16. ui.verticalLayout->addWidget(ui.buttonBox);
    17.  
    18. connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(closeClicked()));
    19. }
    20.  
    21. void PageWidget::closeClicked()
    22. {
    23. emit pageClosed(this);
    24. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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 "*".
    Qt Code:
    1. connect(page, SIGNAL(pageClosed(PageWidget*)), SLOT(removePage(PageWidget*)));
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to caduel for this useful post:

    GenericProdigy (2nd February 2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Signals and Slots Problem (Solved)

    Well that was simple. There were no errors interestingly enough, but your solution worked perfectly. Appreciated.

    P.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Signals and Slots Problem (Solved)

    Quote Originally Posted by GenericProdigy View Post
    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.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    GenericProdigy (2nd February 2009)

  7. #5
    Join Date
    Feb 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

Similar Threads

  1. Problem with signals & slots
    By Palmik in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2009, 22:45
  2. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  3. Signals And Slots problem
    By ldiamond in forum Newbie
    Replies: 7
    Last Post: 23rd March 2008, 00:11
  4. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39
  5. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.