[SOLVED] cannot catch a signal



  • @
    #ifndef MAINWINDOWS_H
    #define MAINWINDOWS_H

    #include <QObject>
    #include <QWidget>
    #include <QPushButton>

    class MainWindows : public QWidget
    {
    Q_OBJECT
    public:
    explicit MainWindows(QWidget *parent = 0);

    private:
    QPushButton *ingrandimento;
    QPushButton *rimpicciolimento;

    signals:

    private slots:
    void updateAll(int newIngrandimento);
    };

    #endif // MAINWINDOWS_H
    @

    @

    include "mainwindows.h"

    #include "guida.h"
    #include "programsettings.h"
    #include <QVBoxLayout>
    #include <QDebug>

    MainWindows::MainWindows(QWidget *parent) :
    QWidget(parent)
    {
    this->setWindowFlags(Qt::FramelessWindowHint);

    QVBoxLayout *left = new QVBoxLayout(this);
    ingrandimento = new QPushButton("Non vedo!!!");
    QFont font("Calibri", 26);
    font.setBold(true);
    ingrandimento->setFont(font);
    rimpicciolimento = new QPushButton("Porto gli\nocchiali,\nma ci\nvedo\nancora!!");
    QFont font1("Calibri", 24);
    font1.setBold(true);
    rimpicciolimento->setFont(font1);
    left->addWidget(ingrandimento);
    left->addWidget(rimpicciolimento);
    QObject::connect(ingrandimento, SIGNAL(clicked()), new ProgramSettings(), SLOT(addIngrandimento()));
    QObject::connect(rimpicciolimento, SIGNAL(clicked()), new ProgramSettings(), SLOT(reduceIngrandimento()));
    QObject::connect(new ProgramSettings, SIGNAL(ingrandimentoChanged(int)), this, SLOT(updateAll(int)));
    

    }

    void MainWindows::updateAll(int newIngrandimento)
    {
    qDebug("updateAll");
    ingrandimento->resize(newIngrandimento * 100 + 100, 100 * newIngrandimento + 100);
    rimpicciolimento->resize(newIngrandimento * 100 + 100, 100 * newIngrandimento + 100);
    qDebug()<<"newIngrandimento: " + newIngrandimento;
    }
    @

    signal comes from:

    @
    #ifndef PROGRAMSETTINGS_H
    #define PROGRAMSETTINGS_H

    #include <QObject>
    #include <QRect>

    class ProgramSettings : public QObject
    {
    Q_OBJECT

    public:
    ProgramSettings(QObject *parent = 0);

    static int getIngrandimento();
    void setIngrandimento(const int nvalue = 1);
    static QRect getDesktopDims();
    static QSize getDesktopDimsAsQSize();
    static int getDesktopWidth();
    static int getDesktopHeight();
    

    signals:
    void ingrandimentoChanged(int nvalue);

    public slots:
    void addIngrandimento();
    void reduceIngrandimento();
    };

    #endif // PROGRAMSETTINGS_H
    @

    @
    #include "programsettings.h"

    #include <QSettings>
    #include <QVariant>
    #include <QDesktopWidget>
    #include <messaggioantipanico.h>
    #include <QDebug>

    ProgramSettings::ProgramSettings(QObject *parent) : QObject(parent)
    {
    }

    int ProgramSettings::getIngrandimento()
    {
    QSettings ss("SeiFreud", "Tecnophobie");
    int i = ss.value("ingrandimento", QVariant::fromValue(1)).toInt();
    return i;
    }

    void ProgramSettings::setIngrandimento(const int nvalue)
    {
    if(nvalue > 0 && nvalue < 4)
    {
    QSettings s("SeiFreud", "Tecnophobie");
    s.setValue("ingrandimento", QVariant::fromValue(nvalue));

        emit ingrandimentoChanged(nvalue);
    }
    else
    {
        MessaggioAntiPanico a("Non è possibile cambiare ulteriormente l' ingrandimento!");
        a.show();
    }
    

    }

    QRect ProgramSettings::getDesktopDims()
    {
    QDesktopWidget * d = new QDesktopWidget();
    return d->screenGeometry(-1);
    }

    QSize ProgramSettings::getDesktopDimsAsQSize()
    {
    QDesktopWidget * d = new QDesktopWidget();
    return QSize(d->width(), d->height());
    }

    int ProgramSettings::getDesktopHeight()
    {
    return ProgramSettings::getDesktopDims().width();
    }

    int ProgramSettings::getDesktopWidth()
    {
    return ProgramSettings::getDesktopDims().height();
    }

    void ProgramSettings::addIngrandimento()
    {
    this->setIngrandimento(this->getIngrandimento() + 1);
    }

    void ProgramSettings::reduceIngrandimento()
    {
    this->setIngrandimento(this->getIngrandimento() - 1);
    @

    why does not my PC handle "ingrandimentoChanged" and connect it with the relative slot "updateAll"?



  • Hi,

    Try to put your ProgramSettings as a variable of your Mainwindows.
    As here, it could be already destroyed if no reference is keeped by your application.

    dmcr



  • Wonderfull!!! =))



  • I think you have to keep a reference to the new ProgramSettings() that you use to make the connections. You should have :
    @
    #ifndef MAINWINDOWS_H
    #define MAINWINDOWS_H

    #include <QObject>
    #include <QWidget>
    #include <QPushButton>
     
    class MainWindows : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainWindows(QWidget *parent = 0);
       
    private:
        QPushButton *ingrandimento;
        QPushButton *rimpicciolimento;
        ProgramSettings *programSettings;
     
    signals:
       
    private slots:
        void updateAll(int newIngrandimento);
    };
     
    #endif // MAINWINDOWS_H
    

    @
    @
    include "mainwindows.h"

    #include "guida.h"
    #include "programsettings.h"
    #include <QVBoxLayout>
    #include <QDebug>
     
    MainWindows::MainWindows(QWidget *parent) :
        QWidget(parent)
    {
        this->setWindowFlags(Qt::FramelessWindowHint);
     
        QVBoxLayout *left = new QVBoxLayout(this);
        ingrandimento = new QPushButton("Non vedo!!!");
        QFont font("Calibri", 26);
        font.setBold(true);
        ingrandimento->setFont(font);
        rimpicciolimento = new QPushButton("Porto gli\nocchiali,\nma ci\nvedo\nancora!!");
        QFont font1("Calibri", 24);
        font1.setBold(true);
        rimpicciolimento->setFont(font1);
        left->addWidget(ingrandimento);
        left->addWidget(rimpicciolimento);
        programSettings = new ProgramSettings;
        QObject::connect(ingrandimento, SIGNAL(clicked()),programSettings , SLOT(addIngrandimento()));
        QObject::connect(rimpicciolimento, SIGNAL(clicked()), programSettings, SLOT(reduceIngrandimento()));
        QObject::connect(programSettings, SIGNAL(ingrandimentoChanged(int)), this, SLOT(updateAll(int)));
    }
     
    void MainWindows::updateAll(int newIngrandimento)
    {
        qDebug("updateAll");
        ingrandimento->resize(newIngrandimento * 100 + 100, 100 * newIngrandimento + 100);
        rimpicciolimento->resize(newIngrandimento * 100 + 100, 100 * newIngrandimento + 100);
        qDebug()<<"newIngrandimento: " + newIngrandimento;
    }
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.