PySide2 QListView QTableView sync problem





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















It is python/PySide2 interface related problem, as much as I tried, I couldn't make it sync (QListView and QTableView).



I will try to simplify it, instead of explaining the whole complicated thing which is much bigger with full of forms stuff in QT...



Just imagine to have a data structure (I guess, model) like the following:



dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}


I would like to have a form (or dialog), with 2 parts:



1) In the left of the form, to have a QListView to visualize the following:



*dict1
------
dict2
-----
dict3


NOTES:




  • The asterisk in the dict1 means that is selected.


  • The hyphens are just to separate between rows.



2) In the right of the form, to have a QTableView showing the following:



k1 | v1
-------
k2 | v2
-------
k3 | v3


NOTES:




  • The hyphens are just to separate between rows.


  • The pipes, are just to represent the columns separation.


  • Everytime you select the another element int the QListView, the QTableView must change to the one indicated by the original data structure.



I am sure it could be really easy for most of you guys, but I am just starting with UI stuff and MVC.










share|improve this question

























  • I recommend you go straight to the point, long speeches that do not contribute at all discourage you from helping.

    – eyllanesc
    Jan 3 at 8:06











  • I will, thanks!!

    – eddy2k
    Jan 3 at 8:10











  • I already told you that you should ask another question, please do it, you are only causing noise. In SO the questions should be as precise as possible, if you have many questions in a single post go against it. Remember that the button to create a post says Ask Question, does not say Ask Questions

    – eyllanesc
    Jan 3 at 9:02











  • so sorry, i am really new here, don't wanna bother

    – eddy2k
    Jan 3 at 9:04











  • It is also recommended that you review the SO guides: tour, How to Ask and How to Answer so that you know the dynamics of the place. Finally you probably can not ask questions until some time passes.

    – eyllanesc
    Jan 3 at 9:05


















1















It is python/PySide2 interface related problem, as much as I tried, I couldn't make it sync (QListView and QTableView).



I will try to simplify it, instead of explaining the whole complicated thing which is much bigger with full of forms stuff in QT...



Just imagine to have a data structure (I guess, model) like the following:



dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}


I would like to have a form (or dialog), with 2 parts:



1) In the left of the form, to have a QListView to visualize the following:



*dict1
------
dict2
-----
dict3


NOTES:




  • The asterisk in the dict1 means that is selected.


  • The hyphens are just to separate between rows.



2) In the right of the form, to have a QTableView showing the following:



k1 | v1
-------
k2 | v2
-------
k3 | v3


NOTES:




  • The hyphens are just to separate between rows.


  • The pipes, are just to represent the columns separation.


  • Everytime you select the another element int the QListView, the QTableView must change to the one indicated by the original data structure.



I am sure it could be really easy for most of you guys, but I am just starting with UI stuff and MVC.










share|improve this question

























  • I recommend you go straight to the point, long speeches that do not contribute at all discourage you from helping.

    – eyllanesc
    Jan 3 at 8:06











  • I will, thanks!!

    – eddy2k
    Jan 3 at 8:10











  • I already told you that you should ask another question, please do it, you are only causing noise. In SO the questions should be as precise as possible, if you have many questions in a single post go against it. Remember that the button to create a post says Ask Question, does not say Ask Questions

    – eyllanesc
    Jan 3 at 9:02











  • so sorry, i am really new here, don't wanna bother

    – eddy2k
    Jan 3 at 9:04











  • It is also recommended that you review the SO guides: tour, How to Ask and How to Answer so that you know the dynamics of the place. Finally you probably can not ask questions until some time passes.

    – eyllanesc
    Jan 3 at 9:05














1












1








1








It is python/PySide2 interface related problem, as much as I tried, I couldn't make it sync (QListView and QTableView).



I will try to simplify it, instead of explaining the whole complicated thing which is much bigger with full of forms stuff in QT...



Just imagine to have a data structure (I guess, model) like the following:



dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}


