QWidget + C++ Class Controller --> POINTER PROBLEMS?



  • With this HEADER:
    @
    #ifndef CONTROLEMAIN_H
    #define CONTROLEMAIN_H

    #include <QObject>
    #include <QWidget>

    class ControleMain : public QObject
    {
    Q_OBJECT

    private:
    QWidget currentWidget;

    public:
    explicit ControleMain(QObject *parent = 0);
    void setCurrentWidget(QWidget newCurrentWidget);
    QWidget getCurrentWidget();

    signals:

    public slots:

    };

    #endif // CONTROLEMAIN_H
    @

    I'm trying to implement this:

    @
    #include "controlemain.h"
    #include <QWidget>

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

    void ControleMain::setCurrentWidget(QWidget newCurrentWidget)
    {
    currentWidget = newCurrentWidget;
    }
    QWidget ControleMain::getCurrentWidget()
    {
    return currentWidget;
    }
    @

    But I coun't do that, compilantion error:

    QWidget& QWidget::operator=(const QWidget&) is private.

    It's possible to what I want?
    I think that's a problem with pointers, but I have no experience with C++, I'm not able to solve this problem... anyone can explain for me how to fix it or if it's possible to do?

    Thanks all!


  • Moderators

    By having
    @
    private:
    QWidget currentWidget;
    @
    in your class, and by having your getter and setter taking a QWidget, you're basically trying to copy a QWidget object, which is forbidden.

    You probably want to keep a pointer to a QWidget, as in:

    @
    private:
    QWidget *currentWidget; // the * makes it a pointer to a QWidget

    public:
    /// ...
    void setCurrentWidget(QWidget *newCurrentWidget);
    QWidget *getCurrentWidget();
    @

    and

    @
    void ControleMain::setCurrentWidget(QWidget *newCurrentWidget)
    {
    currentWidget = newCurrentWidget;
    }
    QWidget *ControleMain::getCurrentWidget()
    {
    return currentWidget;
    }
    @

    I'd highly recommend studying up on the basics of C++ before jumping in too deep with Qt. It'll make your life a LOT easier!



  • Note that this limitation (copying forbidden) is true for all QObject derived classes. In terms of widgets the logic of this is easy to understand as well: what would you expect to happen if you copy a widget? Where does it end up? You'll quickly come to the conclusion that copying widgets doesn't make any sense at all.

    You could copy around references to widgets though, if you prefer that over pointers.



  • Thanks mlong, thanks again Andre...
    I reading some online documentation and some free books of c++, but I could not understand how to create a class using a getter and setter referenced (it's right?).
    Off course I really think that it's a good idea, but like always my term in sort to show something to my client, and I'm studying and Working at same time...

    Thanks!


Log in to reply
 

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