Return/emit/catch



  • //1
    class Foo : public QObject
    {
        Q_OBJECT
    public:
        explicit Foo(QObject *parent = 0) :
            m_foo(1)
        {}
        ~Foo()
        {}
    
    private:
        int m_foo;
    
    public slots:
        int divideByBar(int bar = 0)
        {
            if(bar == 0)
                return -1;
            return m_foo / bar;
        }
    };
    
    
    //"mainwindow.h"
    #include "foo.h"
    
    class MainWindow
    {
        Q_OBJECT
    public:
        MainWindow(...)
        {
            Foo *foo = new Foo(this);
            int baz = foo->divideByBar();
            if(baz == -1)
                return;
            qDebug() << "---continue---";
        }
        ~Mainwindow()
        {}
    };
    
    
    
    
    
    
    
    
    
    
    
    
    //2
    class Foo : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged)
    public:
        explicit Foo(QObject *parent = 0) :
            m_foo(1)
        {
            connect(this, SIGNAL(fooChanged(int)), this, SLOT(setFoo(int)));
        }
        ~Foo()
        {}
    
        int foo() const;
    
    private:
        int m_foo;
    
    signals:
        void fooChanged(int foo);
    
    public slots:
        void setFoo(int foo)
        {
            setProperty("foo", foo);
        }
    
        int divideByBar(int bar = 0)
        {
            if(bar == 0)
                return -1;
            return m_foo / bar;
        }
    };
    
    //"mainwindow.h"
    #include "foo.h"
    
    class MainWindow
    {
        Q_OBJECT
    public:
        MainWindow(...)
        {
            Foo *foo = new Foo(this);
            foo->setFoo(1);
            // | foo->setProperty("foo", 1); ?
            int baz = foo->divideByBar();
            if(baz == -1)
                return;
            qDebug() << "---continue---";
        }
        ~Mainwindow()
        {}
    };
    
    
    
    
    
    
    
    
    
    
    
    
    //3
    class Foo : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged)
    public:
        explicit Foo(QObject *parent = 0) :
            m_foo(0)
        {}
        ~Foo()
        {}
    
        int foo() const;
    
    private:
        int m_foo;
    
    signals:
        void fooChanged(int foo);
        void bazCreated(int bar);
    
    public slots:
        void setFoo(const int &foo)
        {
            if(foo == m_foo)
                return;
            m_foo = foo;
            emit fooChanged(m_foo);
        }
    
        void divideByBar(int bar = 0)
        {
            if(bar == 0)
            {
                //emit?
                emit bazCreated(-1);
                return;
            }
            //emit?
            emit bazCreated(m_foo / bar);
        }
    };
    
    //"mainwindow.h"
    #include "foo.h"
    
    class MainWindow
    {
        Q_OBJECT
    public:
        MainWindow(...)
        {
            Foo *foo = new Foo(this);
            connect(foo, SIGNAL(bazCreated(int)), this, SLOT(bazCreated(int)));
            foo->divideByBar();
        }
        ~Mainwindow()
        {}
    
    public slots:
        void bazCreated(int baz)
        {
            if(baz == -1)
                return;
    
            "---continue---";
        }
    };
    
    
    
    
    
    
    
    
    
    
    
    
    //4
    class Foo : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int foo MEMBER m_foo NOTIFY fooChanged)
    public:
        explicit Foo(QObject *parent = 0) :
            m_foo(0)
        {}
        ~Foo()
        {}
    
        int foo() const;
    
    private:
        int m_foo;
    
    public slots:
        double divideByBar(int bar = 0)
        {
            double baz = -1;
            if(bar == 0)
                throw baz;
            baz = m_foo / bar;
            return baz;
        }
    };
    
    //"mainwindow.h"
    #include "foo.h"
    
    class MainWindow
    {
        Q_OBJECT
    public:
        MainWindow(...)
        {
            Foo *foo = new Foo(this);
            try
            {
                foo->divideByBar();
            }
            catch(int baz)
            {
                "---continue---";
            }
        }
        ~Mainwindow()
        {}
    };
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What would your question exactly be ?


Log in to reply
 

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