PDA

View Full Version : Adding items to QlistWidget or QtableWidget



Nfrancisj
17th April 2016, 23:02
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!

anda_skoa
18th April 2016, 09:17
As a first try I would go for a QGridLayout on a widget in a QScrollArea.

Cheers,
_

Nfrancisj
9th May 2016, 02:57
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)


class ViewerLabel(QLabel):
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():

layout = QGridLayout()
scrollArea = QScrollArea()

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

anda_skoa
9th May 2016, 12:49
"layout" is not a QWidget, QScrollArea::setWidget expects one.

Cheers,
_

Nfrancisj
9th May 2016, 17:01
Right! I should have know that! Haha. So I need to use .setLayout instead of .setWidget.

Thanks Again!

Nfrancisj
10th May 2016, 02:52
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.

anda_skoa
10th May 2016, 06:58
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,
_

Nfrancisj
10th May 2016, 07:15
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..

11934

Can I not use QGridLayout here instead of QVBox? Everytime I complie, the designer crashes. With other layout types it works fine.

anda_skoa
10th May 2016, 07:24
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,
_

Nfrancisj
10th May 2016, 07:34
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?

Nfrancisj
13th May 2016, 05:23
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

anda_skoa
13th May 2016, 09:22
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.


By the way Anda, does QLabel not have a ".setDragEnabled"?

Yes, that's right.
Usually labels aren't sources for drags.



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

Nfrancisj
13th May 2016, 20:04
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!

anda_skoa
14th May 2016, 11:06
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,
_

Nfrancisj
14th May 2016, 18:07
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/66043-Drag-and-Drop-Events-on-Pixmap-Thumbnails-attached-to-Qlabel?p=290646#post290646


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



from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys


class Label_Template(QLabel):
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;")


class ScrollAreaExample(QDialog):
def __init__(self):
super(ScrollAreaExample, self).__init__()

self.scrollArea = QScrollArea() # Create Scroll Area
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.Scr ollBarAlwaysOn)
#self.scrollArea.setHorizontalScrollBarPolicy(Qt.S crollBarAlwaysOn)


#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(s tr(self.rows+self.columns)), self.rows, self.columns)



# Add Labels layout to Scroll Area Container
self.scrollArea_Container.setLayout(self.gridLayou t_4labels)

# Add Scroll Area Container to Scroll Area
self.scrollArea.setWidget(self.scrollArea_Containe r)

# Add Scroll Area to a Grid Layout
self.gridLayout_4ScrollArea.addWidget(self.scrollA rea)

# Add Grid Layout to Window (this class)
self.setLayout(self.gridLayout_4ScrollArea)



if __name__ == "__main__":
app = QApplication(sys.argv)
window = ScrollAreaExample()
window.show()
sys.exit(app.exec_())

Nfrancisj
29th June 2016, 23:33
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.

Nfrancisj
19th October 2016, 19:31
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