Results 1 to 17 of 17

Thread: Adding items to QlistWidget or QtableWidget

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

    Default Adding items to QlistWidget or QtableWidget

    In my quest to create something like this: https://vimeo.com/110260781

    I have an object, a boxLayout, with a few widgets inside- a label, that acts as a image player, a label that shows the frame count, and a slider. I have interactivity setup just like the video where I have mouse over events and timeline start and stop functions.

    First of, which should I use? QlistWidget or QtableWidget? or is there something more appropriate for this case?

    secondly, how would I go about implementing something like this? How do I add my template/boxLayout to each cell of the table.

    Thank You!

  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: Adding items to QlistWidget or QtableWidget

    As a first try I would go for a QGridLayout on a widget in a QScrollArea.

    Cheers,
    _

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

    Default Re: Adding items to QlistWidget or QtableWidget

    So this is what I have. I'm trying your last comment Anda, but I'm doing something wrong.
    I have a few labels, which I put inside a QGridLayout, and then I'm trying to put it into the scroll area, but I'm getting an error on line 34
    The Error says
    TypeError: scrollArea.setWidget(QWidget) : argument 1 has unexpected type (QGridLayout)
    Qt Code:
    1. class ViewerLabel(QLabel):
    2. def __init__(self, parent=None):
    3. super(ViewerLabel, self).__init__(parent)
    4. self.setMouseTracking(True)
    5. self.setAlignment(Qt.AlignCenter)
    6.  
    7. def enterEvent(self,event):
    8. button.setStyleSheet("background-color:#45b545;")
    9.  
    10. def leaveEvent(self,event):
    11. button.setStyleSheet("background-color:yellow;")
    12.  
    13. def main():
    14.  
    15. layout = QGridLayout()
    16. scrollArea = QScrollArea()
    17.  
    18. label_01 = ViewerLabel(“label 01”)
    19. label_02 = ViewerLabel(“label 02”)
    20. label_03 = ViewerLabel(“label 03”)
    21. label_04 = ViewerLabel(“label 04”)
    22. label_05 = ViewerLabel(“label 05”)
    23. label_06 = ViewerLabel(“label 06”)
    24.  
    25. layout.addwidget(label_01, 0, 1)
    26. layout.addwidget(label_02, 0, 2)
    27. layout.addwidget(label_03, 1, 1)
    28. layout.addwidget(label_04, 1, 2)
    29. layout.addwidget(label_05, 2, 1)
    30. layout.addwidget(label_06, 2, 2)
    31.  
    32. layout.setSpacing(10)
    33.  
    34. scrollArea.setWidget(layout)
    35. window.setLayout(scrollArea)
    36.  
    37. window.setFixedSize(400,500)
    38. window.show()
    39.  
    40. return app.exec_()
    41.  
    42.  
    43. if __name__ == __main__:
    44. main()
    To copy to clipboard, switch view to plain text mode 

  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: Adding items to QlistWidget or QtableWidget

    "layout" is not a QWidget, QScrollArea::setWidget expects one.

    Cheers,
    _

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

    Default Re: Adding items to QlistWidget or QtableWidget

    Right! I should have know that! Haha. So I need to use .setLayout instead of .setWidget.

    Thanks Again!
    Last edited by Nfrancisj; 9th May 2016 at 17:15.

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

    Default Re: Adding items to QlistWidget or QtableWidget

    Nope...well that didn't work for me either. Now I get an error on window.setLayout because that's expecting a layout, not widget.

    So how do I do this? How do I get a GridLayout to work with ScrollArea. I also tried this in the designer, and it kinda didn't work either. I added the scroll to the form, buttons inside the scroll, and set the scroll to GridLayout. The scroll did size with the window, but the scroll bars didn't appear.

  7. #7
    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: Adding items to QlistWidget or QtableWidget

    Quote Originally Posted by Nfrancisj View Post
    Right! I should have know that! Haha. So I need to use .setLayout instead of .setWidget.
    No.

    setWidget() is correct, it sets the content widget of the scrollarea.
    setLayout() would change the layout of the scrollarea, you definitely don't want that.

    As I said in my initial reply, add a widget to the scroll area, put the buttons onto that widget using an appropriate layout.

    Cheers,
    _

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

    Default Re: Adding items to QlistWidget or QtableWidget

    add a widget to the scroll area, put the buttons onto that widget using an appropriate layout.
    I think I'm getting confused on the term "widget" you use. When you say "widget", what are you referring to?

    Thanks!


    Added after 10 minutes:


    So I have this..

    image.jpg

    Can I not use QGridLayout here instead of QVBox? Everytime I complie, the designer crashes. With other layout types it works fine.
    Last edited by Nfrancisj; 10th May 2016 at 07:15.

  9. #9
    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: Adding items to QlistWidget or QtableWidget

    Quote Originally Posted by Nfrancisj View Post
    I think I'm getting confused on the term "widget" you use. When you say "widget", what are you referring to?
    An instance of QWidget.

    Cheers,
    _

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

    Default Re: Adding items to QlistWidget or QtableWidget

    Just to re-word, because I'm a still a bit confused...I apologize.
    You mean create the buttons, add them to a layout, then add the layout to the scrollArea?

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

    Default Re: Adding items to QlistWidget or QtableWidget

    By the way Anda, does QLabel not have a ".setDragEnabled"? I'm trying to do a drag and drop on a QLabel that has a Pixmap. Ideas on how I can activate drag drop on QLabel? I see "acceptDrops" but no drag.

    Thanks

  12. #12
    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: Adding items to QlistWidget or QtableWidget

    Quote Originally Posted by Nfrancisj View Post
    You mean create the buttons, add them to a layout, then add the layout to the scrollArea?
    No, you already tried that.

    The scrollarea needs a content widget to work on.
    That widget can be anything, including a layouted container for other widgets.

    Quote Originally Posted by Nfrancisj View Post
    By the way Anda, does QLabel not have a ".setDragEnabled"?
    Yes, that's right.
    Usually labels aren't sources for drags.

    Quote Originally Posted by Nfrancisj View Post
    I'm trying to do a drag and drop on a QLabel that has a Pixmap. Ideas on how I can activate drag drop on QLabel? I see "acceptDrops" but no drag.
    So you want to drag a pixmap from one label to another?

    Cheers,
    _

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

    Default Re: Adding items to QlistWidget or QtableWidget

    The scrollarea needs a content widget to work on.
    this is exactly was i was missing! Now I understand. Thankyou!

    So you want to drag a pixmap from one label to another?
    I think so. If you take a look at this video, at time 40secs, the user drags the highlighted images into the bookmark section. (https://vimeo.com/110260781)
    I am using a Qlabel with a Pixmap added to it. I was hoping to be able to drag the label object into another layout like in the vid. Looks like a QVBoxLayout possibly.
    So, if i cant drag the Qlabel, Can i drag the Pixmap and add it to another label? This would mean on drop event, I would have to create a label in the bookmarks section and apply the Pixmap to that label, right?

    thanks!

  14. #14
    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: Adding items to QlistWidget or QtableWidget

    For starting the drag you'll have to implement some mouse handling, e.g. get the mouse down and mouse move and decide to start a drag based on the moved distance, see QStyleHints::startDragDistance().

    You then create a QDrag object, set the pixmap and add whatever data you'll at the drop location to correctly "insert".

    For the drop you implement the drag and drop methods on the target widget.

    Cheers,
    _

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

    Default Re: Adding items to QlistWidget or QtableWidget

    Ive started a new thread regarding drag/drop issue, just to keep things clean for others. I hope you will join me there Anda.
    http://www.qtcentre.org/threads/6604...646#post290646


    If anyone is interested in how I set my code for this topic, please feel free to copy it from below.


    Qt Code:
    1. from PyQt4.QtGui import *
    2. from PyQt4.QtCore import *
    3. import sys
    4.  
    5.  
    6. class Label_Template(QLabel):
    7. def __init__(self,setText):
    8. super(Label_Template, self).__init__()
    9. self.setFrameShape(QFrame.StyledPanel)
    10. self.setAlignment(Qt.AlignCenter)
    11. self.setMinimumHeight(100)
    12. self.setMinimumWidth(100)
    13. self.setStyleSheet("background-color:yellow;")
    14. self.setText(setText)
    15.  
    16. def enterEvent(self,event):
    17. self.setStyleSheet("background-color:#45b545;")
    18.  
    19. def leaveEvent(self,event):
    20. self.setStyleSheet("background-color:yellow;")
    21.  
    22.  
    23. class ScrollAreaExample(QDialog):
    24. def __init__(self):
    25. super(ScrollAreaExample, self).__init__()
    26.  
    27. self.scrollArea = QScrollArea() # Create Scroll Area
    28. self.gridLayout_4ScrollArea = QGridLayout() # Create Grid Layout for Scroll
    29. self.gridLayout_4labels = QGridLayout() # Create Grid Layout for Labels
    30. self.scrollArea_Container = QWidget() #Create a Container for Widets that will go into the Scroll Area
    31.  
    32.  
    33.  
    34. #self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    35. #self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    36.  
    37.  
    38. #Create Stuff
    39. self.numRows = 5
    40. self.numColumns = 3
    41.  
    42. for self.columns in range(0, self.numColumns):
    43. for self.rows in range(0,self.numRows):
    44. self.gridLayout_4labels.addWidget(Label_Template(str(self.rows+self.columns)), self.rows, self.columns)
    45.  
    46.  
    47.  
    48. # Add Labels layout to Scroll Area Container
    49. self.scrollArea_Container.setLayout(self.gridLayout_4labels)
    50.  
    51. # Add Scroll Area Container to Scroll Area
    52. self.scrollArea.setWidget(self.scrollArea_Container)
    53.  
    54. # Add Scroll Area to a Grid Layout
    55. self.gridLayout_4ScrollArea.addWidget(self.scrollArea)
    56.  
    57. # Add Grid Layout to Window (this class)
    58. self.setLayout(self.gridLayout_4ScrollArea)
    59.  
    60.  
    61.  
    62. if __name__ == "__main__":
    63. app = QApplication(sys.argv)
    64. window = ScrollAreaExample()
    65. window.show()
    66. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Adding items to QlistWidget or QtableWidget

    Im trying to change the thickness of the scrollbars i've created using the code above. I saw that list and table widets have a method called autoScrollMargin, but i cant seem to find that on QScrollArea.

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

    Default Re: Adding items to QlistWidget or QtableWidget

    i'm having trouble centering my widget in the cell of a table. I have a label with a pixmap, which I added to a cell of a QtableWidget. Are there any methods that allow me to align the contents of cell Left/Center/Right?
    I did try padding using stylesheets, but its not really want I want. I would like equal distance, say 5px on each side of the cell.


    thanks

Similar Threads

  1. Replies: 1
    Last Post: 20th June 2013, 06:24
  2. QListWidget, help adding new items.
    By JeremyRussell in forum Newbie
    Replies: 4
    Last Post: 6th April 2011, 23:54
  3. Replies: 1
    Last Post: 22nd February 2010, 10:53
  4. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 10:03
  5. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 20:04

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.