QtTableWidget to fire of a function
Hiya,
I'm studying QtTableWidget. I've been going through the docs, and I'd like to know a few things that I wasn't able to understand.
I have a table with only 1 column and 8 rows. Each cell filled with a delicious fruit as item. I have a function that just prints "you clicked..." And then the name of the fruit. Currently I have a push button that makes use of the .currentItem() to get the cell I have selected and fires off the function.
What I'd like to know:
1) can I get rid of the button and just have the click fire of the function? So, I have the table, I click on 'Apple' cell and it fires off the function.
2) how do I setup this table so when I move my mouse over a cell, it highlights. And when I move my mouse away, it dims.
3) I didn't see anything about right-click context in the docs(table widget section) is there another class that handles that magic?
4) I have auto-scroll set to true. How would I go about setting the thickness of the scroll bar?
5) can I add images and video in each cell? Like a thumbnail.
6) how would I change the cell size? Can I tell the cells to be, say, 20x20 pixels?
I'm using PyQt
Cheers!
Re: QtTableWidget to fire of a function
Quote:
Originally Posted by
Nfrancisj
1) can I get rid of the button and just have the click fire of the function? So, I have the table, I click on 'Apple' cell and it fires off the function.
Yes, the tablewidget has a signal for that: QTableWidget::itemClicked().
Quote:
Originally Posted by
Nfrancisj
2) how do I setup this table so when I move my mouse over a cell, it highlights. And when I move my mouse away, it dims.
You could try with QTableWidget::itemEntered() but it might need more sophisticated mouse event handling.
Quote:
Originally Posted by
Nfrancisj
3) I didn't see anything about right-click context in the docs(table widget section) is there another class that handles that magic?
There are several options to do a context menu.
One is to add actions to the widget and set the context menu policy to ActionContextMenu.
Another is to connect to the contextMenuRequested() signal and set the context menu policy appropriately.
Quote:
Originally Posted by
Nfrancisj
4) I have auto-scroll set to true. How would I go about setting the thickness of the scroll bar?
What is the thickness? The width of a vertical scrollbar?
Quote:
Originally Posted by
Nfrancisj
5) can I add images and video in each cell? Like a thumbnail.
Image should be possible via QTableWidgetItem::setIcon(), or setting the filename as item data and using a custom item delegate for painting or setting a suitable cell widget.
Quote:
Originally Posted by
Nfrancisj
6) how would I change the cell size? Can I tell the cells to be, say, 20x20 pixels?
Also a couple of options, e.g. a custom item delegate that reports that as its sizeHint or changing the column size through the horizontal header view, respectively the row height through the vertical header view.
Cheers,
_
Re: QtTableWidget to fire of a function
Thanks! Super Helpful!
is there a better way to get items from the table?
currently im using: 'Item' and specifying a row and column
current_Item = self.tableWidget.item(self.tableWidget.row(self.tableWidget.currentItem()),self.tableWidget.column(self.tableWidget.currentItem()))
Re: QtTableWidget to fire of a function
Depends on what you have at that moment.
If you have row/column, then item(row, column) is a good choice.
In your example it is just wasted lookups, currentItem() is already the item.
Cheers,
_
Re: QtTableWidget to fire of a function
CurrentItem returns the object at a memory location. How would I get the text? I'm not in front of a computer right now, but will this work?
current_Item = self.tableWidget.currentItem().text()
Btw...how's Austria this time of year? Where did you learn all this Qt stuff? :D
Cheers!
Re: QtTableWidget to fire of a function
Quote:
Originally Posted by
Nfrancisj
CurrentItem returns the object at a memory location.
Yes, just like item(row, column)
Quote:
Originally Posted by
Nfrancisj
How would I get the text? I'm not in front of a computer right now, but will this work?
The same way you do that now, this is just a different way of getting at the item object
Quote:
Originally Posted by
Nfrancisj
current_Item = self.tableWidget.currentItem().text()
Exactly.
Quote:
Originally Posted by
Nfrancisj
Btw...how's Austria this time of year?
Mostly cold :)
Quote:
Originally Posted by
Nfrancisj
Where did you learn all this Qt stuff? :D
Working on various projects at KDE and trying to come up with solutions for problems people have on forums.
Cheers,
_
Re: QtTableWidget to fire of a function
Quote:
Originally Posted by
anda_skoa
I'm having trouble finding info on itemClicked.
I saw this:
[signal] void QTableWidget::itemDoubleClicked(QTableWidgetItem * item)
But how do I implement it? Say I have a function:
def myPrint():
print ('hooray')
How do I get itemClicked to fire this function?
Secondly... What is QTableWidgetItem * item? What kind of info does it have?
Is there a benefit to using the signals and slot mechanism rather than the code style.
So example:
self.PushButton1.clicked.connect(myPrint()) VS ui.connect(PushButton1, Signal(clicked), Slot ...)
any benefit using one over the other?
How do I call the myPrint function using Signal/Slot method on Pushbutton1? Or can I?
Re: QtTableWidget to fire of a function
Quote:
Originally Posted by
Nfrancisj
I'm having trouble finding info on itemClicked.
I saw this:
[signal] void QTableWidget::itemDoubleClicked(QTableWidgetItem * item)
There is also QTableWidget::itemClicked().
Quote:
Originally Posted by
Nfrancisj
But how do I implement it? Say I have a function:
def myPrint():
print ('hooray')
How do I get itemClicked to fire this function?
You connect this function to the signal
Quote:
Originally Posted by
Nfrancisj
Secondly... What is QTableWidgetItem * item? What kind of info does it have?
That is a pointer to a QTableWidgetItem with the name "item".
In Python that will likely be an object reference to a QTableWidgetItem object.
Quote:
Originally Posted by
Nfrancisj
Is there a benefit to using the signals and slot mechanism rather than the code style.
So example:
self.PushButton1.clicked.connect(myPrint()) VS ui.connect(PushButton1, Signal(clicked), Slot ...)
any benefit using one over the other?
I guess the second one is more like it is done in C++, but the first one will probably be easier for other Python people to understand.
Quote:
Originally Posted by
Nfrancisj
How do I call the myPrint function using Signal/Slot method on Pushbutton1? Or can I?
I am afraid I don't understand, you have the connect right there, no?
Cheers,
_
Re: QtTableWidget to fire of a function
I think I'm confusing myself. Hahaha I'm watching Qt C++ vids on YouTube and trying to find the equivalent in PyQt/PySide.
I apologize. :D
Let me start again:
I have a function. Same one above, myPrint()
Using the non signal method, this works:
ui.pushbutton1.clicked.connect(myPrint())
Now I'm trying to see if I can get it using Signals&Slots.
This doesn't work.
connect(ui.pushbutton1, SIGNAL(clicked()), ui.pushbutton1, SLOT(myPrint()))
Neither does this...
connect(ui.pushbutton1, SIGNAL(clicked()), myPrint())
What is the correct syntax?
Thanks!
Re: QtTableWidget to fire of a function
Quote:
Originally Posted by
Nfrancisj
I
Using the non signal method, this works:
ui.pushbutton1.clicked.connect(myPrint())
If that works, why not use it?
Quote:
Originally Posted by
Nfrancisj
I
Now I'm trying to see if I can get it using Signals&Slots.
This doesn't work.
connect(ui.pushbutton1, SIGNAL(clicked()), ui.pushbutton1, SLOT(myPrint()))
myPrint is not a function of pushbutton1, right?
Quote:
Originally Posted by
Nfrancisj
I
Neither does this...
connect(ui.pushbutton1, SIGNAL(clicked()), myPrint())
There is no SLOT() in this line.
You might also have to specify "self" as the receiver object.
Cheers,
_
Re: QtTableWidget to fire of a function
Quote:
Originally Posted by
anda_skoa
If that works, why not use it?
_
There are something's easier in slot method. I'm just trying to learn all the possibles just to be flexible. :D
Quote:
Originally Posted by
anda_skoa
There is no SLOT() in this line.
_
That's what I thought when I saw this: @time 13:32, Line 23
http://youtu.be/0vvb7Kv59qA
Then I tried it in my script, and it didn't work. :( I even duplicated the setup he has. I used QtDesigner instead of writing it out,but I can't seem to understand what he is doing different to make line 23 work. :confused:
Hahaha, there's another guy in Spain, I'm trying to find his video again, who does Signals and slots in a different way also. He uses some weird syntax.
By the way, I'm not doing any projects or anything. just studying QT in my spare time. :D and I thank you very much, again, for helping me understand things.
cheers!
Re: QtTableWidget to fire of a function
I figured it out. I need to write it this way:
QtCore.QObject.connect(self.myButton, QtCore.SIGNAL('clicked()'), myFunction)
The clouds have cleared, and now everything hooks up well. :D