Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QCreator puts slot handlers in wrong class.
Forum Updated to NodeBB v4.3 + New Features

QCreator puts slot handlers in wrong class.

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
31 Posts 7 Posters 4.5k Views 2 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.
  • HowardHarknessH HowardHarkness

    @J-Hilk said in QCreator puts slot handlers in wrong class.:

    but IIRC you actually can't go to the function if its already defined

    Up until this morning, I was able to navigate to an already-defined slot handler that way. I recall being able to do that back in the days of Qt 3.x.

    Having re-installed Qt from scratch, I can now at least continue developing, but the creation and navigation to a slot handler function is still broken.

    The plugin will create in your c++ class a slot (function declaration and definition) that uses connectByName syntax

    Poking through the moc-generated code, I found:

    QMetaObject::connectSlotsByName(MainWindow);
    

    ...in function void setupUi(QmainWindow *MainWindow) in file ui_mainwindow.h. Now I'm wondering (from what you are telling me) if that is a problem. BTW, when I added a slot handler (which got put into the wrong class) and built the project, I was unable to find that slot handler name anywhere in the moc-generated code, which I suppose explains why it's not possible to actually invoke that slot handler function.

    Also, as a matter of principle, please consider not using the "ConnectSlotByName" feature Qt has.

    Oy, vey. What is the alternative? Typing in over 100 (so far) connect calls?

    I don't think I have enough Qt experience to build a Qt application without QCreator. I'm getting a sinking feeling...

    You can have more than one QtCreator installation as well too

    Hmmm... when I installed the newer QC4.15, the installer uninstalled 4.11.1.

    just as an FYI, QtCreator is now in major version 6

    Would QC 6 work with Qt 5.14.2? Somehow, I get the feeling that would screw me up even worse.

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #9

    @HowardHarkness said in QCreator puts slot handlers in wrong class.:

    BTW, when I added a slot handler (which got put into the wrong class) and built the project, I was unable to find that slot handler name anywhere in the moc-generated code, which I suppose explains why it's not possible to actually invoke that slot handler function.

    yes, and thats the problem with the runtime connect check rather than compile time :D
    try explicitly rerunning qmake , which should recreate the moc files. It should fix that issue.
    (You can find that in the BUILD menu)

    Oy, vey. What is the alternative? Typing in over 100 (so far) connect calls?

    yes, don't give up control for convenience, we do that enough in regular life already.
    but, 100 is rather much, for a single class. Do you have your whole Ui in one single class ?

    I don't think I have enough Qt experience to build a Qt application without QCreator. I'm getting a sinking feeling...

    You shouldn't need to, why do you think you need to do that ?

    Hmmm... when I installed the newer QC4.15, the installer uninstalled 4.11.1.

    ok, I have to ask, how did you try to update/install, that crucial here

    Would QC 6 work with Qt 5.14.2? Somehow, I get the feeling that would screw me up even worse.

    considering, that you can build Qt4 projects with QtCreator6, 5.14 projects should be more than possible.

    The difficult part could be getting your hands on precompiled Qt Versions. The Online installer, if you do not log in with a commercial licence, does not show you the options to download older/archived versions.
    So you'll have to get some from somewhere else, for example your old installation, or compile it from source yourself.
    But.... at that point you're spending way too much time on this´, for your one project that you want to do :D


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    HowardHarknessH 2 Replies Last reply
    1
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #10

      @J-Hilk said in QCreator puts slot handlers in wrong class.:

      The difficult part could be getting your hands on precompiled Qt Versions. The Online installer, if you do not log in with a commercial licence, does not show you the options to download older/archived versions.

      I don't think this is true. With just an Open Source license, I can select older versions in the Maintenance Tool after selecting 'Archive' on the right side and re-filtering. I know there are some versions that I don't have access to as an Open Source licensee like the latest Qt5.15.x version (I think the latest is 5.15.8 but I only get up to 5.15.2).

      9ea91d94-9890-4f6a-b8d5-5ab3a7dcea92-image.png

      J.HilkJ 1 Reply Last reply
      0
      • M mchinand

        @J-Hilk said in QCreator puts slot handlers in wrong class.:

        The difficult part could be getting your hands on precompiled Qt Versions. The Online installer, if you do not log in with a commercial licence, does not show you the options to download older/archived versions.

        I don't think this is true. With just an Open Source license, I can select older versions in the Maintenance Tool after selecting 'Archive' on the right side and re-filtering. I know there are some versions that I don't have access to as an Open Source licensee like the latest Qt5.15.x version (I think the latest is 5.15.8 but I only get up to 5.15.2).

        9ea91d94-9890-4f6a-b8d5-5ab3a7dcea92-image.png

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #11

        @mchinand said in QCreator puts slot handlers in wrong class.:

        I don't think this is true. With just an Open Source license, I can select older versions in the Maintenance Tool after selecting 'Archive' on the right side and re-filtering. I know there are some versions that I don't have access to as an Open Source licensee like that latest Qt5.15.x version (I think the latest is 5.15.8 but I only get up to 5.15.2).

        well I'm willing to be taught differently :D
        It's been a while since I used the online installer, the last time I did, the Categories selected section was on the left side and not the right.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @HowardHarkness said in QCreator puts slot handlers in wrong class.:

          BTW, when I added a slot handler (which got put into the wrong class) and built the project, I was unable to find that slot handler name anywhere in the moc-generated code, which I suppose explains why it's not possible to actually invoke that slot handler function.

          yes, and thats the problem with the runtime connect check rather than compile time :D
          try explicitly rerunning qmake , which should recreate the moc files. It should fix that issue.
          (You can find that in the BUILD menu)

          Oy, vey. What is the alternative? Typing in over 100 (so far) connect calls?

          yes, don't give up control for convenience, we do that enough in regular life already.
          but, 100 is rather much, for a single class. Do you have your whole Ui in one single class ?

          I don't think I have enough Qt experience to build a Qt application without QCreator. I'm getting a sinking feeling...

          You shouldn't need to, why do you think you need to do that ?

          Hmmm... when I installed the newer QC4.15, the installer uninstalled 4.11.1.

          ok, I have to ask, how did you try to update/install, that crucial here

          Would QC 6 work with Qt 5.14.2? Somehow, I get the feeling that would screw me up even worse.

          considering, that you can build Qt4 projects with QtCreator6, 5.14 projects should be more than possible.

          The difficult part could be getting your hands on precompiled Qt Versions. The Online installer, if you do not log in with a commercial licence, does not show you the options to download older/archived versions.
          So you'll have to get some from somewhere else, for example your old installation, or compile it from source yourself.
          But.... at that point you're spending way too much time on this´, for your one project that you want to do :D

          HowardHarknessH Offline
          HowardHarknessH Offline
          HowardHarkness
          wrote on last edited by HowardHarkness
          #12

          @J-Hilk said in QCreator puts slot handlers in wrong class.:

          yes, don't give up control for convenience, we do that enough in regular life already.
          but, 100 is rather much, for a single class. Do you have your whole Ui in one single class ?

          Yes, I am. Suboptimal design choice, I'm sure, but seemed ok at the time, and I'm not sure how to go about re-structuring it. The application is for evaluation of some new hardware. A single board computer with dozens of registers, each of which has from 4 to 20 subfields to display and edit. I started out with a QTabWidget with a separate tab for each register, then when that got too cumbersome, switched to a QStackedWidget controlled by a QListWidget. The rest of the GUI is for setting IP addresses and ports and sending commands to the board. Replies are parsed into the register displays and subfields (I really did do a connect for the read callback -- following a recipe I found somewhere in the Qt examples).

          So you'll have to get some from somewhere else, for example your old installation, or compile it from source yourself.
          But.... at that point you're spending way too much time on this´, for your one project that you want to do :D

          Accurate observation.

          My other problem is that I'm the only one in the group who has actually used Qt for more than a trivial application (although long ago, in the 3.x days), so I'm currently out ahead of anybody in my group I might otherwise tap on for help :(

          I hope to be done with this by the end of February, and document it well enough so that whomever gets "volunteered" to maintain it isn't completely lost, and I can end my contract with a clear conscience -- and go back to embedded hard-realtime systems programming, which I do better (and like better) than GUI programming.

          Meanwhile, QCreator still puts new slot handlers in some random class. It's a different one each time I start up the IDE after doing the "reset" I described in my original post on this thread.

          Howard Lee Harkness
          https://howardleeharkness.com/resume
          Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @HowardHarkness said in QCreator puts slot handlers in wrong class.:

            BTW, when I added a slot handler (which got put into the wrong class) and built the project, I was unable to find that slot handler name anywhere in the moc-generated code, which I suppose explains why it's not possible to actually invoke that slot handler function.

            yes, and thats the problem with the runtime connect check rather than compile time :D
            try explicitly rerunning qmake , which should recreate the moc files. It should fix that issue.
            (You can find that in the BUILD menu)

            Oy, vey. What is the alternative? Typing in over 100 (so far) connect calls?

            yes, don't give up control for convenience, we do that enough in regular life already.
            but, 100 is rather much, for a single class. Do you have your whole Ui in one single class ?

            I don't think I have enough Qt experience to build a Qt application without QCreator. I'm getting a sinking feeling...

            You shouldn't need to, why do you think you need to do that ?

            Hmmm... when I installed the newer QC4.15, the installer uninstalled 4.11.1.

            ok, I have to ask, how did you try to update/install, that crucial here

            Would QC 6 work with Qt 5.14.2? Somehow, I get the feeling that would screw me up even worse.

            considering, that you can build Qt4 projects with QtCreator6, 5.14 projects should be more than possible.

            The difficult part could be getting your hands on precompiled Qt Versions. The Online installer, if you do not log in with a commercial licence, does not show you the options to download older/archived versions.
            So you'll have to get some from somewhere else, for example your old installation, or compile it from source yourself.
            But.... at that point you're spending way too much time on this´, for your one project that you want to do :D

            HowardHarknessH Offline
            HowardHarknessH Offline
            HowardHarkness
            wrote on last edited by
            #13

            @J-Hilk said in QCreator puts slot handlers in wrong class.:

            ok, I have to ask, how did you try to update/install, that crucial here

            I downloaded the (insecure) installer for QT 4.15 from https://download.qt.io/official_releases/qtcreator/4.15/4.15.0/ and ran it. Once that was done, nothing worked. The 4.11.1 version was gone, and I was unable to run a Qt IDE. Obviously, I screwed something up -- probably starting with downloading an installer that warned me that it was an insecure download.

            Once bitten, twice shy. Not really eager to repeat that experience.

            Howard Lee Harkness
            https://howardleeharkness.com/resume
            Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

            HowardHarknessH 1 Reply Last reply
            0
            • HowardHarknessH HowardHarkness

              @J-Hilk said in QCreator puts slot handlers in wrong class.:

              ok, I have to ask, how did you try to update/install, that crucial here

              I downloaded the (insecure) installer for QT 4.15 from https://download.qt.io/official_releases/qtcreator/4.15/4.15.0/ and ran it. Once that was done, nothing worked. The 4.11.1 version was gone, and I was unable to run a Qt IDE. Obviously, I screwed something up -- probably starting with downloading an installer that warned me that it was an insecure download.

              Once bitten, twice shy. Not really eager to repeat that experience.

              HowardHarknessH Offline
              HowardHarknessH Offline
              HowardHarkness
              wrote on last edited by
              #14

              I'm still hoping that somebody can show me a way to convince QCreator to add new slot handlers to my mainwindow class, instead of to some random class that seems to change every time I restart Qt. I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

              Howard Lee Harkness
              https://howardleeharkness.com/resume
              Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

              JonBJ J.HilkJ HowardHarknessH 3 Replies Last reply
              0
              • HowardHarknessH HowardHarkness

                I'm still hoping that somebody can show me a way to convince QCreator to add new slot handlers to my mainwindow class, instead of to some random class that seems to change every time I restart Qt. I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #15

                @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                There isn't.

                HowardHarknessH 1 Reply Last reply
                0
                • HowardHarknessH HowardHarkness

                  I'm still hoping that somebody can show me a way to convince QCreator to add new slot handlers to my mainwindow class, instead of to some random class that seems to change every time I restart Qt. I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #16

                  @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                  I'm still hoping that somebody can show me a way to convince QCreator to add new slot handlers to my mainwindow class, instead of to some random class that seems to change every time I restart Qt. I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                  I would say it's obvious that the macro fails, so why would flag or switch turn that of ? Unlikely.

                  But, try the following. When in DesginerView right click on the file combobox and select ˙close All except "myCurrentOpenFile.ui"

                  Then try your go to slot thing.

                  Opening the file new could be a potential workaround for the faulty macro.
                  See, if that helps at all.

                  578ff6d5-c491-43c0-bc38-2276853b9e80-image.png


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  HowardHarknessH 1 Reply Last reply
                  0
                  • HowardHarknessH HowardHarkness

                    I'm still hoping that somebody can show me a way to convince QCreator to add new slot handlers to my mainwindow class, instead of to some random class that seems to change every time I restart Qt. I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                    HowardHarknessH Offline
                    HowardHarknessH Offline
                    HowardHarkness
                    wrote on last edited by
                    #17

                    I may have achieved a "wear-through".

                    The key seems to be in the position of the #include "ui_mainwindow.h" in the mainwindow.cpp file. When I positioned that #include to be the first thing included in that compilation unit, suddenly QCreator could find the right place to insert new slot handlers. In other classes, I used a forward declaration for class MainWindow where possible, and made sure that if I included ui_mainwindow.h, it was not the first #include.

                    I have no idea why that would work -- and I'm not sure it's a permanent fix.

                    Howard Lee Harkness
                    https://howardleeharkness.com/resume
                    Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

                    1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                      I'm still hoping that somebody can show me a way to convince QCreator to add new slot handlers to my mainwindow class, instead of to some random class that seems to change every time I restart Qt. I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                      I would say it's obvious that the macro fails, so why would flag or switch turn that of ? Unlikely.

                      But, try the following. When in DesginerView right click on the file combobox and select ˙close All except "myCurrentOpenFile.ui"

                      Then try your go to slot thing.

                      Opening the file new could be a potential workaround for the faulty macro.
                      See, if that helps at all.

                      578ff6d5-c491-43c0-bc38-2276853b9e80-image.png

                      HowardHarknessH Offline
                      HowardHarknessH Offline
                      HowardHarkness
                      wrote on last edited by
                      #18

                      @J-Hilk said in QCreator puts slot handlers in wrong class.:

                      But, try the following.

                      Actually, I did try that, and it did not fix the problem. At about the same time you posted that, I did find something that appeared to work. For now.

                      Howard Lee Harkness
                      https://howardleeharkness.com/resume
                      Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                        I would think that there is some flag or switch in the environment that I could set to fix this annoyance.

                        There isn't.

                        HowardHarknessH Offline
                        HowardHarknessH Offline
                        HowardHarkness
                        wrote on last edited by
                        #19

                        @JonB said in QCreator puts slot handlers in wrong class.:

                        There isn't.

                        Per my other post in this thread, I found something that appears to work, for now. I'm speculating that the parser in the QCreator IDE simply gets stuck on the first instance of the ui_mainwindow it finds, and assumes that whatever compilation unit it found it in is the main window.

                        Just a theory, but seems plausible.

                        Howard Lee Harkness
                        https://howardleeharkness.com/resume
                        Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

                        JonBJ 1 Reply Last reply
                        0
                        • HowardHarknessH HowardHarkness

                          @JonB said in QCreator puts slot handlers in wrong class.:

                          There isn't.

                          Per my other post in this thread, I found something that appears to work, for now. I'm speculating that the parser in the QCreator IDE simply gets stuck on the first instance of the ui_mainwindow it finds, and assumes that whatever compilation unit it found it in is the main window.

                          Just a theory, but seems plausible.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #20

                          @HowardHarkness
                          I did not say you could not work around it, maybe and best of luck, just that there does not appear any "some flag or switch in the environment ".

                          HowardHarknessH 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @HowardHarkness
                            I did not say you could not work around it, maybe and best of luck, just that there does not appear any "some flag or switch in the environment ".

                            HowardHarknessH Offline
                            HowardHarknessH Offline
                            HowardHarkness
                            wrote on last edited by
                            #21

                            This is a summary that I'm going to mark as the solution, although I'm not 100% sure this is a permanent fix.

                            QCreator quirks

                            I encountered a problem with QCreator putting new slot handlers in the wrong class. This made for significant inconvenience in adding new slot handler functions.

                            I found what I think is a fix for this particular QCreator wonkiness. QCreator appears to get "stuck" on whatever compilation unit it is on when it encounters the first instance of #include "ui_mainwindow.h" and assumes that is your "main" window class. The workaround I found was to make sure that file is included first in the mainwindow.cpp file, and last in any other file that uses it. Where possible, don't even include ui_mainwindow.h at all, but use a forward declaration of the form:

                            	namespace Ui
                            	{
                            		class MainWindow;
                            	}
                            

                            You will need to do a "reset" of the IDE after implementing this workaround. Detailed instructions from the Qt website:

                            1. Close Qt Creator.
                            2. Use your file explorer to navigate to the folder where your project is stored. Delete the ".pro.user" file that is there, such as Life.pro.user.
                            3. Also delete the "build" folder for your project. This is located in the parent directory of your project's directory, and it usually has a long name like build-Life-Desktop_Qt_5_5_0_MinGW_32bit-Debug . Delete this entire directory.
                            4. Re-open your project's .pro file in Qt Creator. It should ask you to "Configure Project".

                            Howard Lee Harkness
                            https://howardleeharkness.com/resume
                            Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

                            J.HilkJ 1 Reply Last reply
                            0
                            • HowardHarknessH HowardHarkness

                              This is a summary that I'm going to mark as the solution, although I'm not 100% sure this is a permanent fix.

                              QCreator quirks

                              I encountered a problem with QCreator putting new slot handlers in the wrong class. This made for significant inconvenience in adding new slot handler functions.

                              I found what I think is a fix for this particular QCreator wonkiness. QCreator appears to get "stuck" on whatever compilation unit it is on when it encounters the first instance of #include "ui_mainwindow.h" and assumes that is your "main" window class. The workaround I found was to make sure that file is included first in the mainwindow.cpp file, and last in any other file that uses it. Where possible, don't even include ui_mainwindow.h at all, but use a forward declaration of the form:

                              	namespace Ui
                              	{
                              		class MainWindow;
                              	}
                              

                              You will need to do a "reset" of the IDE after implementing this workaround. Detailed instructions from the Qt website:

                              1. Close Qt Creator.
                              2. Use your file explorer to navigate to the folder where your project is stored. Delete the ".pro.user" file that is there, such as Life.pro.user.
                              3. Also delete the "build" folder for your project. This is located in the parent directory of your project's directory, and it usually has a long name like build-Life-Desktop_Qt_5_5_0_MinGW_32bit-Debug . Delete this entire directory.
                              4. Re-open your project's .pro file in Qt Creator. It should ask you to "Configure Project".
                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #22

                              @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                              #include "ui_mainwindow.h" and assumes that is your "main" window class

                              here is actually a more fundamental problem on your part.

                              ui_ClassName.h should always only be included once, and that is from your ClassName.cpp file. Interpret ui_mainwindow.h as a private header of mainwindow.cpp

                              why do you include it anywhere else , a 2nd time, in your code base ?


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              JonBJ HowardHarknessH 2 Replies Last reply
                              2
                              • J.HilkJ J.Hilk

                                @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                                #include "ui_mainwindow.h" and assumes that is your "main" window class

                                here is actually a more fundamental problem on your part.

                                ui_ClassName.h should always only be included once, and that is from your ClassName.cpp file. Interpret ui_mainwindow.h as a private header of mainwindow.cpp

                                why do you include it anywhere else , a 2nd time, in your code base ?

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #23

                                @J-Hilk said in QCreator puts slot handlers in wrong class.:

                                ui_ClassName.h should always only be included once, and that is from your ClassName.cpp file. Interpret ui_mainwindow.h as a private header of mainwindow.cpp

                                Double-plus++. Sounds like a very possible reason.

                                @HowardHarkness said:

                                instead of to some random class that seems to change every time I restart Qt.

                                Do all these "random" classes have #include "ui_mainwindow.h" in them?

                                1 Reply Last reply
                                0
                                • J.HilkJ J.Hilk

                                  @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                                  #include "ui_mainwindow.h" and assumes that is your "main" window class

                                  here is actually a more fundamental problem on your part.

                                  ui_ClassName.h should always only be included once, and that is from your ClassName.cpp file. Interpret ui_mainwindow.h as a private header of mainwindow.cpp

                                  why do you include it anywhere else , a 2nd time, in your code base ?

                                  HowardHarknessH Offline
                                  HowardHarknessH Offline
                                  HowardHarkness
                                  wrote on last edited by
                                  #24

                                  @J-Hilk said in QCreator puts slot handlers in wrong class.:

                                  why do you include it anywhere else , a 2nd time, in your code base ?

                                  I separated out parts of the hardware (register groups) into separate classes, and I needed a pointer to the various line edit widgets in the main window to get/set the values. The pointer to the main window was named ui and it was defined as a Ui::MainWindow* in ui_mainwindow.h.

                                  	namespace Ui {
                                  			class MainWindow: public Ui_MainWindow {};
                                  	}
                                  

                                  There were some .cpp files I couldn't figure out how to get past the compiler without including ui_mainwindow.h. I needed that #include because the ui pointer can't be used without it. When I implement the other parts of the hardware, there will be more of the same sort of problem.

                                  It would appear that might not have been the optimal design... However, it's not immediately obvious what I should have done. Is there something other than a Ui::MainWindow* that I can use to access the line edits? I don't really want to have to pass a separate pointer to each line edit as a parameter. to the hardware classes.

                                  Howard Lee Harkness
                                  https://howardleeharkness.com/resume
                                  Current contract ends on March 11th, 2022 -- Looking for C++ Real-time Embedded Systems work

                                  jsulmJ J.HilkJ 2 Replies Last reply
                                  0
                                  • HowardHarknessH HowardHarkness

                                    @J-Hilk said in QCreator puts slot handlers in wrong class.:

                                    why do you include it anywhere else , a 2nd time, in your code base ?

                                    I separated out parts of the hardware (register groups) into separate classes, and I needed a pointer to the various line edit widgets in the main window to get/set the values. The pointer to the main window was named ui and it was defined as a Ui::MainWindow* in ui_mainwindow.h.

                                    	namespace Ui {
                                    			class MainWindow: public Ui_MainWindow {};
                                    	}
                                    

                                    There were some .cpp files I couldn't figure out how to get past the compiler without including ui_mainwindow.h. I needed that #include because the ui pointer can't be used without it. When I implement the other parts of the hardware, there will be more of the same sort of problem.

                                    It would appear that might not have been the optimal design... However, it's not immediately obvious what I should have done. Is there something other than a Ui::MainWindow* that I can use to access the line edits? I don't really want to have to pass a separate pointer to each line edit as a parameter. to the hardware classes.

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

                                    @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                                    I needed a pointer to the various line edit widgets in the main window to get/set the values. The pointer to the main window was named ui and it was defined as a Ui::MainWindow* in ui_mainwindow.h

                                    This sounds very very wrong!
                                    To access an instance of a class do what is usually done in Qt/C++:

                                    1. Pass a pointer to the instance
                                    2. Use signals/slots to exchange data between classes

                                    I would strongly suggest to go for 2. This way your other classes do not have to know wnything about the main class. Especially you should NEVER expose internal details (like line edits in main window) to the outside world - this is really bad design.

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

                                    1 Reply Last reply
                                    3
                                    • HowardHarknessH HowardHarkness

                                      @J-Hilk said in QCreator puts slot handlers in wrong class.:

                                      why do you include it anywhere else , a 2nd time, in your code base ?

                                      I separated out parts of the hardware (register groups) into separate classes, and I needed a pointer to the various line edit widgets in the main window to get/set the values. The pointer to the main window was named ui and it was defined as a Ui::MainWindow* in ui_mainwindow.h.

                                      	namespace Ui {
                                      			class MainWindow: public Ui_MainWindow {};
                                      	}
                                      

                                      There were some .cpp files I couldn't figure out how to get past the compiler without including ui_mainwindow.h. I needed that #include because the ui pointer can't be used without it. When I implement the other parts of the hardware, there will be more of the same sort of problem.

                                      It would appear that might not have been the optimal design... However, it's not immediately obvious what I should have done. Is there something other than a Ui::MainWindow* that I can use to access the line edits? I don't really want to have to pass a separate pointer to each line edit as a parameter. to the hardware classes.

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by J.Hilk
                                      #26

                                      @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                                      I separated out parts of the hardware (register groups) into separate classes, and I needed a pointer to the various line edit widgets in the main window to get/set the values. The pointer to the main window was named ui and it was defined as a Ui::MainWindow* in ui_mainwindow.h.

                                      oh my, I feel the cold sweat running down my spine ! ;-) 😉

                                      So you took the privat Ui-pointer of your MainWindow and passed it to children/member classes/instances, so they could read/write directly from the GUI ?

                                      Yes, that goes against coding guidelines. Usually, in Qt world, one does this via signals and slots.
                                      Your member class instance has a new value you want to display it? Let it blindly emit a Signal it doesn't care what happens with it, emit signal and done. In MainWindow connect the signal of your member to the setter of the widget you want to use to display the value. -> Tight coupling broken.

                                      You want to update a member instance with new values from the GUI ? Listen to input finished signal of your widget and connect it with a setter of your other class.

                                      There were some .cpp files I couldn't figure out how to get past the compiler without including ui_mainwindow.h. I needed that #include because the ui pointer can't be used without it. When I implement the other parts of the hardware, there will be more of the same sort of problem.

                                      Working against/around the compiler, usually not a good sign :P
                                      The Ui Pointer as a private variable should really stay privat and exclusive to that one class.

                                      It would appear that might not have been the optimal design... However, it's not immediately obvious what I should have done. Is there something other than a Ui::MainWindow* that I can use to access the line edits? I don't really want to have to pass a separate pointer to each line edit as a parameter. to the hardware classes

                                      Like I said, neither the Ui-Pointer nor any other elements of the GUI the pointer points too should be exposed outside of the MainWindow class. Break the tight coupling, it will lead to other problems along the line, like hard/impossible to maintain/refactor code.

                                      Use signal/slots and/or getters&setters


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      Christian EhrlicherC HowardHarknessH 2 Replies Last reply
                                      2
                                      • J.HilkJ J.Hilk

                                        @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                                        I separated out parts of the hardware (register groups) into separate classes, and I needed a pointer to the various line edit widgets in the main window to get/set the values. The pointer to the main window was named ui and it was defined as a Ui::MainWindow* in ui_mainwindow.h.

                                        oh my, I feel the cold sweat running down my spine ! ;-) 😉

                                        So you took the privat Ui-pointer of your MainWindow and passed it to children/member classes/instances, so they could read/write directly from the GUI ?

                                        Yes, that goes against coding guidelines. Usually, in Qt world, one does this via signals and slots.
                                        Your member class instance has a new value you want to display it? Let it blindly emit a Signal it doesn't care what happens with it, emit signal and done. In MainWindow connect the signal of your member to the setter of the widget you want to use to display the value. -> Tight coupling broken.

                                        You want to update a member instance with new values from the GUI ? Listen to input finished signal of your widget and connect it with a setter of your other class.

                                        There were some .cpp files I couldn't figure out how to get past the compiler without including ui_mainwindow.h. I needed that #include because the ui pointer can't be used without it. When I implement the other parts of the hardware, there will be more of the same sort of problem.

                                        Working against/around the compiler, usually not a good sign :P
                                        The Ui Pointer as a private variable should really stay privat and exclusive to that one class.

                                        It would appear that might not have been the optimal design... However, it's not immediately obvious what I should have done. Is there something other than a Ui::MainWindow* that I can use to access the line edits? I don't really want to have to pass a separate pointer to each line edit as a parameter. to the hardware classes

                                        Like I said, neither the Ui-Pointer nor any other elements of the GUI the pointer points too should be exposed outside of the MainWindow class. Break the tight coupling, it will lead to other problems along the line, like hard/impossible to maintain/refactor code.

                                        Use signal/slots and/or getters&setters

                                        Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #27

                                        @J-Hilk said in QCreator puts slot handlers in wrong class.:

                                        Yes, that goes against coding guidelines.

                                        This is against c++ in general ... :)

                                        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
                                        0
                                        • HowardHarknessH HowardHarkness

                                          @J-Hilk I'm using QCreator 4.11.1 -- updating my development environment is a bit tricky here due to the way my PC is locked down. I have to get permission from IT, which can take days... However, I will look into that. I'm not really sure what the current (or best) version of QCreator is, but I guess I will be able to find that out with some searching.

                                          I was unaware of the "ConnectSlotByName" function, so I wasn't using it (AFAIK -- I just now read the docs for that, and didn't completely understand what that function does). What I generally do is right-click on the widget I want to make a slot handler for, and then click on the "go to slot...", followed by the selection of the slot. In this case, currentRowChanged(int) for a QListWidget.
                                          f99f12aa-f1c6-4e25-8ea2-fadd5c624ed1-image.png

                                          I can work around the creation in the wrong class by cutting & pasting in the correct class (and changing the classname qualifier). It's a relatively minor annoyance, but what's REALLY annoying is when I want to go to an existing slot handler, and it creates a new one in the wrong class that I have to delete from both cpp and h files, and then do a search separately for the handler. When it actually works, the navigation features of QCreator are really convenient -- when they don't work, it bogs me down and disrupts my train of thought.

                                          Which brings up another pet peeve -- I wish that QCreator would alphabetize the slot handler entries -- at least in the .h file. I currently do that separately (NotePad++ has a fairly handly sort feature). Then I don't have to do a control-F search to find a slot handler -- I have nearly 100 at this point.

                                          andrA Offline
                                          andrA Offline
                                          andr
                                          wrote on last edited by
                                          #28

                                          @HowardHarkness said in QCreator puts slot handlers in wrong class.:

                                          Which brings up another pet peeve -- I wish that QCreator would alphabetize the slot handler entries -- at least in the .h file. I currently do that separately (NotePad++ has a fairly handly sort feature). [...]

                                          Note that you can sort selected lines alphabetically by pressing Alt-Shift-S

                                          HowardHarknessH M 2 Replies Last reply
                                          1

                                          • Login

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