Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. custom model/view - need guidance (newbie)
Qt 6.11 is out! See what's new in the release blog

custom model/view - need guidance (newbie)

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 2 Posters 991 Views 1 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.
  • superagaS Offline
    superagaS Offline
    superaga
    wrote on last edited by
    #1

    Hi EXPERTS!

    this is my first post on this forum (and yes I read the Qt Code of Conduct :) ).
    I already searched for this topic, but couldn't find it. If it should already exist, please apologize me!
    Currently I've been able to do only some simple GUI using PyQt5 and nothing more, but since I have a new (big) project I though to take the opportunity to rise my knowledge on this. I really hope that someone will help me to improve my skills in this wonderful QT world.

    I'm starting a new project where I need to show (using textboxes, plots, ...) real-time data (that come from a network data stream).
    This GUI will provide to the user also other functions, so my idea is to create it using the following paradigms:

    • multithreading - to manage tasks and the GUI;
    • model/view architecture - to show/change data.

    Of course in the Qt forum I'm looking mainly to this second item... My idea, and please correct me if I am wrong, is to make a custom model to fetch the data from the network and write them to a buffer (or probably better to a file or into a database).
    I suppose that I'll need also a custom delegate that would be able to "talk" to this model to paint/update data in the widgets.

    Is this flow correct? Since I consider myself a newbie, please feel free to propose me other way to approach the problem.
    Just for the sake of clarity I provide you some other info (may be could be useful to better understand the problem):

    • data sampling time could be up 100 Hz;
    • data could contain up to 100 floats.

    Any help suggestion would be very appreciated.

    Many thanks!

    Kind regards,
    AGA

    VRoninV 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #4

      Disclaimer

      I'm one of the worst software designers the planet has ever seen. My code always ends up a mess so I'm not totally qualified to answer.


      Firstly forget about threading and make the program work on the main thread (it might be slow as hell but at this stage it doesn't matter).
      Make sure you compartmentalise each functionality in its own QObject, don't make one big class that becomes unmanageable (we all did it and got burned).
      Handle the communication between objects via signal/slots. Avoid as much as you can one object directly call a method of another and if you have to make a note in the code (e.g. qWarning()) so that you know you need to police access in those cases.
      Now you'll have:

      • an object that receives data from the network and sends out signals when it has new data.
      • an object that receives the signal from the first and saves the data somewhere
        The GUI then depends on the implementation of the logic in the previous objects. For example, if you save the data in a database you could then use QSqlQueryModel to access it directly and display it in a chart/table

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • superagaS superaga

        Hi EXPERTS!

        this is my first post on this forum (and yes I read the Qt Code of Conduct :) ).
        I already searched for this topic, but couldn't find it. If it should already exist, please apologize me!
        Currently I've been able to do only some simple GUI using PyQt5 and nothing more, but since I have a new (big) project I though to take the opportunity to rise my knowledge on this. I really hope that someone will help me to improve my skills in this wonderful QT world.

        I'm starting a new project where I need to show (using textboxes, plots, ...) real-time data (that come from a network data stream).
        This GUI will provide to the user also other functions, so my idea is to create it using the following paradigms:

        • multithreading - to manage tasks and the GUI;
        • model/view architecture - to show/change data.

        Of course in the Qt forum I'm looking mainly to this second item... My idea, and please correct me if I am wrong, is to make a custom model to fetch the data from the network and write them to a buffer (or probably better to a file or into a database).
        I suppose that I'll need also a custom delegate that would be able to "talk" to this model to paint/update data in the widgets.

        Is this flow correct? Since I consider myself a newbie, please feel free to propose me other way to approach the problem.
        Just for the sake of clarity I provide you some other info (may be could be useful to better understand the problem):

        • data sampling time could be up 100 Hz;
        • data could contain up to 100 floats.

        Any help suggestion would be very appreciated.

        Many thanks!

        Kind regards,
        AGA

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        @superaga said in custom model/view - need guidance (newbie):

        is to make a custom model to fetch the data from the network and write them to a buffer

        Models can't live on a different thread from the view (the view calls methods of the model directly creating race conditions).

        Other than that the general idea is alright, nobody will ever read 10,000 raw numbers each second so you probably want to summarise the data in some way that makes sense for your case

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • superagaS Offline
          superagaS Offline
          superaga
          wrote on last edited by
          #3

          Hi @VRonin ,

          many thanks for your reply!

          I didn't know about this. Sure makes sense! So probably I should do something like this:

          • a thread to fetch data and another to eventually send back (parameters);
          • a thread to store the data into a file or into a database;
          • a thread for the entire GUI.

          Could you please explain me a bit more how should I proceed?

          To be more precise let's take this example:
          I have an embedded system that acquires data and send them to the PC through the network (up to 100 signals).
          I need to create an interface to visualize (as you said) only a portion of these data, let's say only the last 2 s, while storing all the data stream to a file/database.

          What is the first milestone to reach (if this the right architecture to use)?
          I'm sorry but at least now (at the beginning) I need to be guided.

          Many thanks!

          Kind regards,
          AGA

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #4

            Disclaimer

            I'm one of the worst software designers the planet has ever seen. My code always ends up a mess so I'm not totally qualified to answer.


            Firstly forget about threading and make the program work on the main thread (it might be slow as hell but at this stage it doesn't matter).
            Make sure you compartmentalise each functionality in its own QObject, don't make one big class that becomes unmanageable (we all did it and got burned).
            Handle the communication between objects via signal/slots. Avoid as much as you can one object directly call a method of another and if you have to make a note in the code (e.g. qWarning()) so that you know you need to police access in those cases.
            Now you'll have:

            • an object that receives data from the network and sends out signals when it has new data.
            • an object that receives the signal from the first and saves the data somewhere
              The GUI then depends on the implementation of the logic in the previous objects. For example, if you save the data in a database you could then use QSqlQueryModel to access it directly and display it in a chart/table

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3
            • superagaS Offline
              superagaS Offline
              superaga
              wrote on last edited by
              #5

              Hi @VRonin ,

              Many thanks again for your answer.

              ok, I'll start taking the things easy, but modular, so they can be scaled up.
              Once I'll have a working program of this 2 basic function I'll ask again.

              Regarding the custom model creation, can you (or someone else) share or point me to a simple example that is not related to the classic QTableWidget, QListWidget or QTreeWidget?

              Many thanks!

              Kind regards,
              AGA

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #6

                @superaga said in custom model/view - need guidance (newbie):

                Regarding the custom model creation

                My advice is: if you can, avoid it. If you can stick to QStandardItemModel then don't complicate your life.
                If you really want to go down the rabbit hole, Peppe has a nice introduction video for a first step

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1
                • superagaS Offline
                  superagaS Offline
                  superaga
                  wrote on last edited by
                  #7

                  HI @VRonin ,

                  many thanks for the link. I found the video extremely helpful!
                  That cleared my a lot the working model and the related API and classes. Now I have more info to decide and I think I might start to use the QAbstractTableModel since my data will be in a dataset form. I'll have a look to some basic example based on this and I'll try to escalate to my needs.

                  Many thanks!

                  Kind regards,
                  AGA

                  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