So I have a MainWindow.ui in the class QMainWindow. I also have a separate QWidget and QDialog window (not sure which one is better to use) that I would like to open by using the menu bar from MainWindow.ui. I know that I can simply do something like:
self.main_layout.addWidget(self.form_widget)
or
flashC = flashCard()
flashC.show()
But I want to do this in Qt Designer, so that I don't have to change the .py file every time I run pyuic5 (since these changes will be replaced upon running.)
My question is, how do I make it so that QMainWindow can open other windows that I make later from within Qt Designer. Right now I can't access any other windows in my signal/slot editor.
I've been trying to figure this out and the books and other resources that I've looked at haven't pointed me in the right direction. I just want to know if it's possible to call other windows from within Qt Designer.
You can have multiple QMainWindow in your application. To create a second window simply define it in Qt Designer as you have for your current window, then import and create an instance of it.
Remember it will only appear once you call .show()
Related
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)
I'm starting to use Qt Designer.
I am trying to create a game, and the first task that I want to do is to create a window where you have to input the name of the map that you want to load. If the map exists, I then switch to the main game window, and if the name of the map doesn't exist, I want to display a popup window that tells the user that the name of the map they wrote is not valid.
I'm a bit confused with the part of showing the "not valid" pop-up window.
I realized that I have two options:
Creating 2 separated .ui files, and with the help of the .show() and .hide() commands show the correspoding window if the user input is invalid.
The other option that I'm thinking of creating both windows in the same .ui file, which seems to be a better option, but I don't really know how to work with windows that come from the same file. Should I create a separate class for each of the windows that come from the Qt Designer file? If not, how can I access both windows from the same class?
Your second option seems impossible, it would be great to share the .ui since in my years that I have worked with Qt Designer I have not been able to implement what you point out.
An .ui is an XML file that describes the elements and their properties that will be used to create a class that is used to fill a particular widget. So considering the above, your second option is impossible.
This concludes that the only viable option is its first method.
I'm creating a custom ui in Maya 2017 which uses PyQt5 (well... technically PySide2, but it's essentially the same).
I've got a few CustomContextMenu popup menus that I've created in my ui and I've used popup.setTearOffEnabled(True) to be able to tear them off into a separate window (popup, being the QMenu item).
I cannot seem to figure out how to set the title for the resulting torn off window. Currently, each torn off window is titled "Maya-2017", but I'd like to give it a unique name for clarity. I've noticed that Maya's menu items with tear off functionality name the resulting window with the menu's name, so it would seem this is doable. Am I just missing something obvious?
I have tried using popup.setTitle('test name') on the QMenu thinking it would then name the tear off window this title, but it doesn't seem to do anything. Other than that, I'm at a loss.
I'm not sure whether torn-off menus appear the same on all platforms, but on my Linux system, they are shown as tool windows with a title-bar. So the title can be set like this:
menu = QMenu('File')
menu.setTearOffEnabled(True)
menu.setWindowTitle('File')
I use PyQt4 and Python 2.7.9.
My program contains a few QLineEdit objects. The problem is that when the program is launched, one of the QLineEdits is being focused automatically, which causes my placeholder text to disappear.
Is there any way to prevent it, or at least don't let it hide the placeholder text?
Another way is
self.this_widget.clearFocus()
after window has been shown. Only in Qt5 placeholder texts are displayed even with focus. So maybe switch to PyQt5.
You can use setFocus to put the focus on a different widget (although, depending on which widget you pick, you might also need to set the focus-policy first):
self.some_other_widget.setFocusPolicy(QtCore.Qt.TabFocus)
self.some_other_widget.setFocus()
Alternatively, if you use Qt Designer to create the GUI, you could edit the tab-order so that the line-edit is not the first in the chain. This can also be done programmatically using QWidget.setTabOrder.
I'm using QtDesigner and have an MDI application with two tabs. I can't seem to get the tabs to open up the way I want them to. I'd like subwindow to have the focus and be the first one on the left when the application opens up. So 'subwindow_2' would be on the right and would not have the focus when the application opens.
I have the focus correct when the application opens. But the tab order is wrong. In other words the focus is on 'subwindow' but that shows up on the right instead of the left.
I've tried a number of things in QtDesigner such as changing activation order between creation order and stacking order but that seems to have no effect. How do I fix this ?
import sys
from PyQt4 import QtCore, QtGui
from phreqMDI import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyForm,self).__init__(parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.ui.mdiArea.addSubWindow(self.ui.subwindow_2)
self.ui.mdiArea.addSubWindow(self.ui.subwindow)
Someone mentioned that pyuic4 doesn't do well with MDI. Perhaps there is something I need to change in my code instead of doing it in QtDesigner ?
You need to add them in the correct tab order, and then explicitly activate the relevant subwindow:
self.ui.mdiArea.addSubWindow(self.ui.subwindow)
self.ui.mdiArea.addSubWindow(self.ui.subwindow_2)
self.ui.mdiArea.setActiveSubWindow(
self.ui.mdiArea.subWindowList()[0])
PS:
For some reason, it seems necessary to use the subWindowList when setting the active subwindow during __init__. Passing the return value of addSubWindow to setActiveSubWindow didn't work for me - but that may be a platform-specific thing (I tested on Linux).
UPDATE:
So, assuming you're on Windows, there do seem to be some platform-specific differences. As a workaround, try setting the active subwindow with a timer, like this:
self.ui.mdiArea.addSubWindow(self.ui.subwindow)
self.ui.mdiArea.addSubWindow(self.ui.subwindow_2)
QtCore.QTimer.singleShot(10,
lambda: self.ui.mdiArea.setActiveSubWindow(
self.ui.mdiArea.subWindowList()[0]))