[PyQt] my program is ignoring setgeometry and the form

Michael Staggs tausciam at gmail.com
Tue Aug 20 00:47:01 BST 2013


I have my program: 
     1. from PyQt4.QtCore[1] import *
     2. from PyQt4.QtGui[2] import *
     3. from window import Ui_MainWindow
     4.  
     5. THUMBNAIL_SIZE = 128
     6. SPACING        = 10
     7. IMAGES_PER_ROW = 5
     8.      
     9. class TableWidget(QTableWidget[3]):
     10.     def __init__(self, parent=None, **kwargs):
     11.         _QTableWidget_.__init__(self, parent, **kwargs)
     12.      
     13.         self.setIconSize(QSize[4](128,128))
     14.         self.setColumnCount(IMAGES_PER_ROW)
     15.         self.setGridStyle(Qt[5].NoPen)
     16.      
     17.         # Set the default column width and hide the header
     18.         self.verticalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING)
     19.         self.verticalHeader().hide()
     20.      
     21.         # Set the default row height and hide the header
     22.         self.horizontalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING)
     23.         self.horizontalHeader().hide()
     24.      
     25.         # Set the table width to show all images without horizontal scrolling
     26.         
self.setMinimumWidth((THUMBNAIL_SIZE+SPACING)*IMAGES_PER_ROW+(SPACING*2))
     27.      
     28.     def addPicture(self, row, col, picturePath):
     29.         item=QTableWidgetItem[6]()
     30.      
     31.         # Scale the image by either height or width and then 'crop' it to the
     32.         # desired size, this prevents distortion of the image.
     33.         p=QPixmap[7](picturePath)
     34.         if p.height()>p.width(): p=p.scaledToWidth(THUMBNAIL_SIZE)
     35.         else: p=p.scaledToHeight(THUMBNAIL_SIZE)
     36.         p=p.copy(0,0,THUMBNAIL_SIZE,THUMBNAIL_SIZE)
     37.         item.setIcon(QIcon[8](p))
     38.      
     39.         self.setItem(row,col,item)
     40.      
     41. class MainWindow(QMainWindow[9], Ui_MainWindow):
     42.     def __init__(self, parent=None, **kwargs):
     43.         super(MainWindow, self).__init__(parent)
     44.         self.setupUi(self)
     45.      
     46.         centralWidget=QWidget[10](self)
     47.         l=QVBoxLayout[11](centralWidget)
     48.      
     49.         self.tableWidget=TableWidget(self)
     50.         l.addWidget(self.tableWidget)
     51.      
     52.         self.setCentralWidget(centralWidget)
     53.      
     54.         
picturesPath=QDesktopServices[12].storageLocation(_QDesktopServices_.PicturesLocation
)
     55.         pictureDir=QDir[13](picturesPath)
     56.         pictures=pictureDir.entryList(['*.jpg','*.png','*.gif'])
     57.      
     58.         rowCount=len(pictures)//IMAGES_PER_ROW
     59.         if len(pictures)%IMAGES_PER_ROW: rowCount+=1
     60.         self.tableWidget.setRowCount(rowCount)
     61.      
     62.         row=-1
     63.         for i,picture in enumerate(pictures):
     64.             col=i%IMAGES_PER_ROW
     65.             if not col: row+=1
     66.             self.tableWidget.addPicture(row, col, pictureDir.absoluteFilePath(picture))      
     67.      
     68. if __name__=="__main__":
     69.     from sys import argv, exit
     70.      
     71.     a=QApplication[14](argv)
     72.     m=MainWindow()
     73.     m.show()
     74.     m.raise_()
     75.     exit(a.exec_())
and I have my gui file I got by designing the form using QT Designer then running pyuic4: 
     1. # -*- coding: utf-8 -*-
     2.  
     3. # Form implementation generated from reading ui file 'window.ui'
     4. #
     5. # Created by: PyQt4 UI code generator 4.9.6
     6. #
     7. # WARNING! All changes made in this file will be lost!
     8.  
     9. from PyQt4 import QtCore[1], QtGui[2]
     10.  
     11. try:
     12.     _fromUtf8 = QtCore[1].QString[15].fromUtf8
     13. except AttributeError:
     14.     def _fromUtf8(s):
     15.         return s
     16.  
     17. try:
     18.     _encoding = QtGui[2].QApplication[14].UnicodeUTF8
     19.     def _translate(context, text, disambig):
     20.         return QtGui[2].QApplication[14].translate(context, text, disambig, _encoding)
     21. except AttributeError:
     22.     def _translate(context, text, disambig):
     23.         return QtGui[2].QApplication[14].translate(context, text, disambig)
     24.  
     25. class Ui_MainWindow(object):
     26.     def setupUi(self, MainWindow):
     27.         MainWindow.setObjectName(_fromUtf8("MainWindow"))
     28.         MainWindow.resize(800, 600)
     29.         self.centralwidget = QtGui[2].QWidget[10](MainWindow)
     30.         self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
     31.         self.tableWidget = QtGui[2].QTableWidget[3](self.centralwidget)
     32.         self.tableWidget.setGeometry(QtCore[1].QRect[16](70, 20, 661, 381))
     33.         self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
     34.         self.tableWidget.setColumnCount(0)
     35.         self.tableWidget.setRowCount(0)
     36.         self.listWidget = QtGui[2].QListWidget[17](self.centralwidget)
     37.         self.listWidget.setGeometry(QtCore[1].QRect[16](70, 400, 661, 181))
     38.         self.listWidget.setObjectName(_fromUtf8("listWidget"))
     39.         MainWindow.setCentralWidget(self.centralwidget)
     40.  
     41.         self.retranslateUi(MainWindow)
     42.         QtCore[1].QMetaObject[18].connectSlotsByName(MainWindow)
     43.  
     44.     def retranslateUi(self, MainWindow):
     45.         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
According to the websites I’ve read, I’ve done what needs to be done to link the two. I 
added 
     1. from window import Ui_MainWindow
     2. class MainWindow(QMainWindow[9], Ui_MainWindow):
     3.     def __init__(self, parent=None, **kwargs):
     4.         super(MainWindow, self).__init__(parent)
     5.         self.setupUi(self)
I thought the tablewidget from my program would be mapped onto my window.py form I 
created. However, it does no such thing. It totally ignores the form except for setting the 
title and geometry of the main window. Then, it acts like I don’t even have a window.py 
What am I doing wrong? Right now, I’m using my pictures directory for a working example. 
Eventually, I want to display album covers in the top window, click on the album cover, and 
display the songs in the bottom window. I thought about using a dict where the album is 
the key and the list of songs is the value in order to do that. However, it’s totally ignoring 
my form.
Here is a picture of the form as it should be: 
http://i.imgur.com/Wrp1zHW.png[19] 
The program should, at this point, be displaying all my pictures in that top window. 
However, this is what happens when I run it: 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130819/388f7002/attachment-0001.html>


More information about the PyQt mailing list