A while back I made a project using PyQt. I created some .ui files and generated the corresponding .py files using pyuic4. I want to start work on it again, but I have lost the .ui files (I formatted my PC and took a backup, but the .ui files were residing in the Qt designer folder and got lost).
Is there any way I can restore those .ui files from the .py files generated?
It is possible to do this using QFormBuilder:
from PyQt4 import QtCore, QtGui, QtDesigner
from myui import Ui_Dialog
def dump_ui(widget, path):
builder = QtDesigner.QFormBuilder()
stream = QtCore.QFile(path)
stream.open(QtCore.QIODevice.WriteOnly)
builder.save(stream, widget)
stream.close()
app = QtGui.QApplication([''])
dialog = QtGui.QDialog()
Ui_Dialog().setupUi(dialog)
dialog.show()
dump_ui(dialog, 'myui.ui')
(NB: showing the window seems to be quite important in order to get the best results).
Don't expect to get a perfect reconstruction of your original ui file, though. You will almost certainly need to do quite a lot of tidying up to get something acceptable - but if your ui is quite complex, it should still be worth it.
Related
When I convert a .ui file in QtDesigner to a .py file, the format changes and it runs differently.
When I run it in QtDesigner it looks like a normal page but once I convert it to a .py file and run it, the edges are cut off and I cannot see half the buttons/labels. Even once I expand the screen that has opened the labels are cut off and only half visible.
Is there a way I can stop this from happening?
You firstly need to correctly set the layout and widgets inside them, in a way that the size of each object is guaranteed when moving to the code.
Try to watch this tutorial, I found it very useful!
Qt Designer - create application GUI (DESIGN APPLICATION LAYOUT) - part 02
And then you need to just import the .ui file as follows:
from PyQt5 import uic
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
uic.loadUi("NameofYourFile.ui",self)
self.show()
I'm writing a PyQt5 app that includes some text widgets. If I copy some text to the clipboard, I can paste it into other programs only while my program is running. If I close my program, its clipboard data disappears.
I see there is an open Qt bug about this which suggests calling the Windows API OleFlushClipboard, but I am looking for a cross-platform solution.
My question is basically identical to this one, but the answer there is over 10 years old and it doesn't seem to work anymore. I put that code in my program at various positions (in a closeEvent handler, in another function that I called manually just to test it, etc.) and it doesn't flush the clipboard.
My test code is basically identical to that from the old question, but updated for PyQt5:
import sys
import random
from PyQt5 import QtGui, QtWidgets, QtCore
print("Qt Version", QtCore.QT_VERSION_STR)
print("PyQt Version", QtCore.PYQT_VERSION_STR)
app = QtWidgets.QApplication(sys.argv)
edit = QtWidgets.QLineEdit()
edit.setText('try copying this random number {}'.format(random.randint(1, 1000)))
edit.show()
app.exec_()
If I run this and copy the text from the QLineEdit, then close the app, I can't paste the data anywhere; it's gone. If I run it, copy the text, paste it into another app (before closing my PyQt5 test program) and then close my test program, I get strange and inconsistent behavior where I can paste into some apps but not others. It seems I can usually continue to paste into whatever app I pasted into before closing my test program, but pasting into other apps is hit or miss. But the bottom line is that I want pasting to always work into any app regardless of when I closed my Qt program relative to the paste and Qt isn't making that work.
Is there any cross-platform way to force Qt to stop storing "pointers" to clipboard data and to immediately put the actual copied data into the actual system clipboard? I would prefer not to rely on something like closeEvent but to instead completely disable whatever fancy stuff Qt is doing and have it always immediately put the actual data directly into the system clipboard on every clipboard event without ever maintaining any kind of control or pointer handling (so even if I force-killed my Qt app and it didn't get to "clean up" and flush the clipboard on exit, the data would still be on the clipboard). I'm testing this on Windows but I want a solution that works for at least Windows, Mac and Linux.
I created a main app with a mdiArea for loading map graphics with Qt Designer *.ui and coded with pyQt4 using uic.loadUi() in python.
I also created a separate *.ui file and tested the dockWidget successfully in a separate python script file.
I wish to combine these 2 UI so that the main_app window will have the mdiArea widget on the left, while the dockWidget as the info_panel on the right.
I tried to load the *.ui file in the main app python, but ended up the dockWidget as a separate window when show().
Any advice to resolve this?
I hope I need not have to use Qt Designer to combine the mdiArea main_app UI with the dockWidget info_panel and load them as a single UI. ;P
Thanks in advance.
I've worked on some software where every different pane is done as a separate. Ui file, so that they can be changed independently without requiring merges. It worked fine. Can you turn the map and dock parts into widgets, and then make a new "main window" ui, and then give that a layout and add the other two as child widgets to it?
I made a gui using Pyqt earlier. I did all the coding on the terminal in Linux. Now I am going to make a big project in Pyqt.
Is there any sdk which will help me to overcome the coding part, so I can just drag and drop the items? I know about qt designer, but I don't know how I should write and integrate it with Python.
Do you have suggestions on what program to use for this?
Qt Designer when coupled with PyQt4 is usually used only for the layout process, as opposed to defining signals, buddies, etc. You perform the visual layout of your widgets, and save the .ui file.
Using pyuic4, you can then compile the .ui -> .py, and import that into your coded project.
Though there are probably 3 different approaches to using the UI at this point, what I typically do is multiple inheritance. If class MyMainWindow is meant to be a QMainWindow, then I inherit my class from QMainWindow, and the UI class.
Something like this...
pyuic4 myMainWindow.ui -o myMainWindowUI.py
main.py
from PyQt4 import QtGui
from myMainWindowUI import Ui_MainWindow
class MyMainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs)
super(MyMainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
The setupUi method applies your entire UI design to the class and you can now access all of the widgets you designed by their object names.
win = MyMainWindow()
print win.listWidget
print win.button1
win.show()
See Creating interfaces visually with designer
if using pyqt4 use pyuic4
if using pyside use pyside-uic
These compile the output from QTDesigner into a python file.
You can google you way to usage of these command line tools but Riverbank is usually a good reference
I want to make a QT4 (using QT designer) dialog, that contains a part where a file has to be selected.
Now, I know QFileDialog exists, and I can program something that does what I want.
But can I also just do it in QT designer?
Is there some way to get a "file select" widget in QT designer?
Or, I remember these buttons, having the selected file as a title and a little arrow allowing the user to select another file by the QFileDialog?
So is there a ready made solution, or do I have to program it myself?
There is no file dialog available from the Qt designer as far as I know. But you can easily do it with a few lines of code.
Assuming you have a simple button called pushButton and the path should be stored in lineEdit.
def selectFile():
lineEdit.setText(QFileDialog.getOpenFileName())
pushButton.clicked.connect(selectFile)
[edit]Just wondering though, are you using KDE by any chance? If so, than you can use the KUrlRequester for this. It can easily be configured to support anything from files to urls to directories.
QFileDialog exists in QtGui. At least in my version 4.4 and probably much earlier too. I think the reason it is not in Designer is because it opens its own window instead of being a widget to place on another window.
The documentation from QTDesigner could be better and at least hint of its existence.
Instantiate it and run the show command. It comes right up and defaults to /.
import QtGui
self.fileDialog = QtGui.QFileDialog(self)
self.fileDialog.show()
You can use method getOpenFileName() in QFileDialog Class.
QFileDialog.getOpenFileName() will return the file path and the selected file type
I got this : ('C:/Users/Sathsara/Desktop/UI/Test/test.py', 'All Files (*)')
To get only the file path use QFileDialog.getOpenFileName()[0]
Sample code:
def selectFile():
print(QFileDialog.getOpenFileName()[0])
dlg.locationBtn.clicked.connect(selectFile)