Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved How to create Container for multiple QWidget?

    General and Desktop
    2
    6
    197
    Loading More Posts
    • 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
      npatil15 last edited by npatil15

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 2
        • N
          npatil15 last edited by

          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 :)

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @npatil15 last edited by mrjj

            @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 Reply Quote 1
            • N
              npatil15 last edited by

              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?

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @npatil15 last edited by

                @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 Reply Quote 0
                • First post
                  Last post