Results 1 to 6 of 6

Thread: Buttons on GUI don't work (No Such Slot error)

  1. #1
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Buttons on GUI don't work (No Such Slot error)

    Hi,

    Im pretty new to QT, and I tried following a tutorial on youtube (part1, part2) to build a simple timer.

    When compiling my code, I get the warning:
    Object::connect: No such slot MainWindow::start()
    Object::connect: (sender name: 'bt_start')
    Object::connect: (receiver name: 'MainWindow')

    Here's my code so far:

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include "ui_mainwindow.h"
    4. #include <QPushButton>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. MainWindow w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTimer>
    6. #include "ui_mainwindow.h"
    7. #include <QTGui>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16. public:
    17. QTimer *timer;
    18. int hours;
    19. int minutes;
    20. int seconds;
    21. explicit MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. void count();
    27. void start();
    28. void stop();
    29. void reset();
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. hours = 1;
    10. minutes = 2;
    11. seconds = 3;
    12. timer = new QTimer(this);
    13. connect(timer, SIGNAL(timeout()), this, SLOT(count()));
    14. connect(ui->bt_start, SIGNAL(clicked()), this, SLOT(start()));
    15. connect(ui->bt_stop, SIGNAL(clicked()), this, SLOT(stop()));
    16. connect(ui->bt_reset, SIGNAL(clicked()), this, SLOT(reset()));
    17.  
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::start(){
    26. timer->start(1000);
    27. }
    28.  
    29. << And the other functions >>
    To copy to clipboard, switch view to plain text mode 

    It seems the constructor doesn't get loaded properly, becausein the GUI hours/minutes/seconds = 0, while I've set them on different numbers.

    Can anyone tell me what I'm doing wrong? Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Buttons on GUI don't work (No Such Slot error)

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTimer>
    6. #include "ui_mainwindow.h"
    7. #include <QTGui>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16. public:
    17. QTimer *timer;
    18. int hours;
    19. int minutes;
    20. int seconds;
    21. explicit MainWindow(QWidget *parent = 0);
    22. ~MainWindow();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26.  
    27. protected slots: // <-- Do not forget to define slots as slots. You can also define them in public
    28. void count();
    29. void start();
    30. void stop();
    31. void reset();
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Buttons on GUI don't work (No Such Slot error)

    Thanks! The buttons are working now

    I noticed the GUI still isn't initialized the wanted I intended. When it starts the timer shows 0:0:0 while I specified certain values. So is there a way to refresh the window from inside the constructor of a function?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Buttons on GUI don't work (No Such Slot error)

    Call your count() method or write code similar to it that will do what you want. I don't know what you count() slot does so it's hard to suggest anything specific.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Buttons on GUI don't work (No Such Slot error)

    oh of course....

    count() has
    Qt Code:
    1. ui->lcdseconds->display(seconds);
    2. ui->lcdminutes->display(minutes);
    3. ui->lcdhours->display(hours);
    To copy to clipboard, switch view to plain text mode 
    So I just added it to the constructor as well, and now it works like a charm

    Thanks for the quick responses. I go watch a few more tutorials now

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Buttons on GUI don't work (No Such Slot error)

    Actually I suggest you do the main tutorial available in Qt docs instead of watching those video tutorials on YouTube. Their quality is... well.. not that good. You'll learn much more from the official tutorial.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 12th June 2010, 02:21
  2. C#, Qt, Qyoto - buttons work sometimes
    By linuxoid in forum Newbie
    Replies: 0
    Last Post: 14th February 2010, 06:33
  3. Slot doesn't seem to work
    By waynew in forum Newbie
    Replies: 3
    Last Post: 10th November 2009, 09:54
  4. minimize slot does not work in linux.Is it a Bug in Qt?
    By anupamgee in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2009, 08:18
  5. copy() slot doesn't work
    By Macok in forum Qt Programming
    Replies: 3
    Last Post: 14th February 2009, 10:55

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
  •  
Qt is a trademark of The Qt Company.