Using PyQT with QT Designer
tausciam at gmail.com
tausciam at gmail.com
Thu Aug 22 21:08:14 EDT 2013
On Thursday, August 22, 2013 3:26:17 AM UTC-5, Phil Thompson wrote:
> It looks like you aren't using a layout to arrange your widgets.
>
> Explicitly specifying geometries is a bad idea.
>
>
>
> Phil
Thanks.QT Designer uses set geometry and I'm totally lost as how to implement it. I've tried using a layout on the central widget. I've tried specifically referencing the Ui_MainWindow in the window.py ui file...
This is what I tried:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from window import Ui_MainWindow
THUMBNAIL_SIZE = 128
SPACING = 10
IMAGES_PER_ROW = 4
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# self.setWindowTitle("Image Gallery")
centralWidget=QWidget(self)
l=QVBoxLayout(centralWidget)
self.tableWidget=TableWidget(self)
l.addWidget(self.tableWidget)
self.listWidget=ListWidget(self)
l.addWidget(self.listWidget)
Ui_MainWindow.pushButton = QPushButton(self)
l.addWidget(Ui_MainWindow.pushButton)
self.pushButton_2 = QPushButton(self)
l.addWidget(self.pushButton_2)
self.pushButton_3 = QPushButton(self)
l.addWidget(self.pushButton_3)
self.setCentralWidget(centralWidget)
picturesPath=QDesktopServices.storageLocation(QDesktopServices.PicturesLocation)
pictureDir=QDir(picturesPath)
pictures=pictureDir.entryList(['*.jpg','*.png','*.gif'])
rowCount=len(pictures)//IMAGES_PER_ROW
if len(pictures)%IMAGES_PER_ROW: rowCount+=1
self.tableWidget.setRowCount(rowCount)
row=-1
for i,picture in enumerate(pictures):
col=i%IMAGES_PER_ROW
if not col: row+=1
self.tableWidget.addPicture(row, col, pictureDir.absoluteFilePath(picture))
class ListWidget(QListWidget):
def __init__(self, parent=MainWindow, **kwargs):
QListWidget.__init__(self, parent, **kwargs)
self.setGeometry(QRect(70, 400, 661, 181))
class TableWidget(QTableWidget):
def __init__(self, parent=MainWindow, **kwargs):
QTableWidget.__init__(self, parent, **kwargs)
self.setIconSize(QSize(128,128))
self.setColumnCount(IMAGES_PER_ROW)
self.setGridStyle(Qt.NoPen)
# Set the default column width and hide the header
self.verticalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING)
self.verticalHeader().hide()
# Set the default row height and hide the header
self.horizontalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING)
self.horizontalHeader().hide()
# Set the table width to show all images without horizontal scrolling
self.setMinimumWidth((THUMBNAIL_SIZE+SPACING)*IMAGES_PER_ROW+(SPACING*2))
def addPicture(self, row, col, picturePath):
item=QTableWidgetItem()
# Scale the image by either height or width and then 'crop' it to the
# desired size, this prevents distortion of the image.
p=QPixmap(picturePath)
if p.height()>p.width(): p=p.scaledToWidth(THUMBNAIL_SIZE)
else: p=p.scaledToHeight(THUMBNAIL_SIZE)
p=p.copy(0,0,THUMBNAIL_SIZE,THUMBNAIL_SIZE)
item.setIcon(QIcon(p))
self.setItem(row,col,item)
if __name__=="__main__":
from sys import argv, exit
a=QApplication(argv)
m=MainWindow()
m.show()
m.raise_()
exit(a.exec_())
and I'm getting this (not even starting at 800x600): http://i.imgur.com/Xg4Qnzl.png
instead of this as it was designed in QT Designer: http://i.imgur.com/ULRolq8.png
Here is the ui file window.py that I got by running pyuic4 on window.ui:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'window.ui'
#
# Created by: PyQt4 UI code generator 4.9.6
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
MainWindow.setAnimated(False)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(70, 20, 661, 381))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(70, 400, 661, 181))
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 150, 71, 81))
self.pushButton.setText(_fromUtf8(""))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("pics/eye.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(64, 64))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(0, 230, 71, 81))
self.pushButton_2.setText(_fromUtf8(""))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("pics/ear.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_2.setIcon(icon1)
self.pushButton_2.setIconSize(QtCore.QSize(64, 64))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(730, 20, 61, 61))
self.pushButton_3.setText(_fromUtf8(""))
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(_fromUtf8("pics/exit.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_3.setIcon(icon2)
self.pushButton_3.setIconSize(QtCore.QSize(48, 48))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
More information about the Python-list
mailing list