简体   繁体   English

Qt中不会发出新的SIGNAL

[英]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. 我已经将这段代码编写为具有QSlider和QLineEdit的QWidget,它们的值相互连接。 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. 但是我添加了一个新的SIGNAL,当值达到80时必须将其发出。此后,必须关闭QAPPlication的实例。 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: 我是如此愚蠢,在sld中未声明到达(),这是Windows中的信号,因此它不会从sld连接,应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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