I create a main window, which opens a new window on a button click. That works fine, but the new window's button doesn't respond to clicks. I have included Q_OBJECT in the class declaration. The code is as follows:

main.cpp:
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3. #include "AnotherWindow.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. MainWindow w;
  9. w.show();
  10.  
  11. return a.exec();
  12. }
To copy to clipboard, switch view to plain text mode 

mainwindow.h:
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QObject>
  5. #include <QWidget>
  6.  
  7. class MainWindow : public QWidget
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. explicit MainWindow(QWidget *parent = 0);
  13. ~MainWindow();
  14. private slots:
  15. void TestFunction();
  16. };
  17.  
  18. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

mainwindow.cpp:
Qt Code:
  1. #include <QtGui>
  2. #include "mainwindow.h"
  3. #include "AnotherWindow.h"
  4.  
  5. MainWindow::MainWindow(QWidget *parent)
  6. : QWidget(parent)
  7. {
  8. QPushButton* pb = new QPushButton("Test");
  9. QVBoxLayout* layout = new QVBoxLayout;
  10. layout->addWidget(pb);
  11. this->setLayout(layout);
  12. connect(pb, SIGNAL( clicked() ), this, SLOT( TestFunction() ));
  13. }
  14.  
  15. void MainWindow::TestFunction()
  16. {
  17. qDebug("This is a test");
  18. AnotherWindow aw;
  19. }
  20.  
  21. MainWindow::~MainWindow()
  22. {
  23. }
To copy to clipboard, switch view to plain text mode 

AnotherWindow.h:
Qt Code:
  1. #ifndef ANOTHERWINDOW_H
  2. #define ANOTHERWINDOW_H
  3.  
  4. #include <QObject>
  5. #include <QWidget>
  6.  
  7. class AnotherWindow : public QWidget
  8. {
  9.  
  10. Q_OBJECT
  11.  
  12. public:
  13. AnotherWindow(QWidget* parent = 0);
  14. private slots:
  15. void AnotherTest();
  16. }
  17. ;
  18.  
  19. #endif // ANOTHERWINDOW_H
To copy to clipboard, switch view to plain text mode 

AnotherWindow.cpp:
Qt Code:
  1. #include <QtGui>
  2. #include <QDebug>
  3. #include "AnotherWindow.h"
  4.  
  5. AnotherWindow::AnotherWindow(QWidget* parent)
  6. {
  7. QWidget* window = new QWidget;
  8. QVBoxLayout* anotherLayout = new QVBoxLayout;
  9. QPushButton* pushy = new QPushButton("OMGWTFBBQ");
  10. anotherLayout->addWidget(pushy);
  11. connect(pushy, SIGNAL(clicked()), this, SLOT(AnotherTest()));
  12. window->setLayout(anotherLayout);
  13. window->show();
  14. }
  15.  
  16. void AnotherWindow::AnotherTest()
  17. {
  18. qDebug("Why isn't this code hit?");
  19. }
To copy to clipboard, switch view to plain text mode