4

I need some advise about how to read PyQt's documentation. Because on my own I can hardly figure anything out. I'm a programming newbie so sorry if my question is confusing. I'll try to explain the best I can :)

This is an example of where I got stuck. I was experimenting around with QListView. Basically just trying to print out data of what I have selected in the view. I got stuck until Justin, a very patient Python tutor, showed me this bit of code.

listView.clicked.connect(B)
def B(index):
    record = sqlmodel.record(index.row())

It connects a clicked signal from QListView to function B. I was very surprised that right away the clicked event sends index to B by itself. I tried to look through QListView's documentation but can't find anything that explains this.

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qlistview.html

In this case, where in the docs should I look at to learn about this clicked event, and the index it sends out?

Would really appreciate any advise. :)

Panupat
  • 452
  • 6
  • 21
  • I don't fully understand the problem. Could you please clarify? Why would you need to emit anything. Is there more code that would give me a context to understand the question better? – batbrat Jan 22 '12 at 17:23
  • hi barber. I'll try to word my question a little better. Sorry, I'm really a beginner and not sure how to word everything. – Panupat Jan 22 '12 at 18:28

2 Answers2

4

Unfortunately PyQt documentation is not full. Better source for knowledge is Qt documentation.

In your case QTableView inherits QAbstractItemView, and there is no clicked signal in QTableView docs, you can find it in List of all members, including inherited members page. And you can see, that this signal comes from QAbstractItemView and it's defined as:

void QAbstractItemView::clicked ( const QModelIndex & index )

Here you can see type of function arguments (clickable).

So, index passed to you function will be instance of QModelIndex and it has row method.

If you are confused with C++ syntax, another option is to use PySide documentation, which is more Python friendly.

QAbstractItemView.clicked in PySide docs

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
reclosedev
  • 9,352
  • 34
  • 51
  • wow that list of members page is awesome. . . can you help explain the (const QModelIndex & index ) a bit? It means it returns 2 values, a QModelIndex object and another index? – Panupat Jan 22 '12 at 18:50
  • 1
    @Panupat, In C++ `const QModuleIndex & index` means that function recieves object of `QMouduleIndex` type, named `index` as a parameter. If you will look to `QModelIndex` page, you'll find `row` method from your question. Another option for learning `PyQt` is using `PySide` documentation [QAbstractItemView.clicked](http://www.pyside.org/docs/pyside/PySide/QtGui/QAbstractItemView.html#PySide.QtGui.PySide.QtGui.QAbstractItemView.clicked) – reclosedev Jan 22 '12 at 18:57
  • Thanks for the PySide link. I'll study it. :) Still curious tho, how can function B receive the `index` too? The clicked event receives it and then sends it over? Is there anywhere in the doc that explains this? – Panupat Jan 22 '12 at 19:15
  • 2
    @Panupat, Maybe you'll find this document useful [New-style Signal and Slot Support](http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/new_style_signals_slots.html) – reclosedev Jan 22 '12 at 19:23
2

The following code connects QTableView's clicked signal to your function. QTableView emits clicked whenever someone clicks an item, which means that your function will be called automatically, since it's connected to that signal.

listView.clicked.connect(viewItemClicked)

Or am I missing something in your question? Read up on signal-slots in Qt if the above is unclear. (PyQt allows any function (i.e. python callable) to be connected to a signal, not just a slot (as it is in C++).

Macke
  • 24,812
  • 7
  • 82
  • 118
  • Hi Macke. Thanks for your response. I need some tips about reading the docs. I edit my question for better wording. Sorry if I was unclear. – Panupat Jan 22 '12 at 18:43