PDA

View Full Version : Error using Signals and Slots with qcheckbox



rookee
10th November 2015, 22:36
Hi,

I'm having the following error while using Signals and Slots with QCheckBox. The following is my code snippet.

In my mainwindow.cpp file:

void MainWindow::on_checkBox_clicked()
{
if(ui->checkBox->isChecked())
{
connect(ui->checkBox, SIGNAL(stateChanged(int)), ui->pushButton_5, SLOT(setEnabled(true)));
}
}


It underlines the connect() line in red when I use 3 )'s followed by ; and when I remove one ) and compile it, I get an error saying "expected ')' before ';' token". I'm not able to figure out which part I'm making a mistake. Please help. Thanks in advance.

prasad_N
11th November 2015, 06:16
Please ask Qt related questions here.

Check your other part of the code also, And in slot the argument should not be true, It should be SLOT(SetEnabled(int))

anda_skoa
11th November 2015, 08:43
1) Are you certain that you want to create a new connection every time the checkbox gets checked?
2) As prasad_N mentioned, the argument types of the signal and the slot don't match. Maybe you wanted to use the toggled(bool) signal?

Cheers,
_

rookee
11th November 2015, 14:56
Thanks for your response prasad_N and anda_skoa. I'm so sorry I'm new to Qt and programming and thought it was Qt related. Could you please help me any way, I had few questions earlier and got very quick response so I instinctively posted it here.

Here's my requirement:
If the user wants to start logging data, user should check the checkBox which in turn enables the Save button letting the user create a log file. If the user wants to stop logging data, he should simply uncheck the checkBox.

I tried your suggestions, the error seems to be fixed but it doesn't enable the save button.

anda_skoa
11th November 2015, 16:09
Not sure why prasad_N wrote the bit about not being Qt related, I would say it clearly is.

If you just want the button's enable state to follow the checkbox's checked state, then just connect the checkbox to the button


connect(checbox, SIGNAL(toggled(bool)), button, SLOT(setEnabled(bool)));

You can even do this in Designer, using the signal/slot mode.

Cheers,
_

rookee
11th November 2015, 17:43
It worked. Awesome!! Thanks a lot anda_skoa!!

Added after 58 minutes:

I have one more tiny bit of an issue with this. When the application is launched, when I check the checkbox for the first time the Save button doesn't get enabled. It does toggle after the first time. Please advise on how to fix it.

stampede
11th November 2015, 19:19
When the application is launched, when I check the checkbox for the first time the Save button doesn't get enabled
Does the initial state of the button and checkbox match ? Can you show some more code ?

rookee
11th November 2015, 21:53
On the Qdesigner I unchecked the enable checkbox for the pushbutton.

Here's the code
In mainwindow.cpp file


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QtGui>
#include <QWidget>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QFileDialog>
#include "mainwindow.h"
#include "globals.h"
#include "globals.cpp"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

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


void MainWindow::on_checkBox_clicked()
{

if(ui->checkBox->isChecked())
{
//ui->pushButton_5->setEnabled(true);
connect(ui->checkBox, SIGNAL(toggled(bool)), ui->pushButton_5, SLOT(setEnabled(bool)));
}
}


In mainwindow.h file


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QMainWindow>
#include <QObject>
#include "wiringPi.h"
#include <QTimer>
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_checkBox_clicked();

private:

Ui::MainWindow *ui;
/*void saveFile();
QString curFile;*/
};

#endif // MAINWINDOW_H

prasad_N
12th November 2015, 04:21
Your are doing it in wrong way.
Put your connect function as it is in mainwindow constructor.
And remove your on_checkbox_clicked().

As you are un checking checkbox, You need to disable pushbutton also initially in designer or in mainwindow constructor.

N small suggestion: you can give proper names to variables in designer itself insted of using deafult names like pushbutton_1 pushbutton_2 l.

rookee
16th November 2015, 15:26
Did you mean the below prasad_N? I unchecked the enable checkbox that belongs to pushbutton_5. And the initial state of the checkbox is unchecked.

mainwindow.cpp file

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QtGui>
#include <QWidget>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QFileDialog>
#include "mainwindow.h"
#include "globals.h"
#include "globals.cpp"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
connect(ui->checkBox, SIGNAL(toggled(bool)), ui->pushButton_5, SLOT(setEnabled(bool)));
delete ui;
}


/*void MainWindow::on_checkBox_clicked()
{

if(ui->checkBox->isChecked())
{
//ui->pushButton_5->setEnabled(true);
connect(ui->checkBox, SIGNAL(toggled(bool)), ui->pushButton_5, SLOT(setEnabled(bool)));
}
}*/

mainwindow.h file



#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QMainWindow>
#include <QObject>
#include "wiringPi.h"
#include <QTimer>
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
//void on_checkBox_clicked();

private:

Ui::MainWindow *ui;
/*void saveFile();
QString curFile;*/
};

#endif // MAINWINDOW_H

d_stranz
16th November 2015, 16:25
No. It makes no sense at all to make a slot connection in the destructor for your MainWindow class. The MainWindow instance is being destroyed and is going out of scope.

If you want two GUI elements to have the same state when you application starts then you either:

1 - Set them to the same state in Qt Designer (i.e. set the checkbox state to unchecked and the button to disabled) or
2 - Set the state in the MainWindow constructor after calling setupUi() then add the connection between the checkbox's toggled() signal and the button's setEnabled() slot:

ui->setupUi();
ui->checkbox->setChecked( false );
ui->button->setEnabled( false );
connect( ui->checkbox, SIGNAL( toggled(bool) ), ui->button, SLOT( setEnabled(bool) ) );

I think you are confused because you might be thinking that when you call connect() that that will somehow cause the button state to change. The connect() doesn't do anything except set up a response for something that might happen in the future - after making the connection, the next time the check box state changes, the connection will cause the push button state to change in response.

A connect() is nothing more than tying two things together with a piece of string. If you have two things that aren't connected, you can move one of them and nothing happens to the other. If you tie them together with a piece of string, still nothing happens until the next time you move one of the objects.

rookee
17th November 2015, 20:05
Now I understand. Thanks d_stranz. Thanks prasad_N.