Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

To identify good design approach for the Qt Application



  • 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


  • Lifetime Qt Champion

    @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.


  • Moderators

    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.



  • @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??


  • Moderators

    @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.



  • @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 ??


  • Moderators

    @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?



  • @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.


  • Lifetime Qt Champion

    Hi,

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



  • @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.



  • @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.



  • @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


Log in to reply