Results 1 to 3 of 3

Thread: Slot doesn't work in new window

  1. #1
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Slot doesn't work in new window

    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 

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slot doesn't work in new window

    Here's one (and possibly the) problem:

    Qt Code:
    1. void MainWindow::TestFunction()
    2. {
    3. qDebug("This is a test");
    4. AnotherWindow aw;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Here you instantiate an AnotherWindow on the stack. What happens here is that the window you create gets destroyed immediately after construction. The push button's clicked() signal automagically detects that the connection was broken, hence there is no activity. The widget you see is the widget created in the AnotherWindow constructor (which is superfluous, btw).

    Better code would probably be:
    Qt Code:
    1. void MainWindow::TestFunction()
    2. {
    3. qDebug("This is a test");
    4. AnotherWindow *aw = new AnotherWindow(this);
    5. aw->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. AnotherWindow::AnotherWindow(QWidget* parent)
    2. {
    3. QVBoxLayout* anotherLayout = new QVBoxLayout;
    4. QPushButton* pushy = new QPushButton("OMGWTFBBQ");
    5. anotherLayout->addWidget(pushy);
    6. connect(pushy, SIGNAL(clicked()), this, SLOT(AnotherTest()));
    7. setLayout(anotherLayout);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Hope this helps.

    On an unrelated note, you don't need to explicitly include QObject before you include QWidget. QWidget is a QObject and the dependency implications are handled for you by the compiler. It won't make much of a difference during compiling, it is more of a style issue.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    mohjlir (27th January 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slot doesn't work in new window

    Perfect, thanks for your help!

Similar Threads

  1. QNetworkAccessManager doesn't work
    By Floppy in forum Newbie
    Replies: 7
    Last Post: 14th November 2009, 16:32
  2. Slot doesn't seem to work
    By waynew in forum Newbie
    Replies: 3
    Last Post: 10th November 2009, 09:54
  3. Shortcut doesn't work
    By stella1016 in forum Qt Programming
    Replies: 1
    Last Post: 28th May 2009, 02:37
  4. copy() slot doesn't work
    By Macok in forum Qt Programming
    Replies: 3
    Last Post: 14th February 2009, 10:55
  5. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 07:49

Tags for this Thread

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.