Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Catching signal of checkbox to do an action

Tags:

c++

qt

In my Qt program, I want to catch the signal of one of my checkbox to know if it's checked or not. I did it :

#include <iostream>
#include <QVBoxLayout>
#include <QtGui>
#include "Window.h"

Window::Window() : QWidget()
{
  setFixedSize(400, 800);

  m_bouton = new QPushButton("Module 1");
  m_bouton->setCursor(Qt::PointingHandCursor);

  m_bouton2 = new QPushButton("Module 2");
  m_bouton2->setCursor(Qt::PointingHandCursor);

  m_bouton3 = new QPushButton("Module 3");
  m_bouton3->setCursor(Qt::PointingHandCursor);

  m_bouton4 = new QPushButton("Module 4");
  m_bouton4->setCursor(Qt::PointingHandCursor);

  m_check4 = new QCheckBox(this);
  m_check4->move(300, 50);
  m_check4->setChecked(true);

  m_check3 = new QCheckBox(this);
  m_check3->move(200, 50);
  m_check3->setChecked(true);

  m_check2 = new QCheckBox(this);
  m_check2->move(100, 50);
  m_check2->setChecked(true);

  m_check1 = new QCheckBox(this);
  m_check1->move(0, 50);
  m_check1->setChecked(true);

  QVBoxLayout *layout = new QVBoxLayout;
  layout->addWidget(m_bouton);
  layout->addWidget(m_bouton2);
  layout->addWidget(m_bouton3);
  layout->addWidget(m_bouton4);
  this->setLayout(layout);
  this->setStyleSheet("border: 1px solid black; border-radius: 10px;");

  this->setWindowOpacity(0.5);
  QWidget::setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
  QWidget::setWindowTitle("Monitor Core");
  QObject::connect(m_bouton4, SIGNAL(clicked()), this, SLOT(SizeCHange()));
  QObject::connect(m_check4, SIGNAL(clicked()), this, SLOT(SizeCHange()));
}

void    Window::SizeCHange()
{
  setFixedSize(400, 800 - 200);
  m_bouton4->setVisible(false);
}

and this is my .h :

#ifndef _WINDOW_H_
#define _WINDOW_H_

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QCheckBox>

class Window : public QWidget
{
  Q_OBJECT

 public:
  Window();

  public slots:
    void SizeCHange();

 private:
    QPushButton *m_bouton;
    QPushButton *m_bouton2;
    QPushButton *m_bouton3;
    QPushButton *m_bouton4;
    QCheckBox *m_check1;
    QCheckBox *m_check2;
    QCheckBox *m_check3;
    QCheckBox *m_check4;
};

#endif

This is working but just one time. I want to know if it's check or not, then I will be able to resize my window and make something invisible as I want.

like image 704
Michael003 Avatar asked Jan 23 '16 22:01

Michael003


1 Answers

You simply need to use a bool parameter, which indicates the state of the checkbox:

public slots:
    void SizeCHange(bool checked); //using bool parameter

//...
QObject::connect(m_check4, SIGNAL(clicked(bool)), this, SLOT(SizeCHange(bool)));
//...
void Window::SizeCHange(bool checked)
{
    if(checked)
    {
        setFixedSize(400, 800 - 200);
    }
    else
    {
        // removing fixed size
        setMinimumSize(QSize(0, 0));
        setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
    }
    m_bouton4->setVisible(checked);
}

The docs for a signal clicked()

like image 131
Vladimir Bershov Avatar answered Oct 12 '22 23:10

Vladimir Bershov