简体   繁体   中英

new SIGNAL in Qt won't be emitted

I've written this piece of the code A QWidget which has a QSlider and QLineEdit They are connected to each other with their value. there are two new SLOTs which will convert their values and will call other widget to change its value. until here, everything works perfectly.

But I have added a new SIGNAL, which must be emmited when the value reaches 80. And after that the instance of the QAPPlication must be closed. This part doesn't work. Why?

#include "windows.h"
#include <QSlider>
#include <QLineEdit>
#include <QGridLayout>
#include <QApplication>
windows::windows(QWidget *parent) :
    QWidget(parent)
{
    sld=new QSlider(Qt::Horizontal,this);
    sld->setRange(0,100);
    led= new QLineEdit(this);
    QGridLayout *grid=new QGridLayout(this);
    grid->addWidget(sld,0,0);
    grid->addWidget(led,0,1);
    connect(led,SIGNAL(textEdited(QString)),this,SLOT(setSlider(QString)));
    connect(sld,SIGNAL(valueChanged(int)),this,SLOT(setLed(int)));
    connect(sld,SIGNAL(reached()),QApplication::instance(),SLOT(quit()));
}

void windows::setSlider(QString value)
{
    int intValue=value.toInt();
    sld->setValue(intValue);
    if(intValue>80)
        emit reached();
}

void windows::setLed(int value)
{
    QString Qvalue=QString::number(value);
    led->setText(Qvalue);
    if(value>80)
        emit reached();
}

I'm such a stupid, reached() is not declared in sld, it's a SIGNAL in windows and then it won't be connected from sld, it should be like this:

connect(this,SIGNAL(reached()),QApplication::instance(),SLOT(quit()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM