Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget + C++ Class Controller --> POINTER PROBLEMS?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcbasso
    wrote on 2 Jul 2012, 22:20 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on 2 Jul 2012, 22:31 last edited by
      #2

      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!

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 3 Jul 2012, 06:19 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dcbasso
          wrote on 3 Jul 2012, 11:54 last edited by
          #4

          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!

          1 Reply Last reply
          0

          1/4

          2 Jul 2012, 22:20

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved