PDA

View Full Version : Buttons on GUI don't work (No Such Slot error)



Bertus
14th September 2010, 20:07
Hi,

Im pretty new to QT, and I tried following a tutorial on youtube (part1 (http://www.youtube.com/watch?v=mVvB9Yqsigw&feature=related), part2 (http://www.youtube.com/watch?v=JdJJdkbq-Iw&feature=related)) 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:


#include <QtGui/QApplication>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "ui_mainwindow.h"
#include <QTGui>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QTimer *timer;
int hours;
int minutes;
int seconds;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
void count();
void start();
void stop();
void reset();
};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
hours = 1;
minutes = 2;
seconds = 3;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(count()));
connect(ui->bt_start, SIGNAL(clicked()), this, SLOT(start()));
connect(ui->bt_stop, SIGNAL(clicked()), this, SLOT(stop()));
connect(ui->bt_reset, SIGNAL(clicked()), this, SLOT(reset()));

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::start(){
timer->start(1000);
}

<< And the other functions >>


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! :)

tbscope
14th September 2010, 20:10
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "ui_mainwindow.h"
#include <QTGui>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QTimer *timer;
int hours;
int minutes;
int seconds;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

protected slots: // <-- Do not forget to define slots as slots. You can also define them in public
void count();
void start();
void stop();
void reset();
};

#endif // MAINWINDOW_H

Bertus
14th September 2010, 20:58
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?

wysota
14th September 2010, 21:06
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.

Bertus
14th September 2010, 21:11
oh of course....

count() has


ui->lcdseconds->display(seconds);
ui->lcdminutes->display(minutes);
ui->lcdhours->display(hours);
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 ;)

wysota
14th September 2010, 21:13
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.