如何覆盖qt中的信号? [英] How to override a signal in qt?

查看:108
本文介绍了如何覆盖qt中的信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,如何在 Qt 中覆盖信号?

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

我重新定义了 QCheckBox() 并更改了 stateChanged 信号.

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.

此代码有效:

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

<小时>

覆盖 QCheckBox


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()
{

}

挑战就在这里

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

推荐答案

不能用子类中的另一个信号替换一个信号.但是,您可以在自连接插槽中使用原始信号发出附加信号:

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

使用新的连接语法,您可以省略插槽并使用 lambda:

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

这篇关于如何覆盖qt中的信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