PDA

View Full Version : How can i use my funcs in qt?



Helegurbann
10th May 2011, 23:52
#include <qt/qapplication.h>
#include <qt/qpushbutton.h>
#include <qt/qtextedit.h>
#include <qt/qwidget.h>
#include <qt/qtextcodec.h>
#include <qt/QOBJECT.h>
#include <stdio.h>
using namespace std;
int a,b,c;
int Plus(int,int){
c=a+b;
return c;
}

void Exit(){
return exit(1);}

int main(int argc, char *argv[])
{QTextCodec::setCodecForTr( QTextCodec::codecForName("ISO-8859-9") );
QTextCodec::setCodecForCStrings
( QTextCodec::codecForName("ISO-8859-9") );


QApplication app(argc,argv);
QWidget Window;

Window.resize(250,200);
QPushButton button("Plus",&Window);
QPushButton button2("Exit",&Window);
button.setGeometry(75,150,70,30);
button2.setGeometry(150,150,70,30);
QTextEdit textfield1(&Window);
QTextEdit textfield2(&Window);
QTextEdit textfield3(&Window);
textfield1.setGeometry(75,80,120,25);
textfield2.setGeometry(75,110,120,25);
textfield3.setGeometry(75,140,120,25);
a=textfield1.text.ToInt32();
b=textfield2.text.ToInt32();
c=textfield3.text.ToInt32();
QObject::connect(&button,SIGNAL(clicked()),&textfield3,SLOT(Plus()));
QObject::connect(&button2,SIGNAL(clicked()),&app,SLOT(Exit()));//I don't want to use quit()
Window.show();
return app.exec();
}


How can i use Plus() and Exit() funcs?

