-2

I have a question how to override the signal in Qt?

I have redefined QCheckBox() and changed stateChanged signal.

The project is bilding and working. It does not output in the "application output" errors or messages of the " signal with the slot is not connected"

BUT it doesn't link to the slot. I can't figure out what's wrong.


This code works :

  connect(test_checkbox[i], SIGNAL(stateChanged(int)), two_cl , SLOT(run_NEW())); 

I need to emit string in addition to the number :

  connect(test_checkbox[i], SIGNAL(stateChanged(int, QString)), two_cl , SLOT(run_NEW(int, QString)));  

override QCheckBox

.h

#ifndef MYDIMASCHECKBOX_H
#define MYDIMASCHECKBOX_H

#include <QCheckBox>

class MyDimasCheckBox : public QCheckBox
{
    Q_OBJECT
public:
    MyDimasCheckBox(QWidget *parent =0);
    ~MyDimasCheckBox();

    QString stroka;
signals:
    void stateChanged(int, QString);
};

#endif // MYDIMASCHECKBOX_H

.cpp

#include "mydimascheckbox.h"

MyDimasCheckBox::MyDimasCheckBox(QWidget *parent)
{
    stroka = "dimasik :3";
    emit stateChanged(int(), stroka);
}
MyDimasCheckBox::~MyDimasCheckBox()
{

}

And here is where challenge

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QDebug>

#include <QThread>

#include <QCoreApplication>
#include <iostream>
#include <vector>

#include "mydimascheckbox.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

    int glob_i ;
    int glob_flow;
    int vector_schet; 

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

public slots:
    void start_sending(bool); 

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;

    QThread *thread = new QThread();
    QVector<QThread*> vector_thread;

    QList<MyDimasCheckBox*> test_checkbox;   
    MyDimasCheckBox *checkBox = new MyDimasCheckBox();       

     QWidget *checkBoxWidget = new QWidget();
     QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget);
};

class NewsThread: public QThread
{
    Q_OBJECT
public slots:
    void run_NEW(); 
    void run_NEW(int, QString);  

signals:
    void otprawka (int);
};

#endif // MAINWINDOW_H

.cpp

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    glob_i=0;
    glob_flow =0;
    vector_schet =0;


    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
    test_checkbox.clear();
}

void MainWindow::on_pushButton_clicked()
{
    glob_i++;

    checkBoxWidget = new QWidget(); 
    checkBox = new MyDimasCheckBox();      
    layoutCheckBox = new QHBoxLayout(checkBoxWidget); 
    test_checkbox.append(checkBox);

    connect(checkBox, SIGNAL(toggled(bool)),this, SLOT(start_sending(bool))); 

    checkBox->setText(QString::number(glob_i));

    layoutCheckBox->addWidget(checkBox);            

    layoutCheckBox->setAlignment(Qt::AlignCenter);  
    layoutCheckBox->setContentsMargins(0,0,0,0);    

    ui->tW_test->insertRow(ui->tW_test->rowCount());
    ui->tW_test->setCellWidget(ui->tW_test->rowCount()-1, 1,  checkBoxWidget); 

    qDebug() << "glob_i: " << glob_i;
}

void MainWindow::start_sending(bool Value)
{
    qDebug() << "start_sending  "  ;
    // когда нажата отрабатывает, отжата то не отрабатывает
    if (Value == true)
    {
        NewsThread *two_cl = new NewsThread(); 
        qDebug() << "chekbocks: "<< " TRUE" ;
        for (int i =0;i < test_checkbox.length();i++ )
        {
            if(test_checkbox[i]->isChecked() ==Value)
            {
                glob_flow++;

//                connect(test_checkbox[i], SIGNAL(stateChanged(int)), two_cl , SLOT(run_NEW()));
                connect(test_checkbox[i], SIGNAL(stateChanged(int, QString)), two_cl , SLOT(run_NEW(int, QString))); 

                thread = new QThread(); 
                vector_thread.append(thread);

                vector_schet++;
                qDebug() << "vector_schet : " << vector_schet ;
                two_cl->moveToThread(vector_thread[vector_schet-1]); 
                vector_thread[vector_schet-1]->start();  
            }
        }
    }
    else {
        qDebug() << "chekbocks:" << " False";
        glob_flow--;
        qDebug() << "vector_schet : " << vector_schet ;
        vector_thread[vector_schet-1]->exit();
    }
}

void NewsThread::run_NEW()
{
    qDebug() << "run_NEW()";
    for(;;){  
        for (int i=0; i<500; i++){
            qDebug()<< "Число :" << i <<"number \"flow\" :"  ;
            usleep(100000);
        }
    }
}

void NewsThread::run_NEW(int i, QString str){
    qDebug() << "run_NEW(int i, QString str) ";
    for(;;){  
        for (int i=0; i<500; i++){
            qDebug() << " i : " << i;
            qDebug() << " str : " << str;
            qDebug()<< "Число :" << i <<"number \"flow\" :"  ;
            usleep(100000);
        }
    }
}
2
  • 2
    Well, let's do this again here. You emit your new signal in MyDimasCheckBox constructor and you call connect after constructing a checkbox. So signal emits correctly, but at that moment it is connected to nothing, so nothing happens Jun 26, 2018 at 10:32
  • 1
    Possible duplicate of How to process signals in a Qt subclass? Jun 26, 2018 at 20:47

2 Answers 2

7

You cannot replace a signal with another signal in a subclass. You can, however, emit an additional signal with the original signal in a self-connected slot:

class MyDimasCheckBox : public QCheckBox
{
    Q_OBJECT
public:
    MyDimasCheckBox(QWidget *parent =0);
    ~MyDimasCheckBox();

    QString stroka;
private slots:
    // Emits the new signal
    void doEmitStateChanged(int i);

signals:
    void stateChanged(int, QString);
};

MyDimasCheckBox::MyDimasCheckBox(QWidget *parent) : QCheckBox(parent) {
    // Connect original signal to slot 
    connect(this, SIGNAL(stateChanged(int)), this, SLOT(doEmitStateChanged(int)));
}

void MyDimasCheckBox::doEmitStateChanged(int i) { 
    emit stateChanged(i, stroka); 
}

With the new connection syntax, you can omit the slot and use a lambda:

connect(this, qOverload<int>(&QCheckBox::stateChanged), 
        // "this" context-object for QThread-Affinity
        this, [=](int i) { emit this->stateChanged(i, this->stroka); });
10
  • Technically it's not a replacement, just adding a new signal with the same name and different signature. Note that you can't use new connect syntax with the same signal name. Jun 26, 2018 at 10:43
  • 2
    @BeardedBeaver Really? I haven't tried but I would expect it to work just fine so long as you disambiguate using something like qOverload or a suitable static_cast.
    – G.M.
    Jun 26, 2018 at 10:55
  • 1
    @BeardedBeaver you can, it's just ugly pre Qt 5.7
    – Caleth
    Jun 26, 2018 at 10:56
  • Yes indeed you can, I totally forgot about this Jun 26, 2018 at 10:57
  • 1
    Where do you connect? QObject::connect: No such slot NewsThread::doEmitStateChanged(int). This cannot be if you're using my code, as I never used NewsThread
    – king_nak
    Jun 26, 2018 at 11:44
1

Overriding signal is a bad practice [1]:

"APIs with overridden signals are hard to use, unexpected and bug-prone. To make it worse, Qt even allows you to override a signal with a non-signal, and vice-versa."

https://www.kdab.com/nailing-13-signal-slot-mistakes-clazy-1-3/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.