PDA

View Full Version : Problem with connect



Momergil
28th July 2011, 22:37
Hello!

I'm having a problem with connect() function and I wasn't able to find a solution exactly despite numerous related problems have being reported in this forum.

the problem is: always when I want to make a connect() between a not-ui declared QPushButton and a slot function in a given thing ether the compiler returns error saying "no matching function for call ..." or else its says, when I run the software, that it couldn't find the slot.

So here is my situation: I'm creating a software that has a MenuBar with one of the options being "Settings". In the settings (a QWidget) there will be a couple of boxes and RadioButton and so forth to choose a couple of options to change my software. In order to make things simpler, I decided to create a specific class for almost all the windows (QWidgets, normally) that will be openend by clicking in the MenuBar.

The logic is supposed to be as follow:

1. in the MainWindow (that is connected with the ui and so its the place where the MenuBar click will be called) a pointer to the class with the Setings parameters is created. If the button in MenuBar "Settings" is pressed, a function opens a correspondent method in the windows class.

2. There, one of the automatic methods that are called is one that creates and connects all the buttons in the Settings window:


void MenuWindows::createSettingsBut()
{
//Cria botões de clique
v_butOK = new QPushButton("OK");
v_butCAN = new QPushButton("Cancela");
connect(v_butOK, SIGNAL (released()), this, SLOT (update()));
connect(v_butCAN, SIGNAL( released() ), v_setboxwidget, SLOT( close() ) );

//Cria ComboBox (que dá para escolher várias opções) - Qt19
v_combobut1 = new QComboBox(v_setboxwidget);
v_combobut1->addItem("Green");
v_combobut1->addItem("GBR");

//Cria botões de escolha (radio) - Qt18
v_radiobut1 = new QRadioButton("Op 1");
...
}

one of this buttons is "v_butOK", which is a QPushButton with "OK" on it.

3. So the guy using the software will set the configurations in the other buttons as he pleases, and once he wants to get out with the new configuration, he press "OK".

4. In this case, the "OK" QPushButton must be connected with a slot method of the class (class name is "MenuWindows" as seen above) which is named "update()". This method will verify each of the settings parameters to see which changes were made and, based in the analysis, will change the configurations of the software and close the Settings window:


void MenuWindows::update()
{
if (v_checkbut1->isChecked())
{
qDebug() << "Graph 1 on.";
}
else
{
qDebug() << "Graph 1 off.";
}
if (v_checkbut2->isChecked())
{
qDebug() << "Graph 2 on.";
}
else
{
qDebug() << "Graph 2 off.";
}
v_setboxwidget->close();
}
Note: this method is not finished (this is why there is just qDebugs() in it).

In the case, this update() method was declared as "public slots:" in the header file of the class "MenuWindows".

But when I run the software as it is above, the compilation says "Object::connect: No such slot QObject::update() in menuwindows.cpp:28". And the slot is there!

What am I missing?


Thanks!

squidge
28th July 2011, 22:41
Show your header file

Momergil
28th July 2011, 23:01
#ifndef MENUWINDOWS_H
#define MENUWINDOWS_H

//Botões e layouts
#include <QRadioButton>
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QComboBox>

#include <QMainWindow>
#include <QTextEdit>
#include <QString>
#include <QLabel>

class MenuWindows : public QObject
{
public:
//Functions
MenuWindows();
void createSettings();
void createSettingsBut();
void showSettings();
void createAbout(QWidget *a);

//Variables
//Botões
QRadioButton *v_radiobut1, *v_radiobut2, *v_radiobut3, *v_radiobut4;
QPushButton *v_but1, *v_but2, *v_but3, *v_but4, *v_butOK, *v_butCAN;
QComboBox *v_combobut1, *v_combobut2;
QCheckBox *v_checkbut1, *v_checkbut2, *v_checkbut3, *v_checkbut4, *v_checkbut5, *v_checkbut6;
//Outros
QWidget *v_setboxwidget;
QGridLayout *v_gridlayout;
QHBoxLayout *v_hlayout;
QVBoxLayout *v_vlayout;

bool v_ok;

public slots:
void update();

private:
QString textAbout;
};

#endif // MENUWINDOWS_H


Note: the ": public QObject" was something not present in the original. I put that as part of my work to make the connection works, but, as you may have figured out, it didn't...

nosleduc
29th July 2011, 00:40
The Q_OBJECT macro is missing in your header file

Momergil
29th July 2011, 00:59
The Q_OBJECT macro is missing in your header file

Umm, I heard about that earlier. But where am I supposed to put this in the header file? All places where I try, new problems with the compiler arises. For example, if I put it exactly in line 19, the following error appears:

/home/martin/Documentos/Programas Qt/Servidor 3.4.3/menuwindows.cpp:6: error: undefined reference to `vtable for MenuWindows'
/home/martin/Documentos/Programas Qt/Servidor 3.4.3/menuwindows.cpp:6: error: undefined reference to `vtable for MenuWindows'

And how exaclty should I put it?

nosleduc
29th July 2011, 01:10
class MenuWindows : public QObject
{
Q_OBJECT
public:
...
}

if you are using qtcreator you can just recompile but if you are using a different IDE you need to use again qmake on your .pro file it depends on the IDE you are using

Momergil
29th July 2011, 01:13
Well, I'm using Qt creator and I put the macro exactly in the place you (and the Help) indicates, but the problem persists. Here is the complete description of the problem:

moc_mytask.o moc_myserver.o moc_myclient.o -L/home/martin/QtSDK/Desktop/Qt/473/gcc/lib -L/usr/local/lib -lqwt-qt4 -lQtSvg -lQtGui -lQtNetwork -lQtCore -lpthread
menuwindows.o: In function `MenuWindows':
make: Saindo do diretório `/home/martin/Documentos/Programas Qt/Servidor 3.4.3'
/home/martin/Documentos/Programas Qt/Servidor 3.4.3/menuwindows.cpp:6: undefined reference to `vtable for MenuWindows'
/home/martin/Documentos/Programas Qt/Servidor 3.4.3/menuwindows.cpp:6: undefined reference to `vtable for MenuWindows'
collect2: ld returned 1 exit status
make: ** [Servidor] Erro 1
The process "/usr/bin/make" exited with code 2.
Error while building project Servidor (target: )
When executing build step 'Make'

It points to the cpp file.

nosleduc
29th July 2011, 01:18
In the build menu
1) Clean All
2) Run qmake
3) Rebuild all

Momergil
29th July 2011, 01:27
Oh! Finally!

Thanks!