Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom Checkbox



  • ====================================
    Header

    class QCustomToggle : public QCheckBox
    {
    	Q_OBJECT
    public:
    	QCustomToggle(QWidget* parent = Q_NULLPTR);
    	~QCustomToggle();
    private:
    	Ui::QCustomToggle	ui;
    	QCheckBox*			m_pCheckbox;
    public slots:
    	void ClickedChecked();
    protected:
    	void	paintEvent(QPaintEvent* e);
    };
    

    cpp

    QCustomToggle::QCustomToggle(QWidget *parent)
    {
    	ui.setupUi(this);
    
    	m_pCheckbox = this;
    	this->setFixedSize(60, 28);
    	this->setCursor(Qt::PointingHandCursor);
    
    	this->connect(m_pCheckbox, SIGNAL(&QCheckBox::stateChanged(int)), this, SLOT(ClickedChecked()));
    }
    
    QCustomToggle::~QCustomToggle()
    {
    }
    
    void QCustomToggle::paintEvent(QPaintEvent* event)
    {
    	QPainter pPaint(this);
    	pPaint.setRenderHint(QPainter::Antialiasing);
    	pPaint.setPen(Qt::NoPen);
    
    	QColor bgcolor("#777");
    	QColor circlecolor("#DDD");
    	QColor activecolor("#00BCff");
    
    	QRect rect = QRect(0, 0, 60, 28);
    	pPaint.setBrush(bgcolor);
    	pPaint.drawRoundedRect(0, 0, rect.width(), 28, 28 / 2, 28 / 2);
    }
    
    
    void QCustomToggle::ClickedChecked()
    {
    
    }
    

    I'm trying to create a custom widget.
    So I inherited the checkbox, and when I clicked, I tried to signal a change in status, but no signal pops up.

    How can I send a status change signal?


  • Lifetime Qt Champion

    @IknowQT said in Custom Checkbox:

    this->connect(m_pCheckbox, SIGNAL(&QCheckBox::stateChanged(int)), this, SLOT(ClickedChecked()));

    This is wrong- there is no such signal "&QCheckBox::stateChanged(int))". connect() returns false (which you can check) and you will get a warning about this during runtime. Please read the documentation and better use the new signal/slot syntax.

    And please properly format your code with the code-tags for better readability.


Log in to reply