Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c++ - How to override a signal in qt?

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);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...