Results 1 to 19 of 19

Thread: why doesn't the button work?

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default why doesn't the button work?

    Hi, i've created a widget with a button and a spinBox, i want to set a value in the spinBox when i'm going to click on the button. Down here the code.

    ui_spinForm.h --> created with QT Designer
    Qt Code:
    1. #ifndef UI_SPINFORM_H
    2. #define UI_SPINFORM_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QHBoxLayout>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QSpinBox>
    11. #include <QtGui/QWidget>
    12.  
    13. class Ui_spinForm
    14. {
    15. public:
    16. QWidget *widget;
    17. QHBoxLayout *hboxLayout;
    18. QSpinBox *spinBox;
    19. QPushButton *addPushButton;
    20.  
    21. void setupUi(QWidget *spinForm)
    22. {
    23. if (spinForm->objectName().isEmpty())
    24. spinForm->setObjectName(QString::fromUtf8("spinForm"));
    25. spinForm->resize(258, 52);
    26. widget = new QWidget(spinForm);
    27. widget->setObjectName(QString::fromUtf8("widget"));
    28. widget->setGeometry(QRect(60, 10, 133, 29));
    29. hboxLayout = new QHBoxLayout(widget);
    30. hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
    31. hboxLayout->setContentsMargins(0, 0, 0, 0);
    32. spinBox = new QSpinBox(widget);
    33. spinBox->setObjectName(QString::fromUtf8("spinBox"));
    34.  
    35. hboxLayout->addWidget(spinBox);
    36.  
    37. addPushButton = new QPushButton(widget);
    38. addPushButton->setObjectName(QString::fromUtf8("addPushButton"));
    39.  
    40. hboxLayout->addWidget(addPushButton);
    41.  
    42.  
    43. retranslateUi(spinForm);
    44.  
    45. QMetaObject::connectSlotsByName(spinForm);
    46. } // setupUi
    47.  
    48. void retranslateUi(QWidget *spinForm)
    49. {
    50. spinForm->setWindowTitle(QApplication::translate("spinForm", "Try a spinbox", 0, QApplication::UnicodeUTF8));
    51. addPushButton->setText(QApplication::translate("spinForm", "Add", 0, QApplication::UnicodeUTF8));
    52. Q_UNUSED(spinForm);
    53. } // retranslateUi
    54.  
    55. };
    56.  
    57. namespace Ui {
    58. class spinForm: public Ui_spinForm {};
    59. } // namespace Ui
    60.  
    61. #endif // UI_SPINFORM_H
    To copy to clipboard, switch view to plain text mode 

    spinForm.h
    Qt Code:
    1. #include "ui_spinForm.h"
    2.  
    3. class spinForm : public QWidget, public Ui::spinForm
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. spinForm(QWidget *parent = 0);
    9.  
    10. private slots:
    11. void setSpinBoxValue();
    12.  
    13. private:
    14. Ui::spinForm ui;
    15. };
    To copy to clipboard, switch view to plain text mode 

    spinForm.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "spinForm.h"
    3.  
    4. spinForm::spinForm(QWidget *parent):QWidget(parent)
    5. {
    6. connect(addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
    7. ui.setupUi(this);
    8. }
    9.  
    10. void spinForm::setSpinBoxValue()
    11. {
    12. spinBox->setValue(45);
    13. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "ui_spinForm.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QWidget *widget = new QWidget;
    9. Ui::spinForm ui;
    10. ui.setupUi(widget);
    11. widget->show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    When i compile it i don't have any problems, i can lunch the widget but when i click on the button nothing happen.
    I've tried to put spinBox->setValue(45) in the constructor too...
    I've checked the code a lot of time but i haven't catch any errors, maybe cos i'm a newbie!
    Thanks ppl for your support

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Post Re: why doesn't the button work?

    First, you are mixing aproaches.
    Either use the 'ui' as member OR derive it.
    I recommend using it as member - more flexible.
    Try:
    Qt Code:
    1. #include "ui_spinForm.h"
    2.  
    3. class spinForm : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. spinForm(QWidget *parent = 0);
    8. private slots:
    9. void setSpinBoxValue();
    10. private:
    11. Ui::spinForm ui;
    12. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include "spinForm.h"
    3.  
    4. spinForm::spinForm(QWidget *parent):QWidget(parent)
    5. {
    6. ui.setupUi(this);
    7. connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
    8.  
    9. }
    10.  
    11. void spinForm::setSpinBoxValue()
    12. {
    13. ui.spinBox->setValue(45);
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    I did it, and I realized that i was mixing aproaches.
    I don't have any errors when i compile the project, but the button doesn't work...thx

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't the button work?

    run it from a console, and see if you are getting any warnings
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    This is the output from my make command:
    Qt Code:
    1. $make:
    2. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.3.1/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.3.1/include/QtCore -I/usr/local/Trolltech/Qt-4.3.1/include/QtCore -I/usr/local/Trolltech/Qt-4.3.1/include/QtGui -I/usr/local/Trolltech/Qt-4.3.1/include/QtGui -I/usr/local/Trolltech/Qt-4.3.1/include -I. -I. -I. -o main.o main.cpp
    3. g++ -Wl,-rpath,/usr/local/Trolltech/Qt-4.3.1/lib -o spinForm main.o spinForm.o moc_spinForm.o -L/usr/local/Trolltech/Qt-4.3.1/lib -lQtGui -L/usr/local/Trolltech/Qt-4.3.1/lib -L/usr/X11R6/lib -lpng -lSM -lICE -pthread -pthread -lXi -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lglib-2.0 -lrt -ldl -lpthread
    To copy to clipboard, switch view to plain text mode 

    and i attach my .pro file too:
    Qt Code:
    1. TEMPLATE = app
    2. TARGET =
    3. DEPENDPATH += .
    4. INCLUDEPATH += .
    5.  
    6. # Input
    7. HEADERS += spinForm.h
    8. FORMS += spinForm.ui
    9. SOURCES += main.cpp spinForm.cpp
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't the button work?

    try changing setSpinBoxValue() from private slot to public slot!

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't the button work?

    what hgedek said is correct.
    And I didn't ask for compilation output but that you RUN the program in a console.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    i've changed it to public but i have some error:
    Qt Code:
    1. spinForm.cpp: In constructor ‘spinForm::spinForm(QWidget*)’:
    2. spinForm.cpp:9: error: ‘addPushButton’ was not declared in this scope
    3. spinForm.cpp: In member function ‘void spinForm::setSpinBoxValue()’:
    4. spinForm.cpp:15: error: ‘spinBox’ was not declared in this scope
    5. make: *** [spinForm.o] Error 1
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: why doesn't the button work?

    Quote Originally Posted by mattia View Post
    i've changed it to public but i have some error:
    Qt Code:
    1. spinForm.cpp: In constructor ‘spinForm::spinForm(QWidget*)’:
    2. spinForm.cpp:9: error: ‘addPushButton’ was not declared in this scope
    3. spinForm.cpp: In member function ‘void spinForm::setSpinBoxValue()’:
    4. spinForm.cpp:15: error: ‘spinBox’ was not declared in this scope
    5. make: *** [spinForm.o] Error 1
    To copy to clipboard, switch view to plain text mode 
    Actually, it doesn't matter whether it's public or not. That only affects the visibility when the slot is called as a normal function. However, looks like you still have mixed up "single inheritance" and "multiple inheritance" approaches. Take a closer look at second post in this thread.
    J-P Nurmi

  10. #10
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    yes, you are right, I had omit the ui. before the object declered into ui_spinForm.h.
    Now i've added them but i can't set any value into the spinBox, maybe i've to set the range?

  11. #11
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    I've added into Ui_spinForm.h a spinBox->setValue(5); and when i start the widget the value is 5. I've added ui.spinBox->setValue(5); into the spinForm.cpp constructor
    Qt Code:
    1. spinForm::spinForm(QWidget *parent):QWidget(parent)
    2. {
    3. ui.setupUi(this);
    4. connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
    5. ui.spinBox->setValue(3);
    6. }
    To copy to clipboard, switch view to plain text mode 

    but nothing...

  12. #12
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    mmm...i added a textBrowser to see if it works, but in the spinForm.cpp i can't add any text, no errors, but it doesn't work....

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't the button work?

    post your full code, and run the application in a console and see if you get any warnings.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    ui_spinForm.h
    Qt Code:
    1. #ifndef UI_SPINFORM_H
    2. #define UI_SPINFORM_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QHBoxLayout>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QSpinBox>
    11. #include <QtGui/QTextBrowser>
    12. #include <QtGui/QWidget>
    13.  
    14. class Ui_spinForm
    15. {
    16. public:
    17. QWidget *layoutWidget;
    18. QHBoxLayout *hboxLayout;
    19. QSpinBox *spinBox;
    20. QPushButton *addPushButton;
    21. QTextBrowser *textBrowser;
    22.  
    23. void setupUi(QWidget *spinForm)
    24. {
    25. if (spinForm->objectName().isEmpty())
    26. spinForm->setObjectName(QString::fromUtf8("spinForm"));
    27. spinForm->resize(359, 370);
    28. layoutWidget = new QWidget(spinForm);
    29. layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
    30. layoutWidget->setGeometry(QRect(60, 10, 133, 29));
    31. hboxLayout = new QHBoxLayout(layoutWidget);
    32. hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
    33. hboxLayout->setContentsMargins(0, 0, 0, 0);
    34. spinBox = new QSpinBox(layoutWidget);
    35. spinBox->setObjectName(QString::fromUtf8("spinBox"));
    36. spinBox->setValue(5);
    37.  
    38. hboxLayout->addWidget(spinBox);
    39.  
    40. addPushButton = new QPushButton(layoutWidget);
    41. addPushButton->setObjectName(QString::fromUtf8("addPushButton"));
    42.  
    43. hboxLayout->addWidget(addPushButton);
    44.  
    45. textBrowser = new QTextBrowser(spinForm);
    46. textBrowser->setObjectName(QString::fromUtf8("textBrowser"));
    47. textBrowser->setGeometry(QRect(40, 70, 256, 192));
    48.  
    49. retranslateUi(spinForm);
    50.  
    51. QMetaObject::connectSlotsByName(spinForm);
    52. } // setupUi
    53.  
    54. void retranslateUi(QWidget *spinForm)
    55. {
    56. spinForm->setWindowTitle(QApplication::translate("spinForm", "Try a spinbox", 0, QApplication::UnicodeUTF8));
    57. addPushButton->setText(QApplication::translate("spinForm", "Add", 0, QApplication::UnicodeUTF8));
    58.  
    59. Q_UNUSED(spinForm);
    60. } // retranslateUi
    61.  
    62. };
    63.  
    64. namespace Ui {
    65. class spinForm: public Ui_spinForm {};
    66. } // namespace Ui
    67.  
    68. #endif // UI_SPINFORM_H
    To copy to clipboard, switch view to plain text mode 

    spinForm.h
    Qt Code:
    1. #include "ui_spinForm.h"
    2.  
    3. class spinForm : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. spinForm(QWidget *parent = 0);
    9.  
    10. public slots:
    11. void setSpinBoxValue();
    12.  
    13. private:
    14. Ui::spinForm ui;
    15. };
    To copy to clipboard, switch view to plain text mode 

    spinForm.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "spinForm.h"
    3.  
    4. spinForm::spinForm(QWidget *parent):QWidget(parent)
    5. {
    6. ui.setupUi(this);
    7. connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
    8. ui.spinBox->setValue(3);
    9.  
    10. }
    11.  
    12. void spinForm::setSpinBoxValue()
    13. {
    14. ui.spinBox->setValue(45);
    15. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "ui_spinForm.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QWidget *widget = new QWidget;
    9. Ui::spinForm ui;
    10. ui.setupUi(widget);
    11. widget->show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    what can i do to run it in a consolle? is it helpful to see the run-time error?
    cos now i'm using QT Designer and a simple text editor.
    Thanks

  15. #15
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    I imported the project into Eclipse with QT library plug-in but when i make to run the application nothing is shown in consolle.

  16. #16
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't the button work?

    In your main() you instantiate Ui::spinform not your own class spinform.
    Your code is never executed -> that's why you don't see anything!

  17. #17
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    Quote Originally Posted by DeepDiver View Post
    In your main() you instantiate Ui::spinform not your own class spinform.
    Your code is never executed -> that's why you don't see anything!
    Thanks for the hint, i've chenged my main.cpp in this way:
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "spinForm.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QWidget *widget = new QWidget;
    9. spinForm ui(widget);
    10. //ui.setupUi(widget);
    11. widget->show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    and now it run, thanks to everybody...it was my first program with a slot and signal

  18. #18
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: why doesn't the button work?

    Quote Originally Posted by high_flyer View Post
    First, you are mixing aproaches.
    Either use the 'ui' as member OR derive it.
    I recommend using it as member - more flexible.
    I'd like to know how's the derive approach, are there any examples on the net?
    Thanks

  19. #19
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: why doesn't the button work?

    Its all in the docs.
    Note that you can use the single or multiple inheritance approaches, see what best fits your case.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  2. How to ignore Button-Events in Qtopia?
    By Helmut Hönig in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 5th July 2007, 13:24
  3. Mouse Over event on button
    By vishal.chauhan in forum Qt Programming
    Replies: 9
    Last Post: 10th January 2007, 05:03
  4. Replies: 2
    Last Post: 1st August 2006, 10:23
  5. Push button double click
    By curtisw in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 16:40

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.