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. To identify good design approach for the Qt Application
Qt 6.11 is out! See what's new in the release blog

To identify good design approach for the Qt Application

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.3k 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by
    #1

    Hello All,
    I have to design a qt Application where i have to interface the serial port and camera
    I just wanted to ask whether it would be good approch to initialize serial port and camera in the mainWindow class or to create separate serialport and camera class.
    Also if i am creating separate class for serialport is it required to inherit the class with Qbject or
    declare Q_Object macro in both the classes.
    Thanks

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • KiraK Kira

      Hello All,
      I have to design a qt Application where i have to interface the serial port and camera
      I just wanted to ask whether it would be good approch to initialize serial port and camera in the mainWindow class or to create separate serialport and camera class.
      Also if i am creating separate class for serialport is it required to inherit the class with Qbject or
      declare Q_Object macro in both the classes.
      Thanks

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kira In general it is a good idea to separate functionality into different modules/classes.
      Regarding inheritance from QObject: usually you only need this if you want to use signals/slots in your classes.

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

      1 Reply Last reply
      4
      • KiraK Kira

        Hello All,
        I have to design a qt Application where i have to interface the serial port and camera
        I just wanted to ask whether it would be good approch to initialize serial port and camera in the mainWindow class or to create separate serialport and camera class.
        Also if i am creating separate class for serialport is it required to inherit the class with Qbject or
        declare Q_Object macro in both the classes.
        Thanks

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        Hi @Kira

        from personal experience I can tell you, if you do not move those two to each his own manager class, than your mainwindow class will get bloated very quickly.

        Also if i am creating separate class for serialport is it required to inherit the class with Qbject or
        declare Q_Object macro in both the classes.

        That depends, your manager class can be a normal c++ class, that way you can simply access stuff via normal getters and setters.

        If you want to emit signals of any kind - for example "newFrameReady" or "newSerialData" you have to derive from QObject

        And in that case the documentation highly suggest also using Q_OBJECT macro, to the meta object compiler can do its magic without problems.


        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
        3
        • KiraK Offline
          KiraK Offline
          Kira
          wrote on last edited by
          #4

          @jsulm @J-Hilk : Thanks for quick response.

          ->Actually my mainwindow class was getting bloated, because of application functionality
          and it was getting really hard to debug the application.
          ->I had to declare to QObject in my serialport class as it was giving me error maybe because i was using the functionality of serialport like open and close etc.
          ** The region behind asking the problem of Object declaration was i read in few articles that it creates problem of muliple inheritence. I can see qobject macro in mainwindow class till now the code is working fine but in future can it create any issue??

          J.HilkJ 1 Reply Last reply
          0
          • KiraK Kira

            @jsulm @J-Hilk : Thanks for quick response.

            ->Actually my mainwindow class was getting bloated, because of application functionality
            and it was getting really hard to debug the application.
            ->I had to declare to QObject in my serialport class as it was giving me error maybe because i was using the functionality of serialport like open and close etc.
            ** The region behind asking the problem of Object declaration was i read in few articles that it creates problem of muliple inheritence. I can see qobject macro in mainwindow class till now the code is working fine but in future can it create any issue??

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Kira said in To identify good design approach for the Qt Application:

            ->I had to declare to QObject in my serialport class as it was giving me error maybe because i was using the functionality of serialport like open and close etc.

            That shouldn't make any problems, that said, you can not inherit from QOBject more than once. So if your base class was already QSerialPort you would get an error when also trying to inherit from QObject.

            ** The region behind asking the problem of Object declaration was i read in few articles that it creates problem of muliple inheritence. I can see qobject macro in mainwindow class till now the code is working fine but in future can it create any issue??

            It shouldn't cause you problems, it will eventually increase your compile time, for a fresh clean all- rebuild. But that's about it.


            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.

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

              @Kira said in To identify good design approach for the Qt Application:

              ->I had to declare to QObject in my serialport class as it was giving me error maybe because i was using the functionality of serialport like open and close etc.

              That shouldn't make any problems, that said, you can not inherit from QOBject more than once. So if your base class was already QSerialPort you would get an error when also trying to inherit from QObject.

              ** The region behind asking the problem of Object declaration was i read in few articles that it creates problem of muliple inheritence. I can see qobject macro in mainwindow class till now the code is working fine but in future can it create any issue??

              It shouldn't cause you problems, it will eventually increase your compile time, for a fresh clean all- rebuild. But that's about it.

              KiraK Offline
              KiraK Offline
              Kira
              wrote on last edited by
              #6

              @J.Hilk : Thanks for the clear reply.
              Just a last doubt if you don't mind. There it was mentioned it may create problem with the moc files.
              Actually uptil now i haven't came across scenario where working with moc file was necessay so where can i get a good explanation where on role of moc files.
              Also are moc files are necessary for release mode ?

              --> Regarding the my serialport. I have created the class and included in my mainwindow class.
              I am using cout << Serialport initialized << endl ; when the port is initialize.
              But message appears after i close my application in the creator and i get a strange number if i include endl:
              Output: Serial port initialized00000000620DF5BF
              It was not the case earlier do you have any idea ??

              J.HilkJ 1 Reply Last reply
              0
              • KiraK Kira

                @J.Hilk : Thanks for the clear reply.
                Just a last doubt if you don't mind. There it was mentioned it may create problem with the moc files.
                Actually uptil now i haven't came across scenario where working with moc file was necessay so where can i get a good explanation where on role of moc files.
                Also are moc files are necessary for release mode ?

                --> Regarding the my serialport. I have created the class and included in my mainwindow class.
                I am using cout << Serialport initialized << endl ; when the port is initialize.
                But message appears after i close my application in the creator and i get a strange number if i include endl:
                Output: Serial port initialized00000000620DF5BF
                It was not the case earlier do you have any idea ??

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @Kira said in To identify good design approach for the Qt Application:

                @J.Hilk : Thanks for the clear reply.

                You're welcome

                Just a last doubt if you don't mind. There it was mentioned it may create problem with the moc files.
                Actually uptil now i haven't came across scenario where working with moc file was necessay so where can i get a good explanation where on role of moc files.
                Also are moc files are necessary for release mode ?

                moc files are temporary files created by the Meta-Object-Compiler. In Principle, each class of yours that is derived from QObject will create it's own moc file. Its needed for - among other things - the Signal/Slot magic.
                For more on the topic see here: https://doc.qt.io/archives/qt-4.8/moc.html

                in 99% of all cases, you do not have to worry about them or include them into your build in any case. for the fringe cases, I don't have enough experience to give a good answer ;-)

                --> Regarding the my serialport. I have created the class and included in my mainwindow class.
                I am using cout << Serialport initialized << endl ; when the port is initialize.
                But message appears after i close my application in the creator and i get a strange number if i include endl:
                Output: Serial port initialized00000000620DF5BF
                It was not the case earlier do you have any idea ??

                Nope, can you show a bit more code?


                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.

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

                  @Kira said in To identify good design approach for the Qt Application:

                  @J.Hilk : Thanks for the clear reply.

                  You're welcome

                  Just a last doubt if you don't mind. There it was mentioned it may create problem with the moc files.
                  Actually uptil now i haven't came across scenario where working with moc file was necessay so where can i get a good explanation where on role of moc files.
                  Also are moc files are necessary for release mode ?

                  moc files are temporary files created by the Meta-Object-Compiler. In Principle, each class of yours that is derived from QObject will create it's own moc file. Its needed for - among other things - the Signal/Slot magic.
                  For more on the topic see here: https://doc.qt.io/archives/qt-4.8/moc.html

                  in 99% of all cases, you do not have to worry about them or include them into your build in any case. for the fringe cases, I don't have enough experience to give a good answer ;-)

                  --> Regarding the my serialport. I have created the class and included in my mainwindow class.
                  I am using cout << Serialport initialized << endl ; when the port is initialize.
                  But message appears after i close my application in the creator and i get a strange number if i include endl:
                  Output: Serial port initialized00000000620DF5BF
                  It was not the case earlier do you have any idea ??

                  Nope, can you show a bit more code?

                  KiraK Offline
                  KiraK Offline
                  Kira
                  wrote on last edited by
                  #8

                  @J.Hilk :

                  class SerialPort

                  void SerialPort::initActionConnections()
                  {
                      openSerialPort();
                      serialPort->close();
                      openSerialPort();
                      std::cout<<"Serial port initialized"<<endl;
                  }
                  
                  
                  SerialPort::SerialPort()
                  {
                       serialPort = new QSerialPort(this);
                       initActionConnections();
                       openSerialPort();
                  }
                  

                  MainWindow Class constructor:

                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                      serialport = new SerialPort();
                  }
                  

                  Please let me know in case of any other details.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Why are you calling openSerialPort than many times ?
                    Also, initActionConnections content doesn't make much sense. It's basically open, close, open.

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

                    KiraK 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      Why are you calling openSerialPort than many times ?
                      Also, initActionConnections content doesn't make much sense. It's basically open, close, open.

                      KiraK Offline
                      KiraK Offline
                      Kira
                      wrote on last edited by
                      #10

                      @SGaist : I am using Arduino Leonardo. Last time i had a lot of
                      issues communicating arduino with Qt. So i just came
                      with the workaround.
                      I tried <<endl today and the same issue occurs it prints a number.

                      Gojir4G 1 Reply Last reply
                      0
                      • KiraK Kira

                        @SGaist : I am using Arduino Leonardo. Last time i had a lot of
                        issues communicating arduino with Qt. So i just came
                        with the workaround.
                        I tried <<endl today and the same issue occurs it prints a number.

                        Gojir4G Offline
                        Gojir4G Offline
                        Gojir4
                        wrote on last edited by
                        #11

                        @Kira Usually Arduino serial port take few seconds (2-5) to be available. If you try to communicate to early after the port has been opened, this can cause some issues.

                        KiraK 1 Reply Last reply
                        0
                        • Gojir4G Gojir4

                          @Kira Usually Arduino serial port take few seconds (2-5) to be available. If you try to communicate to early after the port has been opened, this can cause some issues.

                          KiraK Offline
                          KiraK Offline
                          Kira
                          wrote on last edited by
                          #12

                          @Gojir4 : Actually i have tested the code with other arduinos and it worked fine. the
                          problem was only with this specific model of arduino. Their is a separate thread for this discussion
                          where i came with these steps which always worked although i could not derieve any logical
                          explanation for the same

                          1 Reply Last reply
                          0

                          • Login

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