Results 1 to 7 of 7

Thread: How can i use my funcs in qt?

  1. #1
    Join Date
    Apr 2011
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4

    Default How can i use my funcs in qt?

    Qt Code:
    1. #include <qt/qapplication.h>
    2. #include <qt/qpushbutton.h>
    3. #include <qt/qtextedit.h>
    4. #include <qt/qwidget.h>
    5. #include <qt/qtextcodec.h>
    6. #include <qt/QOBJECT.h>
    7. #include <stdio.h>
    8. using namespace std;
    9. int a,b,c;
    10. int Plus(int,int){
    11. c=a+b;
    12. return c;
    13. }
    14.  
    15. void Exit(){
    16. return exit(1);}
    17.  
    18. int main(int argc, char *argv[])
    19. {QTextCodec::setCodecForTr( QTextCodec::codecForName("ISO-8859-9") );
    20. QTextCodec::setCodecForCStrings
    21. ( QTextCodec::codecForName("ISO-8859-9") );
    22.  
    23.  
    24. QApplication app(argc,argv);
    25. QWidget Window;
    26.  
    27. Window.resize(250,200);
    28. QPushButton button("Plus",&Window);
    29. QPushButton button2("Exit",&Window);
    30. button.setGeometry(75,150,70,30);
    31. button2.setGeometry(150,150,70,30);
    32. QTextEdit textfield1(&Window);
    33. QTextEdit textfield2(&Window);
    34. QTextEdit textfield3(&Window);
    35. textfield1.setGeometry(75,80,120,25);
    36. textfield2.setGeometry(75,110,120,25);
    37. textfield3.setGeometry(75,140,120,25);
    38. a=textfield1.text.ToInt32();
    39. b=textfield2.text.ToInt32();
    40. c=textfield3.text.ToInt32();
    41. QObject::connect(&button,SIGNAL(clicked()),&textfield3,SLOT(Plus()));
    42. QObject::connect(&button2,SIGNAL(clicked()),&app,SLOT(Exit()));//I don't want to use quit()
    43. Window.show();
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How can i use my funcs in qt?

    First read the basics about signals and slots here 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 (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)

  3. #3
    Join Date
    Apr 2011
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4

    Default Re: How can i use my funcs in qt?

    Thank u for ur help.I tred this method:
    Qt Code:
    1. #include <qt/qapplication.h>
    2. #include <qt/qpushbutton.h>
    3. #include <qt/qtextedit.h>
    4. #include <qt/qwidget.h>
    5. #include <qt/qtextcodec.h>
    6. #include <qt/QOBJECT.h>
    7. int a,b;
    8. int toplama(int,int){return (a+b);}
    9. class toplama : public QObject
    10. {
    11. Q_OBJECT
    12. public slots:
    13. toplama(int,int);
    14.  
    15. signals:
    16. void clicked();
    17. };
    18.  
    19. toplama::toplama(int c,int d){
    20. a=c;
    21. b=d;
    22.  
    23. emit clicked();
    24. }
    25. using namespace std;
    26. int main(int argc, char *argv[])
    27. {QTextCodec::setCodecForTr( QTextCodec::codecForName("ISO-8859-9") );
    28. QTextCodec::setCodecForCStrings
    29. ( QTextCodec::codecForName("ISO-8859-9") );
    30.  
    31.  
    32. QApplication app(argc,argv);
    33. QWidget Window;
    34.  
    35. Window.resize(250,200);
    36. QPushButton button("Topla",&Window);
    37. QPushButton button2("Çık",&Window);
    38. button.setGeometry(75,130,70,30);
    39. button2.setGeometry(150,130,70,30);
    40. QTextEdit textfield1(&Window);
    41. QTextEdit textfield2(&Window);
    42. QTextEdit textfield3(&Window);
    43. textfield1.setGeometry(75,40,120,25);
    44. textfield2.setGeometry(75,70,120,25);
    45. textfield3.setGeometry(75,100,120,25);
    46.  
    47.  
    48.  
    49.  
    50. QObject::connect(&button,SIGNAL(clicked()),&textfield3,SLOT(toplama(textfield1.text().ToInt32(),textfield2.text().ToInt32())));
    51. QObject::connect(&button2,SIGNAL(clicked()),&app,SLOT(quit()));
    52. Window.show();
    53. return app.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

    But i took linker errors in this down:
    Qt Code:
    1. [Linker error] undefined reference to `vtable for toplama'
    2. [Linker error] undefined reference to `toplama::clicked()'
    3. [Linker error] undefined reference to `vtable for toplama'
    4. [Linker error] undefined reference to `toplama::clicked()'
    5. ld returned 1 exit status
    6. F:\QT\Makefile.win [Build Error] [QPlus.exe] Error 1
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: How can i use my funcs in qt?

    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/fo...-program-in-Qt

  5. #5
    Join Date
    Apr 2011
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4

    Default Re: How can i use my funcs in qt?

    Can u write exam code pls?Because this subject(signal and slot) mix up my head.

  6. #6
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can i use my funcs in qt?

    The best thing for you (easier also) is to read a book on QT. This will get you up on your feet in no time.

    in header class

    Qt Code:
    1. public slots:
    2. void plusSlot();
    3. void exitSlot();
    To copy to clipboard, switch view to plain text mode 

    in source
    Qt Code:
    1. void MyClass:plusSlot()
    2. {
    3. this->textfield3.set(plus(this->textfield1.getSomething,this->text.... and so on)
    4. }
    5. void MyClass:exitSlot()
    6. {
    7. samething here
    8. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2011
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4

    Default Re: How can i use my funcs in qt?

    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:
    Qt Code:
    1. /****************************************************************************
    2. **
    3. ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    4. ** All rights reserved.
    5. ** Contact: Nokia Corporation (qt-info@nokia.com)
    6. **
    7. ** This file is part of the QtGui module of the Qt Toolkit.
    8. **
    9. ** $QT_BEGIN_LICENSE:LGPL$
    10. ** Commercial Usage
    11. ** Licensees holding valid Qt Commercial licenses may use this file in
    12. ** accordance with the Qt Commercial License Agreement provided with the
    13. ** Software or, alternatively, in accordance with the terms contained in
    14. ** a written agreement between you and Nokia.
    15. **
    16. ** GNU Lesser General Public License Usage
    17. ** Alternatively, this file may be used under the terms of the GNU Lesser
    18. ** General Public License version 2.1 as published by the Free Software
    19. ** Foundation and appearing in the file LICENSE.LGPL included in the
    20. ** packaging of this file. Please review the following information to
    21. ** ensure the GNU Lesser General Public License version 2.1 requirements
    22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    23. **
    24. ** In addition, as a special exception, Nokia gives you certain additional
    25. ** rights. These rights are described in the Nokia Qt LGPL Exception
    26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    27. **
    28. ** GNU General Public License Usage
    29. ** Alternatively, this file may be used under the terms of the GNU
    30. ** General Public License version 3.0 as published by the Free Software
    31. ** Foundation and appearing in the file LICENSE.GPL included in the
    32. ** packaging of this file. Please review the following information to
    33. ** ensure the GNU General Public License version 3.0 requirements will be
    34. ** met: http://www.gnu.org/copyleft/gpl.html.
    35. **
    36. ** If you have questions regarding the use of this file, please contact
    37. ** Nokia at qt-info@nokia.com.
    38. ** $QT_END_LICENSE$
    39. **
    40. ****************************************************************************/
    41.  
    42. #ifndef QPUSHBUTTON_H
    43. #define QPUSHBUTTON_H
    44. #include <stdio.h>
    45. #include <QtGui/qabstractbutton.h>
    46.  
    47. QT_BEGIN_HEADER
    48.  
    49. QT_BEGIN_NAMESPACE
    50.  
    51. QT_MODULE(Gui)
    52.  
    53. class QPushButtonPrivate;
    54. class QMenu;
    55.  
    56.  
    57. class Q_GUI_EXPORT QPushButton : public QAbstractButton
    58. {
    59. Q_OBJECT
    60.  
    61. Q_PROPERTY(bool autoDefault READ autoDefault WRITE setAutoDefault)
    62. Q_PROPERTY(bool default READ isDefault WRITE setDefault)
    63. Q_PROPERTY(bool flat READ isFlat WRITE setFlat)
    64.  
    65. public:
    66. int Topla(int d,int e){a=d;
    67. b=d;
    68. return (a+b);}
    69. void Cikis();
    70. explicit QPushButton(QWidget *parent=0);
    71. explicit QPushButton(const QString &text, QWidget *parent=0);
    72. QPushButton(const QIcon& icon, const QString &text, QWidget *parent=0);
    73.  
    74. QSize sizeHint() const;
    75. QSize minimumSizeHint() const;
    76.  
    77. bool autoDefault() const;
    78. void setAutoDefault(bool);
    79. bool isDefault() const;
    80. void setDefault(bool);
    81.  
    82. #ifndef QT_NO_MENU
    83. void setMenu(QMenu* menu);
    84. QMenu* menu() const;
    85. #endif
    86.  
    87. void setFlat(bool);
    88. bool isFlat() const;
    89.  
    90. public Q_SLOTS:
    91. #ifndef QT_NO_MENU
    92. void showMenu();
    93. int Toplama(int Topla);
    94. #endif
    95.  
    96.  
    97.  
    98. signals:
    99. void clicked();
    100. protected:
    101. bool event(QEvent *e);
    102. #ifdef Q_WS_MAC
    103. bool hitButton(const QPoint &pos) const;
    104. #endif // Q_WS_MAC
    105. void paintEvent(QPaintEvent *);
    106. void keyPressEvent(QKeyEvent *);
    107. void focusInEvent(QFocusEvent *);
    108. void focusOutEvent(QFocusEvent *);
    109. void initStyleOption(QStyleOptionButton *option) const;
    110. QPushButton(QPushButtonPrivate &dd, QWidget* parent = 0);
    111.  
    112. public:
    113. #ifdef QT3_SUPPORT
    114. QT3_SUPPORT_CONSTRUCTOR QPushButton(QWidget *parent, const char* name);
    115. QT3_SUPPORT_CONSTRUCTOR QPushButton(const QString &text, QWidget *parent, const char* name);
    116. QT3_SUPPORT_CONSTRUCTOR QPushButton(const QIcon& icon, const QString &text, QWidget *parent, const char* name);
    117. inline QT3_SUPPORT void openPopup() { showMenu(); }
    118. inline QT3_SUPPORT bool isMenuButton() const { return menu() != 0; }
    119. inline QT3_SUPPORT void setPopup(QMenu* popup) {setMenu(popup); }
    120. inline QT3_SUPPORT QMenu* popup() const { return menu(); }
    121. #endif
    122.  
    123.  
    124.  
    125. private:
    126. Q_DISABLE_COPY(QPushButton)
    127. Q_DECLARE_PRIVATE(QPushButton)
    128. int a,b,c;
    129. #ifndef QT_NO_MENU
    130. Q_PRIVATE_SLOT(d_func(), void _q_popupPressed())
    131.  
    132. #endif
    133. };
    134. int QPushButton::Toplama(int Topla){
    135. emit clicked();
    136. }
    137.  
    138.  
    139. QT_END_NAMESPACE
    140.  
    141. QT_END_HEADER
    142.  
    143. #endif // QPUSHBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    I taken this linker error:
    Qt Code:
    1. [Linker error] undefined reference to `_imp___ZN11QPushButton7clickedEv'
    2. ld returned 1 exit status
    3. F:\QT\Makefile.win [Build Error] [QTextEdit.exe] Error 1
    To copy to clipboard, switch view to plain text mode 

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.