PDA

View Full Version : connect() problem



harmodrew
4th August 2010, 17:22
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:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include<QMessageBox>
class act: public QObject
{
public slots:
void buttonClicked()
{
QMessageBox("Title","Msg",QMessageBox::NoIcon,QMessageBox::Ok,QMessageBox:: NoButton,QMessageBox::NoButton);
}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QPushButton *PB= new QPushButton(this);
PB->setText("Button text");
act a;
connect(PB, SIGNAL(clicked()), &a,SLOT(buttonClicked()));
PB->show();
}
MainWindow::~MainWindow()
{
delete ui;
}



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

What could be the problem?

Thanks a lot, harmodrew

squidge
4th August 2010, 17:40
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.

harmodrew
4th August 2010, 17:58
Why not use the reference you already have?

What reference?

leoalvesmachado
4th August 2010, 18:14
To use Message Box, use the following example, for instance
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.exec();

harmodrew
4th August 2010, 18:33
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...

squidge
4th August 2010, 21:00
What reference?



act a;
connect(PB, SIGNAL(clicked()), &a,SLOT(buttonClicked()));




connect(PB, SIGNAL(clicked()), this,SLOT(buttonClicked()));


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

harmodrew
5th August 2010, 08:38
So, here are my project files (I've split the class "actionObj" from the mainwindow file).

mainwindow.cpp:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include "actions.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QPushButton *NewButton= new QPushButton(this);
NewButton->setText("Button");

actionsObj *ao;
ao = new actionsObj;
connect(NewButton, SIGNAL(clicked()), this,SLOT(ButtonClicked()));
NewButton->setGeometry(50, 40, 300, 200);
NewButton->show();
// ui->setupUi(this); if I uncomment this, the button isn't clickable!

}

MainWindow::~MainWindow()
{
delete ui;
}


actions.cpp:


#include "actions.h"
#include <QMessageBox>

void actionsObj ::ButtonClicked()
{
QMessageBox msg;
msg.setText("Hello. You've clicked the button");
msg.exec();
}


actions.h:


#ifndef ACTIONS_H
#define ACTIONS_H
#include <qobject.h>

class actionsObj : public QObject
{
private:
Q_OBJECT
public slots:
void ButtonClicked();
};

#endif // ACTIONS_H



mainwindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


main.cpp:


#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QAction>
#include <QString>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString title="MainWindow";

MainWindow w;
w.setGeometry(100, 100, 500, 500);
w.show();
return a.exec();
}


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

Lykurg
5th August 2010, 09:31
connect(NewButton, SIGNAL(clicked()), this,SLOT(ButtonClicked()));.
"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;)

harmodrew
5th August 2010, 09:41
I've changed the code and now it works!


QPushButton *NewButton= new QPushButton(this);
NewButton->setText("Button");

actionsObj *ao= new actionsObj;
connect(NewButton, SIGNAL(clicked()),ao, SLOT(ButtonClicked()));
NewButton->setGeometry(50, 40, 300, 200);
NewButton->show();
//ui->setupUi(this);


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

harmodrew
5th August 2010, 10:08
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 :)

harmodrew
5th August 2010, 14:57
here I am again for the same problem...
mainwindow.cpp:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>

class act :public QObject
{
Q_OBJECT

};

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

act *ao= new act;
}

MainWindow::~MainWindow()
{
delete ui;
}


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!

Lykurg
5th August 2010, 16:11
put your class act in a single file or make sure moc runing right for your class. Simpliest is every class a single file!

harmodrew
5th August 2010, 16:22
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?

squidge
5th August 2010, 17:40
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.

harmodrew
5th August 2010, 17:45
oh, it's clear now! :) thank again! :)