I would like to have a form (or dialog), with 2 parts:



1) In the left of the form, to have a QListView to visualize the following:



*dict1
------
dict2
-----
dict3


NOTES:




  • The asterisk in the dict1 means that is selected.


  • The hyphens are just to separate between rows.



2) In the right of the form, to have a QTableView showing the following:



k1 | v1
-------
k2 | v2
-------
k3 | v3


NOTES:




  • The hyphens are just to separate between rows.


  • The pipes, are just to represent the columns separation.


  • Everytime you select the another element int the QListView, the QTableView must change to the one indicated by the original data structure.



I am sure it could be really easy for most of you guys, but I am just starting with UI stuff and MVC.










share|improve this question
















It is python/PySide2 interface related problem, as much as I tried, I couldn't make it sync (QListView and QTableView).



I will try to simplify it, instead of explaining the whole complicated thing which is much bigger with full of forms stuff in QT...



Just imagine to have a data structure (I guess, model) like the following:



dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}


I would like to have a form (or dialog), with 2 parts:



1) In the left of the form, to have a QListView to visualize the following:



*dict1
------
dict2
-----
dict3


NOTES:




  • The asterisk in the dict1 means that is selected.


  • The hyphens are just to separate between rows.



2) In the right of the form, to have a QTableView showing the following:



k1 | v1
-------
k2 | v2
-------
k3 | v3


NOTES:




  • The hyphens are just to separate between rows.


  • The pipes, are just to represent the columns separation.


  • Everytime you select the another element int the QListView, the QTableView must change to the one indicated by the original data structure.



I am sure it could be really easy for most of you guys, but I am just starting with UI stuff and MVC.







python model-view-controller qtableview qlistview pyside2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 8:59









eyllanesc

86.9k103564




86.9k103564










asked Jan 3 at 7:58









eddy2keddy2k

447




447













  • I recommend you go straight to the point, long speeches that do not contribute at all discourage you from helping.

    – eyllanesc
    Jan 3 at 8:06











  • I will, thanks!!

    – eddy2k
    Jan 3 at 8:10











  • I already told you that you should ask another question, please do it, you are only causing noise. In SO the questions should be as precise as possible, if you have many questions in a single post go against it. Remember that the button to create a post says Ask Question, does not say Ask Questions

    – eyllanesc
    Jan 3 at 9:02











  • so sorry, i am really new here, don't wanna bother

    – eddy2k
    Jan 3 at 9:04











  • It is also recommended that you review the SO guides: tour, How to Ask and How to Answer so that you know the dynamics of the place. Finally you probably can not ask questions until some time passes.

    – eyllanesc
    Jan 3 at 9:05



















  • I recommend you go straight to the point, long speeches that do not contribute at all discourage you from helping.

    – eyllanesc
    Jan 3 at 8:06











  • I will, thanks!!

    – eddy2k
    Jan 3 at 8:10











  • I already told you that you should ask another question, please do it, you are only causing noise. In SO the questions should be as precise as possible, if you have many questions in a single post go against it. Remember that the button to create a post says Ask Question, does not say Ask Questions

    – eyllanesc
    Jan 3 at 9:02











  • so sorry, i am really new here, don't wanna bother

    – eddy2k
    Jan 3 at 9:04











  • It is also recommended that you review the SO guides: tour, How to Ask and How to Answer so that you know the dynamics of the place. Finally you probably can not ask questions until some time passes.

    – eyllanesc
    Jan 3 at 9:05

















I recommend you go straight to the point, long speeches that do not contribute at all discourage you from helping.

– eyllanesc
Jan 3 at 8:06





I recommend you go straight to the point, long speeches that do not contribute at all discourage you from helping.

– eyllanesc
Jan 3 at 8:06













I will, thanks!!

– eddy2k
Jan 3 at 8:10





I will, thanks!!

– eddy2k
Jan 3 at 8:10













