Re: Using PyQT with QT Designer
Lee Harr, thank you. I took your suggestion after I finished coding the audio section. You can see the improved project here: http://i.imgur.com/permuRQ.jpg On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png That's not a preview. That's just the regular design view. (you can tell by the little dots in the background) You need to go to Form - Preview... to see the actual preview. That said... 1.) You may want to ask your question on the PyQt mailing list. Though you are talking with the undisputed PyQt expert in Phil, there are more people on the other list who are familiar with PyQt and who may be willing to look more closely at your specific code. 2.) It may be that the examples you are looking at are not sufficient to help you with the situation you are in. For instance, I've written several programs using Designer and PyQt and I would recommend against using the pyuic method. When I first started with PyQt I also used pyuic and eventually I found the PyQt4.uic method works better for me. 3.) Layouts. You have to use them with Qt or you're going to have a bad time. Looking at your design, I would do something like ... - select the two buttons on the left and click Lay Out Vertically - select the two large white boxes and click Lay Out Vertically - put a vertical spacer underneath the red X button - select the red button and the spacer and click Lay Out Vertically - at this point you may need to resize and rearrange your three vertical layouts so that they don't overlap and are in approximately the positions that you want, then - select the main window and click Lay Out Horizontally Something along those lines would get you about to where you want to be. The form may not look _exactly_ the way you have it there, but it will be a more flexible design and nothing will be overlapping. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Thanks. I probably will do exactly like you suggested later on. But, those two lines have solved the problem I had and I can work on the actual program now. I can come back to the GUI later. Here is what it looks like now: http://i.imgur.com/sLiSU6M.png On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png That's not a preview. That's just the regular design view. (you can tell by the little dots in the background) You need to go to Form - Preview... to see the actual preview. That said... 1.) You may want to ask your question on the PyQt mailing list. Though you are talking with the undisputed PyQt expert in Phil, there are more people on the other list who are familiar with PyQt and who may be willing to look more closely at your specific code. 2.) It may be that the examples you are looking at are not sufficient to help you with the situation you are in. For instance, I've written several programs using Designer and PyQt and I would recommend against using the pyuic method. When I first started with PyQt I also used pyuic and eventually I found the PyQt4.uic method works better for me. 3.) Layouts. You have to use them with Qt or you're going to have a bad time. Looking at your design, I would do something like ... - select the two buttons on the left and click Lay Out Vertically - select the two large white boxes and click Lay Out Vertically - put a vertical spacer underneath the red X button - select the red button and the spacer and click Lay Out Vertically - at this point you may need to resize and rearrange your three vertical layouts so that they don't overlap and are in approximately the positions that you want, then - select the main window and click Lay Out Horizontally Something along those lines would get you about to where you want to be. The form may not look _exactly_ the way you have it there, but it will be a more flexible design and nothing will be overlapping. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
On Thu, 22 Aug 2013 18:08:14 -0700 (PDT), tausc...@gmail.com wrote: 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 ...only because you have told it to... 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... You need to read up on how to use layouts in Designer. The generated .py file will then do what you want automatically. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
I tried that this morning and it destroyed my form. So, right now, that's probably not what I'm looking for. But, if you look at that picture, the app isn't resized to 800x600 like it says in the ui file. The pixmaps aren't on the buttons like I set them up in the ui file. It's not using the ui file at all. So, what's the point of making a QT Designer file at all if it doesn't use it? I'm guessing it CAN use it and there is just something I'm missing. You may be right and I may not want to set the geometry in qt designer down the road. But, right now I do and not only is it not getting that from the ui fileit's not getting anything at all...even though I added all the lines I thought I needed to. If I decide to actually change the gui later, I'd like to be able to use QT Designer to do so...design a layout and not really have to change my program. As it stands, it's totally ignoring my ui file and I have to redo all the work in the program. In which case, there's no point to using qt designer at all. I know I have to be missing something though there has to be a way to use the work qt designer did. Thanks On Aug 23, 2013 2:39 AM, Phil Thompson p...@riverbankcomputing.com wrote: On Thu, 22 Aug 2013 18:08:14 -0700 (PDT), tausc...@gmail.com wrote: 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 ...only because you have told it to... 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... You need to read up on how to use layouts in Designer. The generated .py file will then do what you want automatically. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
On Fri, 23 Aug 2013 08:00:29 -0500, Michael Staggs tausc...@gmail.com wrote: I tried that this morning and it destroyed my form. So, right now, that's probably not what I'm looking for. But, if you look at that picture, the app isn't resized to 800x600 like it says in the ui file. The pixmaps aren't on the buttons like I set them up in the ui file. It's not using the ui file at all. So, what's the point of making a QT Designer file at all if it doesn't use it? pyuic4 uses it to generate the corresponding Python code. Any time you change the .ui file with Designer you have to run pyuic4 again. You should not modify the Python code that pyuic4 generates. I'm guessing it CAN use it and there is just something I'm missing. You may be right and I may not want to set the geometry in qt designer down the road. But, right now I do and not only is it not getting that from the ui fileit's not getting anything at all...even though I added all the lines I thought I needed to. If I decide to actually change the gui later, I'd like to be able to use QT Designer to do so...design a layout and not really have to change my program. As it stands, it's totally ignoring my ui file and I have to redo all the work in the program. In which case, there's no point to using qt designer at all. I know I have to be missing something though there has to be a way to use the work qt designer did. I strongly suggest you do some more reading about using Designer. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Right. I know that if I redesign it I have to run pyuic4 again and that I shouldn't change that file...let qt designer do its job. But, that's exactly what I'm having the problem with...incorporating the file pyuic4 gave me... and why I posted here. If you can point me towards something I need to read then by all means... I'd be grateful. But, all the little tutorials I've found told me to do it this way and obviously my program has no access to itit's not resizing the window or doing anything the ui file states. So, I do know I'm doing something wrong and doing something the little tutorials didn't account for. So, I'm asking here. Again, I'm just learning. I took the codecademy python course and started trying to learn to build a media player...and haven't figured out how to build the gui yet. If you or anyone else can point me to something that would explain what I'm doing wrong, I'll read it from front to back. If it's too advanced it will lose mebut I would like to learn to do this. On Aug 23, 2013 12:17 PM, Phil Thompson p...@riverbankcomputing.com wrote: On Fri, 23 Aug 2013 08:00:29 -0500, Michael Staggs tausc...@gmail.com wrote: I tried that this morning and it destroyed my form. So, right now, that's probably not what I'm looking for. But, if you look at that picture, the app isn't resized to 800x600 like it says in the ui file. The pixmaps aren't on the buttons like I set them up in the ui file. It's not using the ui file at all. So, what's the point of making a QT Designer file at all if it doesn't use it? pyuic4 uses it to generate the corresponding Python code. Any time you change the .ui file with Designer you have to run pyuic4 again. You should not modify the Python code that pyuic4 generates. I'm guessing it CAN use it and there is just something I'm missing. You may be right and I may not want to set the geometry in qt designer down the road. But, right now I do and not only is it not getting that from the ui fileit's not getting anything at all...even though I added all the lines I thought I needed to. If I decide to actually change the gui later, I'd like to be able to use QT Designer to do so...design a layout and not really have to change my program. As it stands, it's totally ignoring my ui file and I have to redo all the work in the program. In which case, there's no point to using qt designer at all. I know I have to be missing something though there has to be a way to use the work qt designer did. I strongly suggest you do some more reading about using Designer. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
On Fri, 23 Aug 2013 12:30:41 -0500, Michael Staggs tausc...@gmail.com wrote: Right. I know that if I redesign it I have to run pyuic4 again and that I shouldn't change that file...let qt designer do its job. But, that's exactly what I'm having the problem with...incorporating the file pyuic4 gave me... and why I posted here. If you can point me towards something I need to read then by all means... I'd be grateful. But, all the little tutorials I've found told me to do it this way and obviously my program has no access to itit's not resizing the window or doing anything the ui file states. So, I do know I'm doing something wrong and doing something the little tutorials didn't account for. So, I'm asking here. Again, I'm just learning. I took the codecademy python course and started trying to learn to build a media player...and haven't figured out how to build the gui yet. If you or anyone else can point me to something that would explain what I'm doing wrong, I'll read it from front to back. If it's too advanced it will lose mebut I would like to learn to do this. http://qt-project.org/doc/qt-4.8/designer-manual.html Designer has a preview option that creates your UI on the fly. The first step would be to get it working as far as you can with that before you try generating any Python code. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Thanks for the manual. I will look into it but all the examples are probably c++. Ive tried zetcode and some of the other tutorials. That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png The problem isn't that I can't design it in QT Designer. It is designed just like I want it. The problem is, when I try to follow zetcode and other tutorials about how to import and use my form as designed by qt designer and run through pyuic4 it doesn't seem to even notice my ui file...and certainly isnt acting on it. I posted my code above where I was trying anything just to get it to use that ui I designed in qt designer.so far to no avail -- Forwarded message -- From: Michael Staggs tausc...@gmail.com Date: Aug 23, 2013 12:54 PM Subject: Re: Using PyQT with QT Designer To: Phil Thompson p...@riverbankcomputing.com Cc: Thanks for the manual. I will look into it but all the examples are probably c++. Ive tried zetcode and some of the other tutorials. That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png The problem isn't that I can't design it in QT Designer. It is designed just like I want it. The problem is, when I try to follow zetcode and other tutorials about how to import and use my form as designed by qt designer and run through pyuic4 it doesn't seem to even notice my ui file...and certainly isnt acting on it. I posted my code above where I was trying anything just to get it to use that ui I designed in qt designer.so far to no avail On Aug 23, 2013 12:42 PM, Phil Thompson p...@riverbankcomputing.com wrote: On Fri, 23 Aug 2013 12:30:41 -0500, Michael Staggs tausc...@gmail.com wrote: Right. I know that if I redesign it I have to run pyuic4 again and that I shouldn't change that file...let qt designer do its job. But, that's exactly what I'm having the problem with...incorporating the file pyuic4 gave me... and why I posted here. If you can point me towards something I need to read then by all means... I'd be grateful. But, all the little tutorials I've found told me to do it this way and obviously my program has no access to itit's not resizing the window or doing anything the ui file states. So, I do know I'm doing something wrong and doing something the little tutorials didn't account for. So, I'm asking here. Again, I'm just learning. I took the codecademy python course and started trying to learn to build a media player...and haven't figured out how to build the gui yet. If you or anyone else can point me to something that would explain what I'm doing wrong, I'll read it from front to back. If it's too advanced it will lose mebut I would like to learn to do this. http://qt-project.org/doc/qt-4.8/designer-manual.html Designer has a preview option that creates your UI on the fly. The first step would be to get it working as far as you can with that before you try generating any Python code. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Again thoughI'm finished with QT Designer. I have the finished product I want exactly like I want it. But, as ive shown in the screenshots, I'm doing exactly what ive seen in zetcode and other tutorials but It doesn't seem to incorporate and act upon that ui file. The first thing you notice is that it doesn't resize the main window to 800x600...which is one of the first lines in the ui file. I know whatever I'm doing wrong has to be a 1 or 2 line solution...just something I should change a littlebut I dont know what that is On Aug 23, 2013 12:42 PM, Phil Thompson p...@riverbankcomputing.com wrote: On Fri, 23 Aug 2013 12:30:41 -0500, Michael Staggs tausc...@gmail.com wrote: Right. I know that if I redesign it I have to run pyuic4 again and that I shouldn't change that file...let qt designer do its job. But, that's exactly what I'm having the problem with...incorporating the file pyuic4 gave me... and why I posted here. If you can point me towards something I need to read then by all means... I'd be grateful. But, all the little tutorials I've found told me to do it this way and obviously my program has no access to itit's not resizing the window or doing anything the ui file states. So, I do know I'm doing something wrong and doing something the little tutorials didn't account for. So, I'm asking here. Again, I'm just learning. I took the codecademy python course and started trying to learn to build a media player...and haven't figured out how to build the gui yet. If you or anyone else can point me to something that would explain what I'm doing wrong, I'll read it from front to back. If it's too advanced it will lose mebut I would like to learn to do this. http://qt-project.org/doc/qt-4.8/designer-manual.html Designer has a preview option that creates your UI on the fly. The first step would be to get it working as far as you can with that before you try generating any Python code. Phil -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Michael Staggs wrote: That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png The problem isn't that I can't design it in QT Designer. It is designed just like I want it. The problem is, when I try to follow zetcode and other tutorials about how to import and use my form as designed by qt designer and run through pyuic4 it doesn't seem to even notice my ui file...and certainly isnt acting on it. I don't know PyQT, so I've kept quiet so far... You don't say what the name of the generated file is, but perhaps since the source file was window.ui, the generated one is window.py My guess is that when you do the from window import Ui_MainWindow it is finding some OTHER window.py file. Have you tried simply adding an illegal line to the generated file, to force the compiler to fail the import? Once you're sure that it is importing this particular file, you can remove such a line. Could be that you have some other window.py file (or window.pyc, or whatever) and that it's finding that one. Or it's finding some older version of this one. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Thank you. I just deleted all of them, reran pyuic4 on window.ui and regenerated window.py just to make sure. Unfortunately, I get the same problem. I've got the GUI perfectly designed just like I want it in window.py... just can't figure out how to use it in my program. On Friday, August 23, 2013 3:16:59 PM UTC-5, Dave Angel wrote: Michael Staggs wrote: That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png The problem isn't that I can't design it in QT Designer. It is designed just like I want it. The problem is, when I try to follow zetcode and other tutorials about how to import and use my form as designed by qt designer and run through pyuic4 it doesn't seem to even notice my ui file...and certainly isnt acting on it. I don't know PyQT, so I've kept quiet so far... You don't say what the name of the generated file is, but perhaps since the source file was window.ui, the generated one is window.py My guess is that when you do the from window import Ui_MainWindow it is finding some OTHER window.py file. Have you tried simply adding an illegal line to the generated file, to force the compiler to fail the import? Once you're sure that it is importing this particular file, you can remove such a line. Could be that you have some other window.py file (or window.pyc, or whatever) and that it's finding that one. Or it's finding some older version of this one. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png That's not a preview. That's just the regular design view. (you can tell by the little dots in the background) You need to go to Form - Preview... to see the actual preview. That said... 1.) You may want to ask your question on the PyQt mailing list. Though you are talking with the undisputed PyQt expert in Phil, there are more people on the other list who are familiar with PyQt and who may be willing to look more closely at your specific code. 2.) It may be that the examples you are looking at are not sufficient to help you with the situation you are in. For instance, I've written several programs using Designer and PyQt and I would recommend against using the pyuic method. When I first started with PyQt I also used pyuic and eventually I found the PyQt4.uic method works better for me. 3.) Layouts. You have to use them with Qt or you're going to have a bad time. Looking at your design, I would do something like ... - select the two buttons on the left and click Lay Out Vertically - select the two large white boxes and click Lay Out Vertically - put a vertical spacer underneath the red X button - select the red button and the spacer and click Lay Out Vertically - at this point you may need to resize and rearrange your three vertical layouts so that they don't overlap and are in approximately the positions that you want, then - select the main window and click Lay Out Horizontally Something along those lines would get you about to where you want to be. The form may not look _exactly_ the way you have it there, but it will be a more flexible design and nothing will be overlapping. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Thank you... I found my problem class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) That seems to take care of it... if I comment out everything else, I get my pristine form I don't know if it's the super call or the setupuibut one of those was my godsend. So, that is solved... I just have to figure out how to put things in the gui where I want thembut I think you're right at any rate. If I try to resize, it doesn't function like I thought it would. I will have to use layouts.. but at least now I am able to use the UI file that I created. On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png That's not a preview. That's just the regular design view. (you can tell by the little dots in the background) You need to go to Form - Preview... to see the actual preview. That said... 1.) You may want to ask your question on the PyQt mailing list. Though you are talking with the undisputed PyQt expert in Phil, there are more people on the other list who are familiar with PyQt and who may be willing to look more closely at your specific code. 2.) It may be that the examples you are looking at are not sufficient to help you with the situation you are in. For instance, I've written several programs using Designer and PyQt and I would recommend against using the pyuic method. When I first started with PyQt I also used pyuic and eventually I found the PyQt4.uic method works better for me. 3.) Layouts. You have to use them with Qt or you're going to have a bad time. Looking at your design, I would do something like ... - select the two buttons on the left and click Lay Out Vertically - select the two large white boxes and click Lay Out Vertically - put a vertical spacer underneath the red X button - select the red button and the spacer and click Lay Out Vertically - at this point you may need to resize and rearrange your three vertical layouts so that they don't overlap and are in approximately the positions that you want, then - select the main window and click Lay Out Horizontally Something along those lines would get you about to where you want to be. The form may not look _exactly_ the way you have it there, but it will be a more flexible design and nothing will be overlapping. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
On Wed, 21 Aug 2013 21:04:47 -0500, Michael Staggs tausc...@gmail.com wrote: I'm learning Python and I have a problem. I've asked the question everywhere and no one helps me, so I'm hoping someone here will. I am making a program that shows album covers and you click on the album cover in the top window. In the bottom window, the list of songs appear and you can click the individual song to play it. It's going to be a media player for children. I'm thinking I'll be able to use a dict and have the album as the key and the list of songs as the value to accomplish this. Right now, I'm just using my picture directory to try and get the basic layout right. I designed a form in QT Designer: http://i.imgur.com/Wrp1zHW.png Here is my gui file I got from running pyuic4 on the ui file: # -*- 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) 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)) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate(MainWindow, MainWindow, None)) Now, according to websites I read, I should just have to add the following to my program to get it to use the form: from window import Ui_MainWindow class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent) self.setupUi(self) and here is my program: from PyQt4.QtCore import * from PyQt4.QtGui import * from window import Ui_MainWindow THUMBNAIL_SIZE = 128 SPACING= 10 IMAGES_PER_ROW = 5 class TableWidget(QTableWidget): def __init__(self, parent=None, **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) class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent) self.setupUi(self) centralWidget=QWidget(self) l=QVBoxLayout(centralWidget) self.tableWidget=TableWidget(self) l.addWidget(self.tableWidget) self.setCentralWidget(centralWidget) picturesPath=QDesktopServices.storageLocation(QDesktopServices.PicturesLocation)
Re: Using PyQT with QT Designer
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))
Using PyQT with QT Designer
I'm learning Python and I have a problem. I've asked the question everywhere and no one helps me, so I'm hoping someone here will. I am making a program that shows album covers and you click on the album cover in the top window. In the bottom window, the list of songs appear and you can click the individual song to play it. It's going to be a media player for children. I'm thinking I'll be able to use a dict and have the album as the key and the list of songs as the value to accomplish this. Right now, I'm just using my picture directory to try and get the basic layout right. I designed a form in QT Designer: http://i.imgur.com/Wrp1zHW.png Here is my gui file I got from running pyuic4 on the ui file: # -*- 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) 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)) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate(MainWindow, MainWindow, None)) Now, according to websites I read, I should just have to add the following to my program to get it to use the form: from window import Ui_MainWindow class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent) self.setupUi(self) and here is my program: from PyQt4.QtCore import * from PyQt4.QtGui import * from window import Ui_MainWindow THUMBNAIL_SIZE = 128 SPACING= 10 IMAGES_PER_ROW = 5 class TableWidget(QTableWidget): def __init__(self, parent=None, **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) class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent) self.setupUi(self) centralWidget=QWidget(self) l=QVBoxLayout(centralWidget) self.tableWidget=TableWidget(self) l.addWidget(self.tableWidget) 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