Results 1 to 7 of 7

Thread: Link two models in PyQt

  1. #1
    Join Date
    Feb 2015
    Location
    India
    Posts
    4
    Thanks
    3
    Qt products
    Platforms
    Unix/X11 Windows

    Question Link two models in PyQt

    I'm new to python programming.Learning on own, with google and you guys help.

    Now working on a API project.need to query a list of tasks on UI and on selection of item It should query details of that task.

    Its working that way.But I want to keep this task execution in background and show other naming on UI.

    Here is detailed picture
    Ng3aT.png


    Screen shot of my UI





    Thanks to Yasin for Dark Orange theme.

    My idea is,
    we query tasks,shots seperately and feed them in seperate models.
    will show display_list.
    If I click on Proj_Shot_Dept_002,its index value is 1.. so I want to send tasklist(index of 1) as signal parameter.
    I'm googling got some possible ways like "widget dta mapper" or a delegate method..
    But Im very new to Python n PyQt..so still couldn't get way to solve it.


    In simple words, I want to show Display list model on UI but when User click,that signal should send tasklist item to execute next query. How can I access it?
    or is there any other way to do it?

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Link two models in PyQt

    What kind of model are you using, qstandarditemmodel or a custom made model?
    If you are using a standarditem model you can use setData ( const QModelIndex & index, pointer to you're detail data class, Qt::UserRole )
    when you click a item you can query data ( the index of the selected item, Qt::UserRole) this will return a QVariant with the pointer to the detail data class, then you can forward this pointer to the detail view.

    to get the clicked item you can connect to the clicked ( const QModelIndex & index ) sinal of the listview
    Last edited by StrikeByte; 25th February 2015 at 09:58.

  3. The following user says thank you to StrikeByte for this useful post:

    kiran_g4e (25th February 2015)

  4. #3
    Join Date
    Feb 2015
    Location
    India
    Posts
    4
    Thanks
    3
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Link two models in PyQt

    @StrikeByte,
    Thankyou for reply

    It's not qstandarditemmodel.
    I'm verymuch new to programming,I could not understand userRole
    can you explain me a bit?

    I'm posting my code for better view.

    Qt Code:
    1. base, form = uic.loadUiType("uiMainWindow.ui")
    2. class ArtistUi(base, form):
    3. def __init__(self, parent=None):
    4. super(base, self).__init__(parent)
    5. self.setupUi(self)
    6.  
    7. Task = MyTasks()
    8. Task_num = Task.getTask() # List of tasks
    9. Shot_num2 = Task.getShot() # list of shots
    10.  
    11.  
    12. taskModel = QtGui.QStringListModel(Task_num)
    13. shotModel = QtGui.QStringListModel(Shot_num2)
    14.  
    15. self.mytasks.setModel(taskModel)
    16.  
    17. self.show()
    18.  
    19. self.connect(self.mytasks, QtCore.SIGNAL("clicked(QModelIndex)"), self.Task_Detail)
    20. # Task_Detail function will query using Task_num and displays in details listview
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Link two models in PyQt

    For each data point in a model there are different roles, each role can contain data
    Some of the roles are predefined like Qt:: DecorationRole, you can use it for decoration puposes, the Qt::UserRole is predefined for the users own data (like a pointer to the detail info)
    So one item in the model can have Qt::EditRole, Qt:: DisplayRole and Qt::UserRole data.

    I don't know if the code below is the way it should be used in python, I'm a c++ programmer and have little experience with python

    In your case
    setting data:
    taskModel.setData(QModelIndex of the selected item or newly created item,QVariant.fromValue(TaskDetailData),Qt::UserRole);

    getting data:
    taskModel.data(QModelIndex of the selected item),Qt::UserRole); //this returns a QVariant with a pointer to the data. Please check if it is a valid one!!!!

  6. The following user says thank you to StrikeByte for this useful post:

    kiran_g4e (26th February 2015)

  7. #5
    Join Date
    Feb 2015
    Location
    India
    Posts
    4
    Thanks
    3
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Link two models in PyQt

    Thank you.I know that DisplayRole.
    Now I got little clarity about userRoles.
    Thanks for your time,but implementation doesn't work this way.
    I need to figureout for pythonic way or I should change my model type.
    I will update once I get i done

  8. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Link two models in PyQt

    Try this way

    1. The slot (or function) Task_Detail should take a parameter of type QModelIndex
    2. Using this QModelIndex you can get the index of the shot that was clicked, first get the shot name using modelIndex.data().toString(), and the find the index of this string in the Shot_num2.

    That should solve your problem. If it still does not help sit back run and understand the code below.

    It is in C++, it should be trivial to port it to Py

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class ArtistUi : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. ArtistUi(QWidget * parent = 0)
    9. : QWidget(parent)
    10. {
    11. taskList = QStringList() << "task001" << "task002" << "task007" << "task134" << "task591";
    12. shotList = QStringList() << "Proj_Shot_dept_002" << "Proj_Shot_dept_004" << "Proj_Shot_dept_005";
    13.  
    14. taskModel = new QStringListModel(taskList);
    15. shotModel = new QStringListModel(shotList);
    16.  
    17. mytasks = new QListView();
    18. myshots = new QListView();
    19.  
    20. mytasks->setModel(taskModel);
    21. myshots->setModel(shotModel);
    22.  
    23. mytasks->show();
    24. myshots->show();
    25.  
    26. mytasks->setEditTriggers(QListView::NoEditTriggers);
    27. myshots->setEditTriggers(QListView::NoEditTriggers);
    28.  
    29. output = new QLabel();
    30.  
    31. QGridLayout * layout = new QGridLayout(this);
    32. layout->addWidget(mytasks, 0, 0, 1, 1);
    33. layout->addWidget(myshots, 0, 1, 1, 1);
    34. layout->addWidget(output, 1, 0, 1, 2, Qt::AlignHCenter);
    35.  
    36. currentTaskIndex = -1;
    37.  
    38. connect(mytasks, SIGNAL(clicked(QModelIndex)), this, SLOT(on_mytasks_clicked(QModelIndex)));
    39. connect(myshots, SIGNAL(clicked(QModelIndex)), this, SLOT(on_myshots_clicked(QModelIndex)));
    40. }
    41.  
    42. private slots:
    43. void on_mytasks_clicked(const QModelIndex & index)
    44. {
    45. int i = taskList.indexOf(index.data().toString());
    46.  
    47. if(i != -1)
    48. {
    49. currentTaskIndex = i;
    50. }
    51. }
    52.  
    53. void on_myshots_clicked(const QModelIndex & index)
    54. {
    55. if(currentTaskIndex == -1)
    56. {
    57. output->setText("No Task Selected");
    58. }
    59. else
    60. {
    61. output->setText(index.data().toString() + " and " + taskList.at(currentTaskIndex));
    62. }
    63. }
    64.  
    65. private:
    66. QStringList taskList;
    67. QStringList shotList;
    68.  
    69. QStringListModel * taskModel;
    70. QStringListModel * shotModel;
    71.  
    72. QListView * mytasks;
    73. QListView * myshots;
    74.  
    75. QLabel * output;
    76.  
    77. int currentTaskIndex;
    78. };
    79.  
    80. int main(int argc, char *argv[])
    81. {
    82. QApplication app(argc, argv);
    83.  
    84. ArtistUi w;
    85. w.show();
    86.  
    87. return app.exec();
    88. }
    89.  
    90. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    kiran_g4e (3rd March 2015)

  10. #7
    Join Date
    Feb 2015
    Location
    India
    Posts
    4
    Thanks
    3
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Link two models in PyQt

    Thankyou Verymuch .
    I will work on this

Similar Threads

  1. Replies: 0
    Last Post: 29th January 2015, 10:55
  2. Replies: 11
    Last Post: 5th May 2011, 14:05
  3. How to link a Push Button with a Web link ???
    By prajnaranjan.das in forum Newbie
    Replies: 3
    Last Post: 10th December 2010, 07:25
  4. Replies: 1
    Last Post: 13th May 2009, 03:18
  5. Proxy models and slection models
    By amunitz in forum The Model-View Framework
    Replies: 1
    Last Post: 23rd September 2008, 14:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.