I already told you that you should ask another question, please do it, you are only causing noise. In SO the questions should be as precise as possible, if you have many questions in a single post go against it. Remember that the button to create a post says Ask Question, does not say Ask Questions

– eyllanesc
Jan 3 at 9:02





I already told you that you should ask another question, please do it, you are only causing noise. In SO the questions should be as precise as possible, if you have many questions in a single post go against it. Remember that the button to create a post says Ask Question, does not say Ask Questions

– eyllanesc
Jan 3 at 9:02













so sorry, i am really new here, don't wanna bother

– eddy2k
Jan 3 at 9:04





so sorry, i am really new here, don't wanna bother

– eddy2k
Jan 3 at 9:04













It is also recommended that you review the SO guides: tour, How to Ask and How to Answer so that you know the dynamics of the place. Finally you probably can not ask questions until some time passes.

– eyllanesc
Jan 3 at 9:05





It is also recommended that you review the SO guides: tour, How to Ask and How to Answer so that you know the dynamics of the place. Finally you probably can not ask questions until some time passes.

– eyllanesc
Jan 3 at 9:05












1 Answer
1






active

oldest

votes


















0














You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.



from PySide2 import QtCore, QtGui, QtWidgets

dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}

def create_model_from_dict(d, parent=None):
model = QtGui.QStandardItemModel(0, 2, parent)
for k, v in dict_of_dicts.items():
it = QtGui.QStandardItem(k)
model.appendRow(it)
for k_, v_ in v.items():
it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
return model

class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
model = create_model_from_dict(dict_of_dicts, self)

self.tableview = QtWidgets.QTableView()
self.tableview.setModel(model)

self.listview = QtWidgets.QListView()
self.listview.setModel(model)
self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)

self.treeview = QtWidgets.QTreeView()
self.treeview.setModel(model)
self.treeview.expandAll()

hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.tableview)
hlay.addWidget(self.treeview)

@QtCore.Slot(QtCore.QItemSelection)
def handleSelectionChanged(self, item):
ixs = item.indexes()
if ixs:
self.tableview.setRootIndex(ixs[0])

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())


enter image description here



enter image description here



