Results 1 to 15 of 15

Thread: connect() problem

  1. #1
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default connect() problem

    Hello you all!
    I'm a newbie in Qt programming so my question will probably seem a stupid question but all guides I read didn't help me...
    I'm developing a C/C++ program in Win Vista & Qt Creator IDE with the Qt framework. It is a Qt GUI Application (the one you can create by clicking New -> project)
    I'd like to connect the event of clicking a PushButton with a function I wrote earlier. Here is the code of my mainwindow.cpp file:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPushButton>
    4. #include<QMessageBox>
    5. class act: public QObject
    6. {
    7. public slots:
    8. void buttonClicked()
    9. {
    10. QMessageBox("Title","Msg",QMessageBox::NoIcon,QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
    11. }
    12. };
    13. MainWindow::MainWindow(QWidget *parent) :
    14. QMainWindow(parent),
    15. ui(new Ui::MainWindow)
    16. {
    17. QPushButton *PB= new QPushButton(this);
    18. PB->setText("Button text");
    19. act a;
    20. connect(PB, SIGNAL(clicked()), &a,SLOT(buttonClicked()));
    21. PB->show();
    22. }
    23. MainWindow::~MainWindow()
    24. {
    25. delete ui;
    26. }
    To copy to clipboard, switch view to plain text mode 

    It is executed correctly but when I push the button the message doesn't show!

    What could be the problem?

    Thanks a lot, harmodrew

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    In your constructor you create a local variable which is destroyed when the constructor has finished.

    Why not use the reference you already have?

    Secondly, the class declaration should be in a seperate header file to the definition so it can be easily compiled by the meta object compiler.

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

    harmodrew (4th August 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    Why not use the reference you already have?
    What reference?

  5. #4
    Join Date
    Apr 2010
    Location
    Porto Alegre, RS, Brazil
    Posts
    37
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    To use Message Box, use the following example, for instance
    Qt Code:
    1. QMessageBox msgBox;
    2. msgBox.setText("The document has been modified.");
    3. msgBox.exec();
    To copy to clipboard, switch view to plain text mode 

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

    harmodrew (4th August 2010)

  7. #5
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    searching around the Net I've found that all signal/slot classes must include in the private section the macro Q_OBJECT.
    Now the problem is that if I include this macro I have the error:
    "collect2: Id returned 1 exit status"
    and clicking the message of the error it doesn't bring me to the row affected...

  8. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    Quote Originally Posted by harmodrew View Post
    What reference?
    Qt Code:
    1. act a;
    2. connect(PB, SIGNAL(clicked()), &a,SLOT(buttonClicked()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. connect(PB, SIGNAL(clicked()), this,SLOT(buttonClicked()));
    To copy to clipboard, switch view to plain text mode 

    Secondly, when you are finished, choose "run qmake" in QtCreator before trying to run. This will generate the rules to run MOC.

  9. #7
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    So, here are my project files (I've split the class "actionObj" from the mainwindow file).

    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPushButton>
    4. #include "actions.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. QPushButton *NewButton= new QPushButton(this);
    11. NewButton->setText("Button");
    12.  
    13. actionsObj *ao;
    14. ao = new actionsObj;
    15. connect(NewButton, SIGNAL(clicked()), this,SLOT(ButtonClicked()));
    16. NewButton->setGeometry(50, 40, 300, 200);
    17. NewButton->show();
    18. // ui->setupUi(this); if I uncomment this, the button isn't clickable!
    19.  
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    To copy to clipboard, switch view to plain text mode 

    actions.cpp:
    Qt Code:
    1. #include "actions.h"
    2. #include <QMessageBox>
    3.  
    4. void actionsObj ::ButtonClicked()
    5. {
    6. msg.setText("Hello. You've clicked the button");
    7. msg.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    actions.h:
    Qt Code:
    1. #ifndef ACTIONS_H
    2. #define ACTIONS_H
    3. #include <qobject.h>
    4.  
    5. class actionsObj : public QObject
    6. {
    7. private:
    8. Q_OBJECT
    9. public slots:
    10. void ButtonClicked();
    11. };
    12.  
    13. #endif // ACTIONS_H
    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 <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

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

    But now I have the following error:
    collect2: Id returned 1 exit status
    and the IDE doesn't bring me to the line affected...what could be the problem? it's possible that a simple program like this, even if I'm a starter, is so complicated and full of problems??

    Thanks a lot
    Last edited by harmodrew; 5th August 2010 at 08:49.

  10. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect() problem

    Qt Code:
    1. connect(NewButton, SIGNAL(clicked()), this,SLOT(ButtonClicked()));
    To copy to clipboard, switch view to plain text mode 
    .
    "this" is wrong because it means your main window, but your slot is in your actionsObj. Also you are producing memory leaks! (ao = new actionsObj

  11. #9
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    I've changed the code and now it works!
    Qt Code:
    1. QPushButton *NewButton= new QPushButton(this);
    2. NewButton->setText("Button");
    3.  
    4. actionsObj *ao= new actionsObj;
    5. connect(NewButton, SIGNAL(clicked()),ao, SLOT(ButtonClicked()));
    6. NewButton->setGeometry(50, 40, 300, 200);
    7. NewButton->show();
    8. //ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 

    the only problem now is that if I uncomment the last row the button isn't clickable anymore. What could be the problem?

  12. #10
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    ok, I've done it!
    simply, the ui->setupUi(this); row is to be written before the other rows, not in the bottom
    thanks you all

  13. #11
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    here I am again for the same problem...
    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QObject>
    4.  
    5. class act :public QObject
    6. {
    7. Q_OBJECT
    8.  
    9. };
    10.  
    11. MainWindow::MainWindow(QWidget *parent) :
    12. QMainWindow(parent),
    13. ui(new Ui::MainWindow)
    14. {
    15. ui->setupUi(this);
    16.  
    17. act *ao= new act;
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    To copy to clipboard, switch view to plain text mode 

    this little code returns me the same error:
    collect2: Id returned 1 exit status
    what it refers to??
    in addition if I comment the Q_OBJECT line it compiles without errors!
    Last edited by harmodrew; 5th August 2010 at 15:16.

  14. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: connect() problem

    put your class act in a single file or make sure moc runing right for your class. Simpliest is every class a single file!

  15. The following user says thank you to Lykurg for this useful post:

    harmodrew (5th August 2010)

  16. #13
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    thanks for the reply, it helped! but, why are we obliged to write the class in an another file to make it work? what does it change?

  17. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    Its to do with the meta object compiler. You can have classes in the same file as your .cpp if you want, but if they include the Q_OBJECT macro, then you need to run MOC, so ideally those classes should be in .h files instead.

  18. #15
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect() problem

    oh, it's clear now! thank again!

Similar Threads

  1. PyQT connect problem
    By di_zou in forum Newbie
    Replies: 1
    Last Post: 6th November 2009, 17:03
  2. Connect Problem
    By nrabara in forum Newbie
    Replies: 3
    Last Post: 4th May 2009, 12:19
  3. connect problem
    By liengen in forum Newbie
    Replies: 2
    Last Post: 22nd October 2008, 16:21
  4. Problem with connect()
    By mrnor3 in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2008, 14:05
  5. connect problem QDialog
    By Zergi in forum Newbie
    Replies: 1
    Last Post: 1st January 2008, 13:36

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.