Results 1 to 12 of 12

Thread: QtTableWidget to fire of a function

  1. #1
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtTableWidget to fire of a function

    Quote Originally Posted by Nfrancisj View Post
    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 View Post
    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 View Post
    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 View Post
    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 View Post
    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 View Post
    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,
    _

  3. #3
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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()))

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Wink 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?

    Cheers!
    Last edited by Nfrancisj; 17th February 2016 at 10:28.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtTableWidget to fire of a function

    Quote Originally Posted by Nfrancisj View Post
    CurrentItem returns the object at a memory location.
    Yes, just like item(row, column)

    Quote Originally Posted by Nfrancisj View Post
    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 View Post
    current_Item = self.tableWidget.currentItem().text()
    Exactly.

    Quote Originally Posted by Nfrancisj View Post
    Btw...how's Austria this time of year?
    Mostly cold

    Quote Originally Posted by Nfrancisj View Post
    Where did you learn all this Qt stuff?
    Working on various projects at KDE and trying to come up with solutions for problems people have on forums.

    Cheers,
    _

  7. #7
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtTableWidget to fire of a function

    Quote Originally Posted by anda_skoa View Post
    Yes, the tablewidget has a signal for that: QTableWidget::itemClicked().
    _
    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?
    Last edited by Nfrancisj; 18th February 2016 at 09:03.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtTableWidget to fire of a function

    Quote Originally Posted by Nfrancisj View Post
    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 View Post
    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 View Post
    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 View Post
    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 View Post
    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,
    _

  9. #9
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    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!

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtTableWidget to fire of a function

    Quote Originally Posted by Nfrancisj View Post
    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 View Post
    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 View Post
    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,
    _

  11. #11
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtTableWidget to fire of a function

    Quote Originally Posted by anda_skoa View Post
    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.

    Quote Originally Posted by anda_skoa View Post
    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.

    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. and I thank you very much, again, for helping me understand things.

    cheers!

  12. #12
    Join Date
    Feb 2016
    Location
    Venice, California
    Posts
    87
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. QSocketNorifier on stdin dont fire signal WinXP
    By dexli in forum Qt Programming
    Replies: 2
    Last Post: 11th December 2013, 10:38
  2. Signal is emtted, slot does not fire.
    By JasonKretzer in forum Newbie
    Replies: 3
    Last Post: 1st August 2013, 21:40
  3. QSqlTableModel OnFieldChange causes what signal to fire?
    By scott_hollen in forum Qt Programming
    Replies: 0
    Last Post: 27th September 2011, 20:23
  4. fire and forget way for QProcess
    By qt_gotcha in forum Newbie
    Replies: 2
    Last Post: 17th August 2010, 19:42
  5. QObject::connect exception fire
    By names in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2010, 07:59

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.