PDA

View Full Version : Problem When Creating my own Slot



Fatla
2nd June 2008, 19:44
I'm creating my first app with my own slot .
Everything goes sooo fine till now . However when I want to create my own slot , nothing
is happened (i.e. slot didn't work !! )

The purpose of my App : when the user press a button , an empty .txt file 'll be created .

I'm working under MS Win XP , & I've tried to run it over Linux to find out the problem . I've such that output :

Object::connect: No such slot QDialog::Custom()
Object::connect: (sender name: 'pushButton')
Object::connect: (receiver name: 'Form')

Though , I've add such this slot "Custom" @ QTDesign .

I've got .exe BUT the slot never responds ( i.e. Createing the file )

I've added a little piece of code @ the header file which was generated , is that true !!
I'm using :
- qt-win-opensource-4.4.0
- qmake to build the project
-MinGw
N.B. : I've created the slot , according to what I've understand from QT Assistant

So , What' the problem @ My Code :



/************************************************** ******************************
** Form generated from reading ui file 'Test.ui'
**
** Created: Mon Jun 2 06:29:48 2008
** by: Qt User Interface Compiler version 4.4.0
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_TEST_H
#define UI_TEST_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

// **************** My Own Code *********************
#include <stdio.h>

// **************** My Own Code *********************

QT_BEGIN_NAMESPACE

class Ui_Form
{

public:
QPushButton *pushButton;
QLineEdit *lineEdit;

void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(475, 393);
pushButton = new QPushButton(Form);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(170, 190, 75, 24));
lineEdit = new QLineEdit(Form);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(160, 90, 113, 20));

retranslateUi(Form);
QObject::connect(pushButton, SIGNAL(pressed()), Form, SLOT(Custom()));
QMetaObject::connectSlotsByName(Form);
} // setupUi

void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
lineEdit->setText(QString());
Q_UNUSED(Form);
} // retranslateUi


// **************** My Own Code *********************
public slots:
void Custom();
//******* My Own Code *********************

};

// **************** My Own Code *********************
void Ui_Form::Custom()
{
FILE * pFile;
pFile = fopen ("C:/Documents and Settings/Ahmed Osama/Desktop/myfile.txt","w");
}
// **************** My Own Code *********************

namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TEST_H



Thanks

fnmblot
2nd June 2008, 20:17
Yes it is true. All changes to the file will be lost when you build the project again.

Fatla
3rd June 2008, 05:22
So , What's the problem in my Code which refuses to invoke my "Custom" function ?!!

jpn
3rd June 2008, 07:17
Ui_Form is not a QObject. Only QObjects can have slots. Take a look at Using a Component in Your Application (http://doc.trolltech.com/4.4/designer-using-a-component.html). Pay attention to single and multiple inheritance approaches. You are supposed to implement the slot in your own class which uses Ui_Form. Do NOT edit ui_test.h!

Fatla
4th June 2008, 00:00
Really Thanks jpn as well as fnmblot for your help .
The resource that you've given to me was Extremely helpful .
I've applied what was mentioned there . However , still my own slot doesn't work ?!!

I've 4 main files besides the other makefile ,....etc
1- Test.ui
2- Main.cpp
3- ui_Test.h
4- CustomSlot.cpp => Which I implement my own slot into it .

Her are my Codes :

=> Main.cpp 's code is :


#include <QApplication>
#include <QDialog>

#include "ui_Test.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);



QWidget *widget = new QWidget;
Ui_Form ui;
ui.setupUi(widget);

widget->show();
return app.exec();
}




-----------------------------------------------------------------------------------------------
=> ui_Test.h 's code is :



/************************************************** ******************************
** Form generated from reading ui file 'Test.ui'
**
** Created: Tue Jun 3 23:57:00 2008
** by: Qt User Interface Compiler version 4.4.0
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_TEST_H
#define UI_TEST_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Form
{
public:
QPushButton *pushButton;
QLineEdit *lineEdit;

void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(533, 393);
pushButton = new QPushButton(Form);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(170, 190, 75, 24));
lineEdit = new QLineEdit(Form);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(160, 90, 113, 20));

retranslateUi(Form);
QObject::connect(pushButton, SIGNAL(pressed()), Form, SLOT(CustomSlot()));

QMetaObject::connectSlotsByName(Form);
} // setupUi

void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
lineEdit->setText(QString());
Q_UNUSED(Form);
} // retranslateUi

};

namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TEST_H


-----------------------------------------------------------------------------------------------

=> CustomSlot.cpp 's code is :



#include "ui_Test.h"
#include <stdio.h>

class Test: public QWidget
{
// Q_OBJECT

public:
Test(QWidget *parent = 0);

private slots:
void CustomSlot();


private:
Ui_Form ui;
};


Test::Test(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}

void Test:: CustomSlot()
{
FILE * pFile;
pFile = fopen ("C:/myfile.txt","w");
}


Thanks

jacek
4th June 2008, 00:37
Uncomment Q_OBJECT and re-run qmake.

jpn
4th June 2008, 07:32
You should:

not declare classes with Q_OBJECT macro in a .cpp file but
place the class declaration in a header file, say CustomSlot.h
add the header file to the .pro file's HEADERS variable
continue with what Jacek said

A workaround is to include the moc file in the end of CustomSlot.cpp.

Fatla
5th June 2008, 18:17
Thanks fnmblot as well as Thanks again jpn .
It seems that I'm that kind of annoying students who won't stop questioning :)

I've made what you 've told me & separated the declaration from the implementation .
However , my slot didn't work yet ?!!
+ How does the header file which generated ,ui_Test.h , can call my CustomSlot ?!!

I've changed a little in my App :
Here are my Codes :

=> Main.cpp 's code is :



#include <QApplication>
#include <QDialog>

#include "ui_Test.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);



QWidget *widget = new QWidget;
Ui_MyForm ui;
ui.setupUi(widget);

widget->show();
return app.exec();
}




---------------------------------------------------------------------------------------------------------------


ui_Test.h 's code is :



/************************************************** ******************************
** Form generated from reading ui file 'Test.ui'
**
** Created: Thu Jun 5 19:58:05 2008
** by: Qt User Interface Compiler version 4.4.0
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_TEST_H
#define UI_TEST_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MyForm
{
public:
QPushButton *btnCreateFile;
QLineEdit *txtData;

void setupUi(QWidget *MyForm)
{
if (MyForm->objectName().isEmpty())
MyForm->setObjectName(QString::fromUtf8("MyForm"));
MyForm->resize(533, 393);
btnCreateFile = new QPushButton(MyForm);
btnCreateFile->setObjectName(QString::fromUtf8("btnCreateFile"));
btnCreateFile->setGeometry(QRect(170, 190, 75, 24));
txtData = new QLineEdit(MyForm);
txtData->setObjectName(QString::fromUtf8("txtData"));
txtData->setGeometry(QRect(160, 90, 113, 20));

retranslateUi(MyForm);
QObject::connect(btnCreateFile, SIGNAL(pressed()), MyForm, SLOT(CreateFile()));

QMetaObject::connectSlotsByName(MyForm);
} // setupUi

void retranslateUi(QWidget *MyForm)
{
MyForm->setWindowTitle(QApplication::translate("MyForm", "Form", 0, QApplication::UnicodeUTF8));
btnCreateFile->setText(QApplication::translate("MyForm", "PushButton", 0, QApplication::UnicodeUTF8));
txtData->setText(QString());
Q_UNUSED(MyForm);
} // retranslateUi

};

namespace Ui {
class MyForm: public Ui_MyForm {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_TEST_H


---------------------------------------------------------------------------------------------------------------

=> CustomSlot.h 's code is :



#ifndef CustmSlot_Declaration_H
#define CustmSlot_Declaration_H
#include "ui_Test.h"

class CustomSlot: public QWidget
{
Q_OBJECT

public:
CustomSlot(QWidget *parent = 0);

private slots:
void CreateFile();


private:
Ui_MyForm ui;
};
#endif




---------------------------------------------------------------------------------------------------------------

=> CustomSlot.cpp 's code is :



#include"CustomSlot.h"
#include "ui_Test.h"
#include <stdio.h>




CustomSlot::CustomSlot(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}

void CustomSlot:: CreateFile()
{
FILE * pFile;
pFile = fopen ("C:/myfile.txt","w");
}


Thanks

jpn
5th June 2008, 18:22
QWidget *widget = new QWidget;
Ui_MyForm ui;
ui.setupUi(widget);

widget->show();


No instance of CustomSlot is created anywhere. You instantiate a plain QWidget and use the non-recommended "direct approach". It should be:


CustomSlot widget;
widget.show();

vycke
5th June 2008, 21:59
Plus you need to have a

connect(ui->btnCreateFile, SIGNAL(pressed()), this, SLOT(CreateFile()));

line in the constructor of your CustomSlot class.

jpn
5th June 2008, 22:17
Plus you need to have a

connect(ui->btnCreateFile, SIGNAL(pressed()), this, SLOT(CreateFile()));

line in the constructor of your CustomSlot class.
Actually that's already done in the generated code. Qt Designer 4.4.0 introduced (http://trolltech.com/developer/resources/notes/changes/changes-4.4.0) a way to define custom slots:

* [132874] Added support for user-defined signals and slots of promoted widgets and main container

Fatla
6th June 2008, 04:26
Hooooooooray , it's Working Now :)

Thanks vycke .
Thanks SooOoOo Much jpn . I owe you a lot .

vycke
6th June 2008, 14:44
Actually that's already done in the generated code. Qt Designer 4.4.0 introduced (http://trolltech.com/developer/resources/notes/changes/changes-4.4.0) a way to define custom slots:


Heh.. missed that in the .ui code -- didn't really look hard though.

Vycke