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!
Re: Adding items to QlistWidget or QtableWidget
As a first try I would go for a QGridLayout on a widget in a QScrollArea.
Cheers,
_
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)
Code:
def __init__(self, parent=None):
super(ViewerLabel, self).__init__(parent)
self.setMouseTracking(True)
self.setAlignment(Qt.AlignCenter)
def enterEvent(self,event):
button.setStyleSheet("background-color:#45b545;")
def leaveEvent(self,event):
button.setStyleSheet("background-color:yellow;")
def main():
label_01 = ViewerLabel(“label 01â€)
label_02 = ViewerLabel(“label 02â€)
label_03 = ViewerLabel(“label 03â€)
label_04 = ViewerLabel(“label 04â€)
label_05 = ViewerLabel(“label 05â€)
label_06 = ViewerLabel(“label 06â€)
layout.addwidget(label_01, 0, 1)
layout.addwidget(label_02, 0, 2)
layout.addwidget(label_03, 1, 1)
layout.addwidget(label_04, 1, 2)
layout.addwidget(label_05, 2, 1)
layout.addwidget(label_06, 2, 2)
layout.setSpacing(10)
scrollArea.setWidget(layout)
window.setLayout(scrollArea)
window.setFixedSize(400,500)
window.show()
return app.exec_()
if __name__ == __main__:
main()
Re: Adding items to QlistWidget or QtableWidget
"layout" is not a QWidget, QScrollArea::setWidget expects one.
Cheers,
_
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!
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. :D
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.
Re: Adding items to QlistWidget or QtableWidget
Quote:
Originally Posted by
Nfrancisj
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,
_
1 Attachment(s)
Re: Adding items to QlistWidget or QtableWidget
Quote:
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..
Attachment 11934
Can I not use QGridLayout here instead of QVBox? Everytime I complie, the designer crashes. With other layout types it works fine.
Re: Adding items to QlistWidget or QtableWidget
Quote:
Originally Posted by
Nfrancisj
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,
_
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?
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
Re: Adding items to QlistWidget or QtableWidget
Quote:
Originally Posted by
Nfrancisj
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
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
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,
_
Re: Adding items to QlistWidget or QtableWidget
Quote:
The scrollarea needs a content widget to work on.
this is exactly was i was missing! Now I understand. Thankyou!
Quote:
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!
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,
_
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.
Code:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
def __init__(self,setText):
super(Label_Template, self).__init__()
self.
setFrameShape(QFrame.
StyledPanel) self.setAlignment(Qt.AlignCenter)
self.setMinimumHeight(100)
self.setMinimumWidth(100)
self.setStyleSheet("background-color:yellow;")
self.setText(setText)
def enterEvent(self,event):
self.setStyleSheet("background-color:#45b545;")
def leaveEvent(self,event):
self.setStyleSheet("background-color:yellow;")
def __init__(self):
super(ScrollAreaExample, self).__init__()
self.
gridLayout_4ScrollArea = QGridLayout() # Create Grid Layout for Scroll self.
gridLayout_4labels = QGridLayout() # Create Grid Layout for Labels self.
scrollArea_Container = QWidget() #Create a Container for Widets that will go into the Scroll Area
#self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
#self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
#Create Stuff
self.numRows = 5
self.numColumns = 3
for self.columns in range(0, self.numColumns):
for self.rows in range(0,self.numRows):
self.gridLayout_4labels.addWidget(Label_Template(str(self.rows+self.columns)), self.rows, self.columns)
# Add Labels layout to Scroll Area Container
self.scrollArea_Container.setLayout(self.gridLayout_4labels)
# Add Scroll Area Container to Scroll Area
self.scrollArea.setWidget(self.scrollArea_Container)
# Add Scroll Area to a Grid Layout
self.gridLayout_4ScrollArea.addWidget(self.scrollArea)
# Add Grid Layout to Window (this class)
self.setLayout(self.gridLayout_4ScrollArea)
if __name__ == "__main__":
window = ScrollAreaExample()
window.show()
sys.exit(app.exec_())
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.
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