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. Template signals and multiple inheritance

Template signals and multiple inheritance

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 1.6k Views
  • 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 10 May 2020, 16:41 last edited by Christian Ehrlicher 5 Oct 2020, 16:42
    #2

    You can't derive from two classes which has QObject as base class by design due to the parent child relationship. You have to find another way (e.g. use the second one as member).

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    T 1 Reply Last reply 10 May 2020, 17:05
    2
    • T TrueNova
      10 May 2020, 16:27

      Hello. I am working on my university project and I encountered a few problems, that I need to solve as fast as I can. I have a template class Drawer, which needs QWidget for a constructor and a QObjectSubclass, which contains just a QObject itself.
      drawer.h

      template <class Center, class Orbit>
      class Drawer : QObjectSubclass, QWidget
      {
      public:
          explicit Drawer(Center *centerItem, QWidget *parent = nullptr);
          ~Drawer();
          Center *centerItem;
          std::vector<std::tuple<Orbit*, itemWidget<Orbit>*, double>> planetObjects;
          vector<double> angles;
          vector<itemWidget<Orbit>*> widgets;
          QTimer *animation;
          QPoint *screenSize, *center;
          itemWidget<Center>* centralWidget;
          QLabel *moreInfo;
          int num=0;
          bool isMoreInfo;
          void addElem(Orbit *planet);
          void delElem(double mass);
          void startClock(){
              animation=new QTimer(this);
              connect(animation, SIGNAL(timeout()), this, SLOT(handler())); //problem
              animation->start(16);
          }
      private slots:
          void moreInfoChecker(){
              if(isMoreInfo)
                  moreInfo->hide();
              else
                  moreInfo->show();
              isMoreInfo=!isMoreInfo;
          }
          void handler(){
              //something more
              drawPlanets();
          }
          void drawPlanets();
      };
      

      First problem, that I have, is that connect gives me a "non-static member 'connect' has multiple-base class subobjects", I tried to add virtual to QWidget and/or QObjectSubclass, but still no results.

      And the second problem I will get, is that I will need to return template value to my MainWindow, I tried signals too, but I know, that it is impossible, how to do it properly?
      I will be pleased isа someone explained me how to solve these 2 problems? Maybe, there are some workarounds?

      Maybe, if someone will see any other solutions to these problems, I will be happy too.
      Thank you.

      P Offline
      P Offline
      Pl45m4
      wrote on 10 May 2020, 16:49 last edited by
      #3

      @TrueNova

      In addition, making all members public is not a good style of coding :)

      What type of subclass is your QObjectSubclass?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      T 1 Reply Last reply 10 May 2020, 16:54
      1
      • P Pl45m4
        10 May 2020, 16:49

        @TrueNova

        In addition, making all members public is not a good style of coding :)

        What type of subclass is your QObjectSubclass?

        T Offline
        T Offline
        TrueNova
        wrote on 10 May 2020, 16:54 last edited by
        #4

        Thank you @Pl45m4, I will change it later. And this is my QObjectSubclass. Yes, looks stupid, maybe... I don't know, but consider, that I am new to Qt and OOP in general.

        class QObjectSubclass : virtual public QObject
        {
            Q_OBJECT
        public:
            QObjectSubclass(QObject *parent = 0);
        };
        
        1 Reply Last reply
        0
        • C Christian Ehrlicher
          10 May 2020, 16:41

          You can't derive from two classes which has QObject as base class by design due to the parent child relationship. You have to find another way (e.g. use the second one as member).

          T Offline
          T Offline
          TrueNova
          wrote on 10 May 2020, 17:05 last edited by TrueNova 5 Oct 2020, 17:09
          #5

          @Christian-Ehrlicher, thank you. And sorry, what do you mean by "use the second one as member"? I am bad at English and, well, I am not really good in programming too.
          I need QWidget to make this and, probably, to paint something with QPainter(I don't know if it is actually needed).
          template <class Center, class Orbit>
          Drawer<Center, Orbit>::Drawer(Center *centerItem, QWidget *parent) : QWidget(parent)
          {

          Edit: do you mean to do something like
          class Drawer{
          Qwidget *somethig;
          }

          Then how to do the thing with constructor?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 10 May 2020, 17:07 last edited by
            #6

            You can only paint in the paintEvent() of the widget.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            T 1 Reply Last reply 10 May 2020, 17:18
            0
            • C Christian Ehrlicher
              10 May 2020, 17:07

              You can only paint in the paintEvent() of the widget.

              T Offline
              T Offline
              TrueNova
              wrote on 10 May 2020, 17:18 last edited by
              #7

              @Christian-Ehrlicher yes, I know, thank you, so what can I do to make implement this "use the second one as member" to my class for it to work, can you, please, give me a minimal of code of it?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 10 May 2020, 17:22 last edited by
                #8

                I can't and won't provide code since I don't know what you're doing and what you really want to achieve. I don't see a reason why someone would derive two times from QObject.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                T 1 Reply Last reply 10 May 2020, 17:32
                2
                • C Christian Ehrlicher
                  10 May 2020, 17:22

                  I can't and won't provide code since I don't know what you're doing and what you really want to achieve. I don't see a reason why someone would derive two times from QObject.

                  T Offline
                  T Offline
                  TrueNova
                  wrote on 10 May 2020, 17:32 last edited by TrueNova 5 Oct 2020, 17:33
                  #9

                  @Christian-Ehrlicher I need to make my Drawer class templated, as I know, QObject can't be so, that's why I divided it into my QObjectSubclass. Also, I need to inherit QWidget(parent) to my constructor, so I derrived my Drawer class from it too. That is why(I am pretty sure) I have this problem with connect() — "non-static member 'connect' has multiple-base class subobjects". Hope that will help you to understand my problem.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 10 May 2020, 17:41 last edited by
                    #10

                    @TrueNova said in Template signals and multiple inheritance:

                    Hope that will help you to understand my problem.

                    I understand your problem but you have to fix it another way - you can't derive two times from QObject (as already said) and templates + QObject won't work too so again - you have to find another solution.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    3
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 10 May 2020, 18:03 last edited by
                      #11

                      Hi,

                      You are explaining the problems you are having. Can you explain exactly what you are trying to achieve ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      T 1 Reply Last reply 10 May 2020, 18:44
                      2
                      • S SGaist
                        10 May 2020, 18:03

                        Hi,

                        You are explaining the problems you are having. Can you explain exactly what you are trying to achieve ?

                        T Offline
                        T Offline
                        TrueNova
                        wrote on 10 May 2020, 18:44 last edited by TrueNova 5 Oct 2020, 19:12
                        #12

                        @SGaist I have classes ierarchy like tree: Galaxy->Star->Planet->Satellite and Drawer class for each one of level. I am drawing, for example, a solar system(Sun and planets on their orbits), also, I want to draw planet system(planet and its satellites)of planets of this solar system. User is in the solar system with star in the center and planets are orbiting it, the user clicks on Earth, and now Earth appers on the Screen with Moon on the orbit. I have classes to show everything on the screen for each my type, and everything works well, but I need to be able to make everything with just one template class(as all of these classes are just almost nearly identical). As my class is template, I divided a QObject into another class and then inherited it in Drawer class(found this solution on Qt Forum), but also I need to be able to send signals from this class. Here I got error message from connect().
                        If you are intend to help my seriously, cause this is a really hard problem, I guess, thank you. Then this may help you: my main goal is to send some signal with undefined type(Planet-, Star-, Satellite pointers) from my template class, when user clicks on orbit item(Planet, for example). Only thing that I need, is to catch this signal from MainWindow and than put the item(this Planet), that signal sent as the central system with its orbits(Satellites) on the screen, how to achieve this goal? Cause I think, there will be a lot of problems with emitting the templated type. Are there any possible way to do, what I need at all? Thank you.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 10 May 2020, 19:19 last edited by
                          #13

                          What message do you want to send ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          T 1 Reply Last reply 10 May 2020, 19:22
                          0
                          • S SGaist
                            10 May 2020, 19:19

                            What message do you want to send ?

                            T Offline
                            T Offline
                            TrueNova
                            wrote on 10 May 2020, 19:22 last edited by TrueNova 5 Oct 2020, 19:24
                            #14

                            @SGaist I want to do something like

                            template<typename T>
                            //something
                            signals:
                                  sendItem( T* item);
                            connect(item, SIGNAL(clicked()), this, SlOT(itemClicked()));
                            itemClicked(){
                                 emit sendItem(currentItem);
                            } 
                            
                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 10 May 2020, 19:52 last edited by
                              #15

                              Well, looks like you are currently trying to re-implement the graphics view framework. You really should consider using it.

                              On a side note, AFAIR, QObject based template class are currently not supported.

                              That said, you are trying to make your planetary objects and those related to them way too smart. If you want to keep your full custom approach, you should rather give them a render method that you will call from the "main" widget paintEvent rather than trying to make each of them a full blown widget.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              T 1 Reply Last reply 10 May 2020, 20:15
                              2
                              • S SGaist
                                10 May 2020, 19:52

                                Well, looks like you are currently trying to re-implement the graphics view framework. You really should consider using it.

                                On a side note, AFAIR, QObject based template class are currently not supported.

                                That said, you are trying to make your planetary objects and those related to them way too smart. If you want to keep your full custom approach, you should rather give them a render method that you will call from the "main" widget paintEvent rather than trying to make each of them a full blown widget.

                                T Offline
                                T Offline
                                TrueNova
                                wrote on 10 May 2020, 20:15 last edited by
                                #16

                                @SGaist I think I understand your vision on this task. And one more time I will review graphics view framework, but somehow it seems too hard for me now. Thank you!

                                1 Reply Last reply
                                0

                                11/16

                                10 May 2020, 18:03

                                • Login

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