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. Looking for a good way to make a widget use another widget
QtWS25 Last Chance

Looking for a good way to make a widget use another widget

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 4.0k 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.
  • JonBJ JonB

    @Gilboonet said in Looking for a good way to make a widget use another widget:

    so that the TableWidget can be accessed on the TriangleItem doubleclick event handler.

    Nooo :) Why should an item in a graphics scene know anything about what might be elsewhere in your UI? You should not tightly couple the graphics widget and the table widget.

    In Qt emit a signal from the graphics widget, perhaps with a parameter of the color of the clicked item, and have a slot in the table widget for setting the corresponding row. connect() them, usually in your whole parent widget/window which holds the table & scene widgets.

    without subclassing my scenes ?

    If you're going to write anything other than small or generic, you will want to subclass QGraphicsScene anyway so you can add whatever functionality, so you might as well subclass QGraphicsScene from the get-go, not avoid it.

    GilboonetG Offline
    GilboonetG Offline
    Gilboonet
    wrote on last edited by
    #4

    @JonB I'm starting the signal/slot implementation, but even if I added a signal to my TriangleItem class (subclassing QGraphicsPolygonItem),

    class TriangleItem : public QGraphicsPolygonItem
    {
    public:
        enum { Type = UserType + 1 };
        int type() const override { return(Type);}
        TriangleItem();
        TriangleItem(QColor, QPolygonF, int, int);
        QColor poolColor;
        int id;
        int col;
        bool estPrem = false;
        bool estLie = false;
        void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;
        void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override;
    
    public slots:
    
    signals:
        void changeToColor();
    };
    

    when I emit this signal 'from the class mouseDoubleClickEvent() handler, I have an error :
    compilation output

    undefined reference to TriangleItem::changeToColor(int)
    

    The error is here

    void TriangleItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
    {
        //DeplieurScene *sc = dynamic_cast<DeplieurScene*>(this->scene());
    
        //if (!sc)
        //    return;
    
        //sc->twC->selectRow(this->col);
        emit changeToColor();
    }
    

    and says :

    Emit keyword being used with non-signal TriangleItem::changeToColor [clazy-incorrect-emit]
    

    Adding "Q_OBJECT" at the beginning of the class doesn't help, also adding QObject as ancestor

    Maybe is there an include to add to use signals ?

    jsulmJ 1 Reply Last reply
    0
    • GilboonetG Gilboonet

      @JonB I'm starting the signal/slot implementation, but even if I added a signal to my TriangleItem class (subclassing QGraphicsPolygonItem),

      class TriangleItem : public QGraphicsPolygonItem
      {
      public:
          enum { Type = UserType + 1 };
          int type() const override { return(Type);}
          TriangleItem();
          TriangleItem(QColor, QPolygonF, int, int);
          QColor poolColor;
          int id;
          int col;
          bool estPrem = false;
          bool estLie = false;
          void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;
          void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override;
      
      public slots:
      
      signals:
          void changeToColor();
      };
      

      when I emit this signal 'from the class mouseDoubleClickEvent() handler, I have an error :
      compilation output

      undefined reference to TriangleItem::changeToColor(int)
      

      The error is here

      void TriangleItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
      {
          //DeplieurScene *sc = dynamic_cast<DeplieurScene*>(this->scene());
      
          //if (!sc)
          //    return;
      
          //sc->twC->selectRow(this->col);
          emit changeToColor();
      }
      

      and says :

      Emit keyword being used with non-signal TriangleItem::changeToColor [clazy-incorrect-emit]
      

      Adding "Q_OBJECT" at the beginning of the class doesn't help, also adding QObject as ancestor

      Maybe is there an include to add to use signals ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @Gilboonet Rerun qmake/cmake after adding Q_OBJECT and rebuild.
      In case you use CMake make sure you have this in your CMakeLists.txt file: https://cmake.org/cmake/help/latest/prop_tgt/AUTOMOC.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      GilboonetG 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Gilboonet Rerun qmake/cmake after adding Q_OBJECT and rebuild.
        In case you use CMake make sure you have this in your CMakeLists.txt file: https://cmake.org/cmake/help/latest/prop_tgt/AUTOMOC.html

        GilboonetG Offline
        GilboonetG Offline
        Gilboonet
        wrote on last edited by
        #6

        @jsulm I added

        set_property(TARGET Deplieur PROPERTY AUTOMOC ON)
        

        But it was not necessarily needed as there was already this parameter set :

        set(CMAKE_AUTOMOC ON)
        

        Anyway, it still doesn't compile, if I only add Q_OBJECT at the beginning of my class, I have errors starting from

        error: 'staticMetaObject' is not a member of 'QGraphicsPolygonItem'
        

        I tried to make my class co-inherit QObject

        class TriangleItem : public QObject, public QGraphicsPolygonItem
        

        Then it compiles but there's a linking error :

        CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj: in function `QtPrivate::QMetaTypeForType<TriangleItem>::getDefaultCtr()::{lambda(QtPrivate::QMetaTypeInterface const*, void*)#1}::operator()(QtPrivate::QMetaTypeInterface const*, void*) const':
        
        I'm surely doing it wrong.
        
        JonBJ 1 Reply Last reply
        0
        • GilboonetG Offline
          GilboonetG Offline
          Gilboonet
          wrote on last edited by
          #7

          It's too bad that it's so hard to add signal/slot functionnalities to a widget, because it seem very interesting. For the moment I have another solution that works. I subclassed QGraphicsScene, add a pointer to my TableWidget on it, then on MouseDoubleClick event handler of my TriangleItem, I simply do a dynamic cast on its scene to and then use the pointer to the TableWidget to change its row correspondingly to the item's color id.

          1 Reply Last reply
          0
          • GilboonetG Gilboonet

            @jsulm I added

            set_property(TARGET Deplieur PROPERTY AUTOMOC ON)
            

            But it was not necessarily needed as there was already this parameter set :

            set(CMAKE_AUTOMOC ON)
            

            Anyway, it still doesn't compile, if I only add Q_OBJECT at the beginning of my class, I have errors starting from

            error: 'staticMetaObject' is not a member of 'QGraphicsPolygonItem'
            

            I tried to make my class co-inherit QObject

            class TriangleItem : public QObject, public QGraphicsPolygonItem
            

            Then it compiles but there's a linking error :

            CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj: in function `QtPrivate::QMetaTypeForType<TriangleItem>::getDefaultCtr()::{lambda(QtPrivate::QMetaTypeInterface const*, void*)#1}::operator()(QtPrivate::QMetaTypeInterface const*, void*) const':
            
            I'm surely doing it wrong.
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #8

            @Gilboonet
            I forgot class TriangleItem : public QGraphicsPolygonItem means no Object so cannot itself emit signals.

            Either persist with class TriangleItem : public QObject, public QGraphicsPolygonItem which is indeed what will be required (keep the Q_OBJECT, try cleaning everything out and rebuilding from scratch) or do it at the QGraphicsScene level, recognising the click/double-click on the item (e.g. https://stackoverflow.com/questions/26079807/function-getting-clicked-object-from-qgraphicsscene), that can emit a signal.

            It's too bad that it's so hard to add signal/slot functionnalities to a widget

            It's not. Anything that's a QWidget will have signals/slots. The issue here is because a QGraphicsItem is not a QObject. Most of the time signals/slots are easy, and they are very helpful. If you do UIs in Qt you will use signals/slots all the time.

            GilboonetG 1 Reply Last reply
            2
            • JonBJ JonB

              @Gilboonet
              I forgot class TriangleItem : public QGraphicsPolygonItem means no Object so cannot itself emit signals.

              Either persist with class TriangleItem : public QObject, public QGraphicsPolygonItem which is indeed what will be required (keep the Q_OBJECT, try cleaning everything out and rebuilding from scratch) or do it at the QGraphicsScene level, recognising the click/double-click on the item (e.g. https://stackoverflow.com/questions/26079807/function-getting-clicked-object-from-qgraphicsscene), that can emit a signal.

              It's too bad that it's so hard to add signal/slot functionnalities to a widget

              It's not. Anything that's a QWidget will have signals/slots. The issue here is because a QGraphicsItem is not a QObject. Most of the time signals/slots are easy, and they are very helpful. If you do UIs in Qt you will use signals/slots all the time.

              GilboonetG Offline
              GilboonetG Offline
              Gilboonet
              wrote on last edited by
              #9

              @JonB I tried to add QObject (it's on my reply) but then there's a linking error that made me conclude just like you said that QgraphicsItem is not a QObject. It could certainly be done but so I need to avoid using signals on my subclassed QGraphicsPolygonObject and I don't want to lose those abilities (dragging, group selection). I'm going to try at the QGraphicsScene level because I'm sure signals/slots are worth the effort.

              1 Reply Last reply
              0
              • GilboonetG Offline
                GilboonetG Offline
                Gilboonet
                wrote on last edited by Gilboonet
                #10

                With my subclassed QGraphicsScene I have the same linking error :

                [17/20 0,6/sec] Linking CXX executable Deplieur.exe
                FAILED: Deplieur.exe 
                C:\WINDOWS\system32\cmd.exe /C "cd . && C:\Qt\Tools\mingw1310_64\bin\g++.exe -DQT_QML_DEBUG -g -mwindows CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj CMakeFiles/Deplieur.dir/main.cpp.obj CMakeFiles/Deplieur.dir/mainwindow.cpp.obj CMakeFiles/Deplieur.dir/depliage.cpp.obj CMakeFiles/Deplieur.dir/facette.cpp.obj CMakeFiles/Deplieur.dir/mat4x4.cpp.obj CMakeFiles/Deplieur.dir/mesh.cpp.obj CMakeFiles/Deplieur.dir/triangleitem.cpp.obj CMakeFiles/Deplieur.dir/vec3d.cpp.obj CMakeFiles/Deplieur.dir/voisin.cpp.obj CMakeFiles/Deplieur.dir/triangle2d.cpp.obj CMakeFiles/Deplieur.dir/structures.cpp.obj CMakeFiles/Deplieur.dir/deplieurscene.cpp.obj CMakeFiles/Deplieur.dir/Deplieur_autogen/EWIEGA46WW/qrc_resources.cpp.obj -o Deplieur.exe -Wl,--out-implib,libDeplieur.dll.a -Wl,--major-image-version,0,--minor-image-version,0  C:/Qt/6.8.0/mingw_64/lib/libQt6Widgets.a  C:/Qt/6.8.0/mingw_64/lib/libQt6Gui.a  C:/Qt/6.8.0/mingw_64/lib/libQt6Core.a  -lmpr  -luserenv  -lmingw32  C:/Qt/6.8.0/mingw_64/lib/libQt6EntryPoint.a  -lshell32  -ld3d11  -ldxgi  -ldxguid  -ld3d12  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
                C:/Qt/Tools/mingw1310_64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/Deplieur.dir/triangleitem.cpp.obj: in function `TriangleItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*)':
                C:/Users/gilbe/Documents/dev/C++/Deplieur/triangleitem.cpp:59: undefined reference to `DeplieurScene::ChangeToColor(int)'
                collect2.exe: error: ld returned 1 exit status
                ninja: build stopped: subcommand failed.
                

                The undefined reference points to the signal

                class DeplieurScene : public QGraphicsScene
                {
                public:
                    explicit DeplieurScene(QObject *parent = nullptr);
                    QTableWidget * twC;
                
                signals:
                    ChangeToColor(int);
                };
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  You forgot Q_OBJECT

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

                  GilboonetG 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    You forgot Q_OBJECT

                    GilboonetG Offline
                    GilboonetG Offline
                    Gilboonet
                    wrote on last edited by Gilboonet
                    #12

                    @Christian-Ehrlicher I don't think so, I added it first and also added public QObject as ancester and it causes an error saying that I used 2 classes with Q_OBJECT and it didn't compile, Then only adding Q_OBJECT as you suggest causes compilation error on a MOC generated file. I can try again to add Q_OBJECT but if it is needed there is also something else to add.

                    [0/4 0,0/sec] Re-checking globbed directories...
                    [1/20 0,5/sec] Automatic MOC and UIC for target Deplieur
                    [2/20 1,0/sec] Automatic RCC for resources.qrc
                    [3/20 0,2/sec] Building CXX object CMakeFiles/Deplieur.dir/mat4x4.cpp.obj
                    [4/20 0,2/sec] Building CXX object CMakeFiles/Deplieur.dir/facette.cpp.obj
                    [5/20 0,3/sec] Building CXX object CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj
                    FAILED: CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj 
                    C:\Qt\Tools\mingw1310_64\bin\g++.exe -DMINGW_HAS_SECURE_API=1 -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NEEDS_QMAIN -DQT_WIDGETS_LIB -DUNICODE -DWIN32 -DWIN64 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -D_UNICODE -D_WIN64 -IC:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/include -isystem C:/Qt/6.8.0/mingw_64/include/QtCore -isystem C:/Qt/6.8.0/mingw_64/include -isystem C:/Qt/6.8.0/mingw_64/mkspecs/win32-g++ -isystem C:/Qt/6.8.0/mingw_64/include/QtWidgets -isystem C:/Qt/6.8.0/mingw_64/include/QtGui -DQT_QML_DEBUG -g -std=gnu++20 -fdiagnostics-color=always -MD -MT CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj -MF CMakeFiles\Deplieur.dir\Deplieur_autogen\mocs_compilation.cpp.obj.d -o CMakeFiles/Deplieur.dir/Deplieur_autogen/mocs_compilation.cpp.obj -c C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/mocs_compilation.cpp
                    In file included from C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/mocs_compilation.cpp:2:
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:79:71: error: type/value mismatch at argument 1 in template parameter list for 'template<class T, class ForceComplete_> struct QtPrivate::TypeAndForceComplete'
                       79 |         QtPrivate::TypeAndForceComplete<ChangeToColor, std::false_type>,
                          |                                                                       ^
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:79:71: note:   expected a type, got 'DeplieurScene::ChangeToColor'
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:75:5: error: template argument 3 is invalid
                       75 |     qt_incomplete_metaTypeArray<qt_meta_stringdata_CLASSDeplieurSceneENDCLASS_t,
                          |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                       76 |         // Q_OBJECT / Q_GADGET
                          |         ~~~~~~~~~~~~~~~~~~~~~~
                       77 |         QtPrivate::TypeAndForceComplete<DeplieurScene, std::true_type>,
                          |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                       78 |         // method ''
                          |         ~~~~~~~~~~~~
                       79 |         QtPrivate::TypeAndForceComplete<ChangeToColor, std::false_type>,
                          |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                       80 |         QtPrivate::TypeAndForceComplete<int, std::false_type>
                          |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                       81 |     >,
                          |     ~
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp: In static member function 'static void DeplieurScene::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:91:32: error: expected ';' before '_r'
                       91 |         case 0: { ChangeToColor _r = _t->((*reinterpret_cast< std::add_pointer_t<int>>(_a[1])));
                          |                                ^~~
                          |                                ;
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:91:19: error: statement cannot resolve address of overloaded function
                       91 |         case 0: { ChangeToColor _r = _t->((*reinterpret_cast< std::add_pointer_t<int>>(_a[1])));
                          |                   ^~~~~~~~~~~~~
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:92:43: error: 'ChangeToColor' does not name a type
                       92 |             if (_a[0]) *reinterpret_cast< ChangeToColor*>(_a[0]) = std::move(_r); }  break;
                          |                                           ^~~~~~~~~~~~~
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:92:56: error: expected '>' before '*' token
                       92 |             if (_a[0]) *reinterpret_cast< ChangeToColor*>(_a[0]) = std::move(_r); }  break;
                          |                                                        ^
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:92:56: error: expected '(' before '*' token
                       92 |             if (_a[0]) *reinterpret_cast< ChangeToColor*>(_a[0]) = std::move(_r); }  break;
                          |                                                        ^
                          |                                                        (
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:92:57: error: expected primary-expression before '>' token
                       92 |             if (_a[0]) *reinterpret_cast< ChangeToColor*>(_a[0]) = std::move(_r); }  break;
                          |                                                         ^
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:92:78: error: '_r' was not declared in this scope; did you mean 'tr'?
                       92 |             if (_a[0]) *reinterpret_cast< ChangeToColor*>(_a[0]) = std::move(_r); }  break;
                          |                                                                              ^~
                          |                                                                              tr
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:92:81: error: expected ')' before ';' token
                       92 |             if (_a[0]) *reinterpret_cast< ChangeToColor*>(_a[0]) = std::move(_r); }  break;
                          |                                                                                 ^
                          |                                                                                 )
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:98:24: error: expected type-specifier before 'ChangeToColor'
                       98 |             using _t = ChangeToColor (DeplieurScene::*)(int );
                          |                        ^~~~~~~~~~~~~
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:99:17: error: '_t' was not declared in this scope; did you mean 'Qt'?
                       99 |             if (_t _q_method = &DeplieurScene::; *reinterpret_cast<_t *>(_a[1]) == _q_method) {
                          |                 ^~
                          |                 Qt
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:99:68: error: '_t' does not name a type
                       99 |             if (_t _q_method = &DeplieurScene::; *reinterpret_cast<_t *>(_a[1]) == _q_method) {
                          |                                                                    ^~
                    C:/Users/gilbe/Documents/dev/C++/Deplieur/build/Desktop_Qt_6_8_0_MinGW_64_bit-Debug/Deplieur_autogen/EWIEGA46WW/moc_deplieurscene.cpp:99:71: error: expected '>' before '*' token
                    
                    1 Reply Last reply
                    0
                    • GilboonetG Offline
                      GilboonetG Offline
                      Gilboonet
                      wrote on last edited by
                      #13

                      What I missed was also void before the signal definition, it works now.

                      1 Reply Last reply
                      0
                      • GilboonetG Offline
                        GilboonetG Offline
                        Gilboonet
                        wrote on last edited by
                        #14

                        Here's what I did :

                        • Subclass QGraphicsScene
                        • add Q_OBJECT to the subclass class definition
                        • add a signal with a parameter to transmit the color
                        • add a slot to my MainWindow class that also uses a parameter of same type
                        • connect somewhere on my MainWindow all that
                        • add an emit of the signel into my Item subclass MouseDoubleClickEvent() with the item color id as parameter

                        It's very light, I will try to use that mechanism whenever possible as it eliminates need for extra data

                        1 Reply Last reply
                        0
                        • GilboonetG Offline
                          GilboonetG Offline
                          Gilboonet
                          wrote on last edited by Gilboonet
                          #15

                          Here is the implementation of the solution using Slots/Signals to make a event on a subclassed QGraphicsItem send a signal that will activate a slot on another widget. As GraphicsItem is not QObject, the signal could not be emited directly from the Item, but instead from its Scene. That is possible because GraphicsScene is QObject.

                          To add a signal to my Scene class, I needed to subclass it and add Q_OBJECT at the beginning of its definition. Then the signal is added to a section of the definition called Signals. It is a void function definition that can have parameters, but don't need to have an impementation.

                          The Q_OBJECT can certainly be added when you use menu option "File|New File|C++ class" and select it.

                          #ifndef DEPLIEURSCENE_H
                          #define DEPLIEURSCENE_H
                          
                          #include <QGraphicsScene>
                          
                          class DeplieurScene : public QGraphicsScene
                          {
                              Q_OBJECT
                          public:
                              explicit DeplieurScene(QObject *parent = nullptr);
                          
                          signals:
                              void changeCouleur(int);
                          };
                          
                          #endif // DEPLIEURSCENE_H
                          

                          Then, I think there are two possibilites, if your target widget class is a subclass you can add a slot to its definition on the private slots section, use the right click option to refactor and add its implementation. This slot must have the same parameter as your signal. Or if your target widget is a basic widget, you can simply add the slot to you MainWindow class. On the slot you use the parameter to do what you need to do.
                          my slot is in my MainWindow class and is :

                          void MainWindow::changeCouleur(int couleur)
                          {
                              ui->tableCouleurs->selectRow(couleur);
                          }
                          

                          Last, you must create the connection between the signal and the slot, somewhere on your MainWindow, mine is :

                                      dep->dessineModele();
                                      connect(dep->scene3d, &DeplieurScene::changeCouleur, this, &MainWindow::changeCouleur);
                                      dep->creeFaces2d();
                                      connect(dep->scene2d, &DeplieurScene::changeCouleur, this, &MainWindow::changeCouleur);
                          
                          1 Reply Last reply
                          0
                          • GilboonetG Gilboonet has marked this topic as solved on

                          • Login

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