Can't get MDI tab order right - python

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]))

Related

Selecting files through directory explorer and saving path as variable [duplicate]

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)

How to open a separate UI Window from QMainWindow?

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()

PySide - GUI inactive zone on the menu bar

I am coding a GUI in PySide for a couple of month now. This soft is half developed using Qt designer and half hard coded. It is a MainWindow soft with a central widget and a lot of docks.
Recently I realized that the menu bar (and a tiny bit of the task bar) had a zone which was 'inactive', ie the mouse can not access the menus directly. This zone is highlighted in green in the picture.
The menus are working, since when I click on the right half of the configure menu button, I can access the File or Edit menu as shown in the following picture
The code translating the Qt designer output into python code is
call pyside-uic mainWindow.ui -o uiMainWindow.py
call pyside-rcc -o fittingRessources_rc.py pathtoressources\fittingRessources.qrc
I have tried to remove the icon of the soft. Move the task bars (in which case the buttons of the task bar are entirely active again). I have tried to remove the icons of the task bar. Nothing made a difference.
I can provide code if necessary, but first I was wondering if any of you already encountered this issue? and how it solved it? since it seems a bit ackward...
Cheers
The error appeared only when I was adding certain docks to the MainWindow using the following code:
self.variablesDock = VariablesDock(self,self.dataCurve)
where self is mainWindow. The VariablesDock class is the following
class VariablesDock(QWidget):
'''
Allows to define variables
'''
def __init__(self, mainWindow, dataCurve):
'''
Constructor
'''
super(VariablesDock,self).__init__(mainWindow)
self.mainWindow = mainWindow
self.dataCurve = dataCurve
self.variablesDockWidget = QtGui.QDockWidget(mainWindow)
... skip some code ...
self.variablesDockWidget.setWidget(self.dockWidgetContents)
mainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.variablesDockWidget)
self.variablesDockWidget.setWindowTitle("Variables")
if I change the line super(VariablesDock,self).__init__(mainWindow) into
super(VariablesDock,self).__init__(None)
the error disappeared.
Cheers

PyQt4 - Making a QIcon look active or "pressed down"

I have a QMainWindow with a toolbar that has a bunch of QIcon buttons. Clicking these buttons switches between widgets in a QStackedWidget. Basically, the buttons function as tabs, opening different parts of the program. Now, what I want to do is make a specific button look pressed down when the corresponding widget is active, so it's easier to see which part of the program is active. Like this:
I rephrased it 20 different ways while Googling and checked out the manual but I'm still at a loss. I'm probably missing something painfully obvious here.
Of course I was missing something painfully obvious :P Here's the solution:
# Create the button and make it checkable
self.testButton = QtGui.QAction(QtGui.QIcon('images/icons/test.png'), 'Test', self)
self.testButton.setCheckable(True)
# Mark the button as checked
self.testButton.setChecked(True)

System theme icons and PyQt4

I'm writing a basic program in python using the PyQt4 module. I'd like to be able to use my system theme's icons for things like the preference dialog's icon, but i have no idea how to do this. So my question is, how do you get the location of an icon, but make sure it changes with the system's icon theme? If it matters, i'm developing this under ubuntu 9.04, so i am using the gnome desktop.
Unfortunately, It appears that Qt does not support getting icons for a specific theme. There are ways to do this for both KDE and Gnome.
The KDE way is quite elegant, which makes sense considering that Qt is KDE's toolkit. Instead of using the PyQt4.QtGui class QIcon, you instead use the PyKDE4.kdeui class KIcon. An example of this is:
from PyKDE4.kdeui import *
icon = KIcon("*The Icon Name*")
see the PyKDE documentation for this class, here.
One way to gain support for this for gnome is to use the python gtk package. It is not as nice as the kde way, but it works none the less. It can be used like this:
from PyQt4 import QtGui
from gtk import icon_theme_get_default
iconTheme = icon_theme_get_default()
iconInfo = iconTheme.lookup_icon("*The Icon Name*", *Int of the icon size*, 0)
icon = QtGui.QIcon(iconInfo.get_filename())
See the documentation for the Icon Theme class and Icon Info class.
EDIT: thanks for the correction CesarB
Use the PyKDE4 KIcon class:
http://api.kde.org/pykde-4.2-api/kdeui/KIcon.html
I spent a decent amount of researching this myself not long ago, and my conclusion was that, unfortunately, Qt doesn't provide this functionality in a cross-platform fashion. Ideally the QIcon class would have defaults for file open, save, '+', '-', preferences, etc, but considering it doesn't you'll have to grab the appropriate icon for your desktop environment.

Categories

Resources