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. How to create Container for multiple QWidget?
QtWS25 Last Chance

How to create Container for multiple QWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 675 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.
  • N Offline
    N Offline
    npatil15
    wrote on 4 Apr 2020, 09:06 last edited by npatil15 4 Apr 2020, 09:12
    #1

    Hello,

    I have multiple QWidgets and want to share each widget with each other for sharing each other data.

    I have created multiple widgets and there are other ways to share widget data by sharing pointer with each other, but is there an elegant solution where I can create a simple container to add all the widgets in it and make this container shareable to each other without passing to each other.

    Please share your suggestions.

    Thanks :)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 4 Apr 2020, 09:29 last edited by
      #2

      Hi
      I would use a model to hold the data and use qdatawidgetmapper to let the widget read and write data.
      If you share each widget to each widget, you get a very tightly coupled design with too many details
      shared among the widgets.
      Using a model, they do not need to know about other widgets at all.

      https://doc.qt.io/qt-5/model-view-programming.html
      https://doc.qt.io/qt-5/qdatawidgetmapper.html

      1 Reply Last reply
      2
      • N Offline
        N Offline
        npatil15
        wrote on 4 Apr 2020, 11:32 last edited by
        #3

        Thanks for your quick reply,
        I was looking over qdatawidgetmapper thoroughly.

        One thing is not understanding about sharing data amoung the widget.
        Example: Let say I have two widget : widget1 and widget2.

            QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
            QueryModel *model = new QueryModel(); // Have created custom model, which I want to share among the two widgets
            mapper->setModel(model);
            mapper->addMapping(widget1, 0);
            mapper->addMapping(widget2, 1);
            mapper->toFirst();
        

        Now in widget1 I want to do some changes in the model, same in widget2 as well. So how I can share this model to both the widgets.

        I have read about delegates but still not understand its working properly. I'm not sure how it will help to share model to both widgets.

        Please give me some suggestions or examples to explain this.

        Thanks :)

        M 1 Reply Last reply 4 Apr 2020, 11:38
        0
        • N npatil15
          4 Apr 2020, 11:32

          Thanks for your quick reply,
          I was looking over qdatawidgetmapper thoroughly.

          One thing is not understanding about sharing data amoung the widget.
          Example: Let say I have two widget : widget1 and widget2.

              QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
              QueryModel *model = new QueryModel(); // Have created custom model, which I want to share among the two widgets
              mapper->setModel(model);
              mapper->addMapping(widget1, 0);
              mapper->addMapping(widget2, 1);
              mapper->toFirst();
          

          Now in widget1 I want to do some changes in the model, same in widget2 as well. So how I can share this model to both the widgets.

          I have read about delegates but still not understand its working properly. I'm not sure how it will help to share model to both widgets.

          Please give me some suggestions or examples to explain this.

          Thanks :)

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 4 Apr 2020, 11:38 last edited by mrjj 4 Apr 2020, 11:40
          #4

          @npatil15
          Hi
          you don't need to share the model to them. the QDataWidgetMapper handles this.
          If you map them to same section of the model , they show same data

          mapper->addMapping(widget1, 0);
          mapper->addMapping(widget2, 0);

          Try this.
          place 2 LineEdits on a form.

             QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
              QStringListModel *model = new QStringListModel();
              QStringList list;
              list << "a" << "b" << "c";
              model->setStringList(list);
          
              mapper->setModel(model);
              mapper->addMapping(ui->lineEdit, 0);
              mapper->addMapping(ui->lineEdit_2, 0);
              mapper->toFirst();
          

          Then edit one of the LineEdits and press enter. as you can see the other will show the updated information

          ps. Delegates are for custom editors and drawing. It has very little to do with the data.
          So
          Models hold the data
          The view shows the data (the widgets)
          Delegates allow custom editors and drawing in views. ( Not all Widgets can use delegates. List/Table/tree views can)

          1 Reply Last reply
          1
          • N Offline
            N Offline
            npatil15
            wrote on 5 Apr 2020, 08:05 last edited by
            #5

            Yes, it works, but If I understand correctly, it will work if both widgets have same types of datatypes/inputs, isn't it?

            Where I have two custom widgets, which have multiple methods and inputs to process this data and need to update in the same model. So how this will work?

            M 1 Reply Last reply 5 Apr 2020, 08:21
            0
            • N npatil15
              5 Apr 2020, 08:05

              Yes, it works, but If I understand correctly, it will work if both widgets have same types of datatypes/inputs, isn't it?

              Where I have two custom widgets, which have multiple methods and inputs to process this data and need to update in the same model. So how this will work?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 5 Apr 2020, 08:21 last edited by
              #6

              @npatil15
              well QDataWidgetMapper must be able to map the QVariant to
              the widget so it only works with Widgets it understands.

              If we really want the widgets to also do data processing behind the scene and not just editing
              then you need to give it access to the model directly if QDataWidgetMapper editing is not enough.

              1 Reply Last reply
              0

              5/6

              5 Apr 2020, 08:05

              • Login

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