Zlatomir
11th May 2011, 00:25
First read the basics about signals and slots here (http://doc.qt.nokia.com/4.7/signalsandslots.html) and then you will see that they are designed for inter-objects communications - and your textfield3 doesn't have a slot called Plus() (which is different than Plus(int param1, int param2) - why do you have those unused parameters?) and app doesn't have a slot called Exit().

So basically you need to code your own slots, you can start by inheriting from some object and expanding them to do what you want (or call the free functions from the slots you code).

Also read about the layouts (http://doc.qt.nokia.com/4.7/widgets-and-layouts.html) (so that you don't have to hard-code the widgets dimensions and position - and recode every-time the screen size changes)

And another suggestion is to use the parent-child relationship and allocate the children on the heap (else you might have errors when you close the application because the parent tries to delete a stack allocated resource)

Helegurbann
12th May 2011, 21:33
Thank u for ur help.I tred this method:


#include <qt/qapplication.h>
#include <qt/qpushbutton.h>
#include <qt/qtextedit.h>
#include <qt/qwidget.h>
#include <qt/qtextcodec.h>
#include <qt/QOBJECT.h>
int a,b;
int toplama(int,int){return (a+b);}
class toplama : public QObject
{
Q_OBJECT
public slots:
toplama(int,int);

signals:
void clicked();
};

toplama::toplama(int c,int d){
a=c;
b=d;

emit clicked();
}
using namespace std;
int main(int argc, char *argv[])
{QTextCodec::setCodecForTr( QTextCodec::codecForName("ISO-8859-9") );
QTextCodec::setCodecForCStrings
( QTextCodec::codecForName("ISO-8859-9") );


QApplication app(argc,argv);
QWidget Window;

Window.resize(250,200);
QPushButton button("Topla",&Window);
QPushButton button2("Çık",&Window);
button.setGeometry(75,130,70,30);
button2.setGeometry(150,130,70,30);
QTextEdit textfield1(&Window);
QTextEdit textfield2(&Window);
QTextEdit textfield3(&Window);
textfield1.setGeometry(75,40,120,25);
textfield2.setGeometry(75,70,120,25);
textfield3.setGeometry(75,100,120,25);




QObject::connect(&button,SIGNAL(clicked()),&textfield3,SLOT(toplama(textfield1.text().ToInt32( ),textfield2.text().ToInt32())));
QObject::connect(&button2,SIGNAL(clicked()),&app,SLOT(quit()));
Window.show();
return app.exec();
}


But i took linker errors in this down:


[Linker error] undefined reference to `vtable for toplama'
[Linker error] undefined reference to `toplama::clicked()'
[Linker error] undefined reference to `vtable for toplama'
[Linker error] undefined reference to `toplama::clicked()'
ld returned 1 exit status
F:\QT\Makefile.win [Build Error] [QPlus.exe] Error 1

DanH
13th May 2011, 04:17
It's going to badly confuse things to have both a class and a static non-class method named "toplama".

And this is not a valid SLOT reference:
SLOT(toplama(textfield1.text().ToInt32(),textfield 2.text().ToInt32())

And numerous other problems.

You're greatly over-reaching. You need to understand the basics of C++ before you try anything fancy.

Check out this list of what you need to know:
http://discussion.forum.nokia.com/forum/showthread.php?221302-What-to-know-before-you-attempt-to-program-in-Qt

Helegurbann
13th May 2011, 17:12
Can u write exam code pls?Because this subject(signal and slot) mix up my head.

zim
13th May 2011, 17:42
The best thing for you (easier also) is to read a book (http://cartan.cas.suffolk.edu/oopdocbook/html/) on QT. This will get you up on your feet in no time.

in header class



public slots:
void plusSlot();
void exitSlot();


in source



void MyClass:plusSlot()
{
this->textfield3.set(plus(this->textfield1.getSomething,this->text.... and so on)
}
void MyClass:exitSlot()
{
samething here
}

Helegurbann
13th May 2011, 18:56
Thank u for ur help.But cannot create my func in qt?I dont want to use plus() or quit() funcs.End i added in QPushButton func in:


/************************************************** **************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
************************************************** **************************/

#ifndef QPUSHBUTTON_H
#define QPUSHBUTTON_H
#include <stdio.h>
#include <QtGui/qabstractbutton.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Gui)

class QPushButtonPrivate;
class QMenu;
class QStyleOptionButton;


class Q_GUI_EXPORT QPushButton : public QAbstractButton
{
Q_OBJECT

Q_PROPERTY(bool autoDefault READ autoDefault WRITE setAutoDefault)
Q_PROPERTY(bool default READ isDefault WRITE setDefault)
Q_PROPERTY(bool flat READ isFlat WRITE setFlat)

public:
int Topla(int d,int e){a=d;
b=d;
return (a+b);}
void Cikis();
explicit QPushButton(QWidget *parent=0);
explicit QPushButton(const QString &text, QWidget *parent=0);
QPushButton(const QIcon& icon, const QString &text, QWidget *parent=0);
~QPushButton();

QSize sizeHint() const;
QSize minimumSizeHint() const;

bool autoDefault() const;
void setAutoDefault(bool);
bool isDefault() const;
void setDefault(bool);

#ifndef QT_NO_MENU
void setMenu(QMenu* menu);
QMenu* menu() const;
#endif

void setFlat(bool);
bool isFlat() const;

public Q_SLOTS:
#ifndef QT_NO_MENU
void showMenu();
int Toplama(int Topla);
#endif



signals:
void clicked();
protected:
bool event(QEvent *e);
#ifdef Q_WS_MAC
bool hitButton(const QPoint &pos) const;
#endif // Q_WS_MAC
void paintEvent(QPaintEvent *);
void keyPressEvent(QKeyEvent *);
void focusInEvent(QFocusEvent *);
void focusOutEvent(QFocusEvent *);
void initStyleOption(QStyleOptionButton *option) const;
QPushButton(QPushButtonPrivate &dd, QWidget* parent = 0);

public:
#ifdef QT3_SUPPORT
QT3_SUPPORT_CONSTRUCTOR QPushButton(QWidget *parent, const char* name);
QT3_SUPPORT_CONSTRUCTOR QPushButton(const QString &text, QWidget *parent, const char* name);
QT3_SUPPORT_CONSTRUCTOR QPushButton(const QIcon& icon, const QString &text, QWidget *parent, const char* name);
inline QT3_SUPPORT void openPopup() { showMenu(); }
inline QT3_SUPPORT bool isMenuButton() const { return menu() != 0; }
inline QT3_SUPPORT void setPopup(QMenu* popup) {setMenu(popup); }
inline QT3_SUPPORT QMenu* popup() const { return menu(); }
#endif



private:
Q_DISABLE_COPY(QPushButton)
Q_DECLARE_PRIVATE(QPushButton)
int a,b,c;
#ifndef QT_NO_MENU
Q_PRIVATE_SLOT(d_func(), void _q_popupPressed())

#endif
};
int QPushButton::Toplama(int Topla){
emit clicked();
}


QT_END_NAMESPACE

QT_END_HEADER

#endif // QPUSHBUTTON_H


I taken this linker error:


[Linker error] undefined reference to `_imp___ZN11QPushButton7clickedEv'
ld returned 1 exit status
F:\QT\Makefile.win [Build Error] [QTextEdit.exe] Error 1