enter image description here






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018374%2fpyside2-qlistview-qtableview-sync-problem%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.



    from PySide2 import QtCore, QtGui, QtWidgets

    dict_of_dicts={
    'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
    'dict2':{'k4':'v4'},
    'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
    }

    def create_model_from_dict(d, parent=None):
    model = QtGui.QStandardItemModel(0, 2, parent)
    for k, v in dict_of_dicts.items():
    it = QtGui.QStandardItem(k)
    model.appendRow(it)
    for k_, v_ in v.items():
    it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
    return model

    class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
    super(Widget, self).__init__(parent)
    model = create_model_from_dict(dict_of_dicts, self)

    self.tableview = QtWidgets.QTableView()
    self.tableview.setModel(model)

    self.listview = QtWidgets.QListView()
    self.listview.setModel(model)
    self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
    self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)

    self.treeview = QtWidgets.QTreeView()
    self.treeview.setModel(model)
    self.treeview.expandAll()

    hlay = QtWidgets.QHBoxLayout(self)
    hlay.addWidget(self.listview)
    hlay.addWidget(self.tableview)
    hlay.addWidget(self.treeview)

    @QtCore.Slot(QtCore.QItemSelection)
    def handleSelectionChanged(self, item):
    ixs = item.indexes()
    if ixs:
    self.tableview.setRootIndex(ixs[0])

    if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())


    enter image description here



    enter image description here



    enter image description here






    share|improve this answer




























      0














      You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.



      from PySide2 import QtCore, QtGui, QtWidgets

      dict_of_dicts={
      'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
      'dict2':{'k4':'v4'},
      'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
      }

      def create_model_from_dict(d, parent=None):
      model = QtGui.QStandardItemModel(0, 2, parent)
      for k, v in dict_of_dicts.items():
      it = QtGui.QStandardItem(k)
      model.appendRow(it)
      for k_, v_ in v.items():
      it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
      return model

      class Widget(QtWidgets.QWidget):
      def __init__(self, parent=None):
      super(Widget, self).__init__(parent)
      model = create_model_from_dict(dict_of_dicts, self)

      self.tableview = QtWidgets.QTableView()
      self.tableview.setModel(model)

      self.listview = QtWidgets.QListView()
      self.listview.setModel(model)
      self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
      self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)

      self.treeview = QtWidgets.QTreeView()
      self.treeview.setModel(model)
      self.treeview.expandAll()

      hlay = QtWidgets.QHBoxLayout(self)
      hlay.addWidget(self.listview)
      hlay.addWidget(self.tableview)
      hlay.addWidget(self.treeview)

      @QtCore.Slot(QtCore.QItemSelection)
      def handleSelectionChanged(self, item):
      ixs = item.indexes()
      if ixs:
      self.tableview.setRootIndex(ixs[0])

      if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication(sys.argv)
      w = Widget()
      w.show()
      sys.exit(app.exec_())


      enter image description here



      enter image description here



      enter image description here






      share|improve this answer


























        0












        0








        0







        You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.



        from PySide2 import QtCore, QtGui, QtWidgets

        dict_of_dicts={
        'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
        'dict2':{'k4':'v4'},
        'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
        }

        def create_model_from_dict(d, parent=None):
        model = QtGui.QStandardItemModel(0, 2, parent)
        for k, v in dict_of_dicts.items():
        it = QtGui.QStandardItem(k)
        model.appendRow(it)
        for k_, v_ in v.items():
        it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
        return model

        class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        model = create_model_from_dict(dict_of_dicts, self)

        self.tableview = QtWidgets.QTableView()
        self.tableview.setModel(model)

        self.listview = QtWidgets.QListView()
        self.listview.setModel(model)
        self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
        self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)

        self.treeview = QtWidgets.QTreeView()
        self.treeview.setModel(model)
        self.treeview.expandAll()

        hlay = QtWidgets.QHBoxLayout(self)
        hlay.addWidget(self.listview)
        hlay.addWidget(self.tableview)
        hlay.addWidget(self.treeview)

        @QtCore.Slot(QtCore.QItemSelection)
        def handleSelectionChanged(self, item):
        ixs = item.indexes()
        if ixs:
        self.tableview.setRootIndex(ixs[0])

        if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())


        enter image description here



        enter image description here



        enter image description here






        share|improve this answer













        You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.



        from PySide2 import QtCore, QtGui, QtWidgets

        dict_of_dicts={
        'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
        'dict2':{'k4':'v4'},
        'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
        }

        def create_model_from_dict(d, parent=None):
        model = QtGui.QStandardItemModel(0, 2, parent)
        for k, v in dict_of_dicts.items():
        it = QtGui.QStandardItem(k)
        model.appendRow(it)
        for k_, v_ in v.items():
        it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
        return model

        class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        model = create_model_from_dict(dict_of_dicts, self)

        self.tableview = QtWidgets.QTableView()
        self.tableview.setModel(model)

        self.listview = QtWidgets.QListView()
        self.listview.setModel(model)
        self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
        self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)

        self.treeview = QtWidgets.QTreeView()
        self.treeview.setModel(model)
        self.treeview.expandAll()

        hlay = QtWidgets.QHBoxLayout(self)
        hlay.addWidget(self.listview)
        hlay.addWidget(self.tableview)
        hlay.addWidget(self.treeview)

        @QtCore.Slot(QtCore.QItemSelection)
        def handleSelectionChanged(self, item):
        ixs = item.indexes()
        if ixs:
        self.tableview.setRootIndex(ixs[0])

        if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())


        enter image description here



        enter image description here



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 8:40









        eyllanesceyllanesc

        86.9k103564




        86.9k103564
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018374%2fpyside2-qlistview-qtableview-sync-problem%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith