Generate Coordinates for PySide QGridView
For some weeks now, I’ve been working on what I think I think is a solution to piracy in Nigeria. I also think it will power the disruption of movie distribution, while putting more money in the hands of artists, producers and indeed everyone involved in the movie industry. Like every entrepreneurs “I think”, this is still all speculative but if you think you’re interested in what I’m working on, you can drop me a message via any of the half a dozen channels.
Anyway, this blog post is not about the project per se, but about a piece of code that has had me strung up for 5 days. Maybe it’s that “slow” feeling in my head.
It’s my first time working with Qt (PySide to be precise) and I had to programatically display a grid (using QGridView) of images, and in order not to overwhelm the UI, I had to load it a set number of times. After spending a few days trying to hack my way through, I resolved to write a function to generate the coordinates (row,col) – lists of lists really, which I could then use in my
[python]addWidget(mPW,row,col)[/python]
code. That took a whole day, and although I have something that works, I’m hoping that you can suggest a better way to achieve the intended result (I’m guessing numpy might have something but I’m wary of additional binaries)…I’m not just comfortable that this is the best approach.
So, here’s the code to generate the coordinates for QGridView
[python]def returnGridList(self, columns, count, start_cood=[0,0]):
”’
returns a list of (row,col) coordinates required to build a QGridView
”’
grid = []
if columns <= start_cood[1]:
raise IndexError(‘The number of columns should be greater than “y”‘)
#if count < columns:
# raise IndexError(‘The number of columns has to be less than the count items’)
s_cood = start_cood[1]
counter = count-1
if columns < count:
rows = count/columns
for row in range(start_cood[0], ceil(start_cood[0]+rows)):
for col in range(s_cood,columns):
grid.append([row,col])
print(counter)
if counter == 0:
break
counter-=1
s_cood = 0
if columns > count:
rows = columns/count
for row in range(start_cood[0], floor(start_cood[0]+rows)):
for col in range(s_cood,columns):
grid.append([row,col])
print(counter)
if counter == 0:
break
counter-=1
s_cood = 0
if columns == count:
rows = int((columns/count)+1)
for row in range(start_cood[0], start_cood[0]+rows):
for col in range(s_cood,columns):
grid.append([row,col])
print(counter)
if counter == 0:
break
counter-=1
s_cood = 0
print(grid)
#return grid[/python]
Look forward to your comments.
Don’t forget to share if you found this helpful, someone else might too.
You can scratch all that…I really need to take the doct’s advice and chill. After waking from a 3 hour nap yesterday afternoon, this (the right solution) came to me. Yes, this is the right solution
[python]
def returnGridList(self, count, columns=5, start_cood=[0,0]):
”’
returns a list of (row,col) coordinates required to build a QGridView
”’
grid = []
if columns <= start_cood[1] or count == 1:
raise IndexError(‘The number of columns should be greater than “y” or count cannot be 1’)
#if count < columns:
# raise IndexError(‘The number of columns has to be less than the count items’)
s_cood = start_cood[1]
counter = count
rows = (count+s_cood)/columns
for row in range(start_cood[0], ceil(start_cood[0]+rows)):
for col in range(s_cood,columns):
grid.append([row,col])
if counter == 0:
break
counter-=1
s_cood = 0
next_cood = grid.pop()
#print(grid, next_cood)
#print(grid,next_cood)
return({‘grid’:grid,’next_cood’:next_cood})
[/python]