Results 1 to 12 of 12

Thread: Error using Signals and Slots with qcheckbox

  1. #1
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Error using Signals and Slots with qcheckbox

    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:
    Qt Code:
    1. void MainWindow::on_checkBox_clicked()
    2. {
    3. if(ui->checkBox->isChecked())
    4. {
    5. connect(ui->checkBox, SIGNAL(stateChanged(int)), ui->pushButton_5, SLOT(setEnabled(true)));
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error using Signals and Slots with qcheckbox

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

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error using Signals and Slots with qcheckbox

    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,
    _

  4. #4
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Error using Signals and Slots with qcheckbox

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error using Signals and Slots with qcheckbox

    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
    Qt Code:
    1. connect(checbox, SIGNAL(toggled(bool)), button, SLOT(setEnabled(bool)));
    To copy to clipboard, switch view to plain text mode 
    You can even do this in Designer, using the signal/slot mode.

    Cheers,
    _

  6. #6
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Error using Signals and Slots with qcheckbox

    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.
    Last edited by rookee; 11th November 2015 at 17:43.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Error using Signals and Slots with qcheckbox

    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 ?

  8. #8
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Error using Signals and Slots with qcheckbox

    On the Qdesigner I unchecked the enable checkbox for the pushbutton.

    Here's the code
    In mainwindow.cpp file
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QApplication>
    4. #include <QtGui>
    5. #include <QWidget>
    6. #include <QFile>
    7. #include <QString>
    8. #include <QTextStream>
    9. #include <QFileDialog>
    10. #include "mainwindow.h"
    11. #include "globals.h"
    12. #include "globals.cpp"
    13.  
    14.  
    15. MainWindow::MainWindow(QWidget *parent) :
    16. QMainWindow(parent),
    17. ui(new Ui::MainWindow)
    18. {
    19. ui->setupUi(this);
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27.  
    28. void MainWindow::on_checkBox_clicked()
    29. {
    30.  
    31. if(ui->checkBox->isChecked())
    32. {
    33. //ui->pushButton_5->setEnabled(true);
    34. connect(ui->checkBox, SIGNAL(toggled(bool)), ui->pushButton_5, SLOT(setEnabled(bool)));
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    In mainwindow.h file
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5. #include <QMainWindow>
    6. #include <QObject>
    7. #include "wiringPi.h"
    8. #include <QTimer>
    9. #include <QMainWindow>
    10.  
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22.  
    23. private slots:
    24. void on_checkBox_clicked();
    25.  
    26. private:
    27.  
    28. Ui::MainWindow *ui;
    29. /*void saveFile();
    30.   QString curFile;*/
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 12th November 2015 at 08:07. Reason: missing [code] tags

  9. #9
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error using Signals and Slots with qcheckbox

    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.
    Last edited by prasad_N; 12th November 2015 at 06:32.
    Thanks :-)

  10. #10
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Error using Signals and Slots with qcheckbox

    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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QApplication>
    4. #include <QtGui>
    5. #include <QWidget>
    6. #include <QFile>
    7. #include <QString>
    8. #include <QTextStream>
    9. #include <QFileDialog>
    10. #include "mainwindow.h"
    11. #include "globals.h"
    12. #include "globals.cpp"
    13.  
    14.  
    15. MainWindow::MainWindow(QWidget *parent) :
    16. QMainWindow(parent),
    17. ui(new Ui::MainWindow)
    18. {
    19. ui->setupUi(this);
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. connect(ui->checkBox, SIGNAL(toggled(bool)), ui->pushButton_5, SLOT(setEnabled(bool)));
    25. delete ui;
    26. }
    27.  
    28.  
    29. /*void MainWindow::on_checkBox_clicked()
    30. {
    31.  
    32.   if(ui->checkBox->isChecked())
    33.   {
    34.   //ui->pushButton_5->setEnabled(true);
    35.   connect(ui->checkBox, SIGNAL(toggled(bool)), ui->pushButton_5, SLOT(setEnabled(bool)));
    36.   }
    37. }*/
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h file

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

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,311
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Error using Signals and Slots with qcheckbox

    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:
    Qt Code:
    1. ui->setupUi();
    2. ui->checkbox->setChecked( false );
    3. ui->button->setEnabled( false );
    4. connect( ui->checkbox, SIGNAL( toggled(bool) ), ui->button, SLOT( setEnabled(bool) ) );
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 16th November 2015 at 16:33.

  12. #12
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Error using Signals and Slots with qcheckbox

    Now I understand. Thanks d_stranz. Thanks prasad_N.

Similar Threads

  1. Replies: 0
    Last Post: 28th October 2014, 17:16
  2. Qt signals/slots dll for non-Qt app
    By bareil76 in forum Qt Programming
    Replies: 3
    Last Post: 24th October 2014, 13:19
  3. Replies: 2
    Last Post: 18th April 2013, 12:15
  4. QT SIGNALS and SLOTS
    By beginQT in forum Newbie
    Replies: 7
    Last Post: 23rd September 2011, 14:40
  5. [SOLVED] Qt: Signals and slots Error: undefined reference to `vtable for
    By TheIndependentAquarius in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2011, 10:51

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.