PySide how to set focus to new window and open it if not exists - qt

I have started learning PySide from example of Zetcode and try to code app which had two windows: "Schematic View" which is parent of "Layout View", each with menu bar. On start that should be only schematic window, and layout win should be started by switchtoLAYOUT in root of menubar.
My questions are:
How to make "switchtoLAYOUT" in root not to show the dropdown and still do action with only one instance of "Layout View" window ?
How to switch the focus between two windows ("switchtoLAYOUT" and "switchtoSCHEMATIC")?
Please inspect my code and suggest me something smart (that should not be hard).
The Code:
import sys
from PySide import QtCore, QtGui
class schematicWindow(QtGui.QMainWindow):
def __init__(self):
super(schematicWindow, self).__init__()
self.defineSchWin()
def defineSchWin(self):
exitAction = QtGui.QAction('&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
menubar.addMenu('&Edit')
menubar.addMenu('&Passives')
menubar.addMenu('&Descretes')
menubar.addMenu('&IC\'s')
swToLayMenu = menubar.addMenu('switchtoLAYOUT')
swToLayAction = QtGui.QAction(self)
swToLayAction.triggered.connect(self.layoutWindow)
swToLayMenu.addAction(swToLayAction) # open layoutWindow (if not exists)
# and set focus to layoutWindow
self.setGeometry(0, 300, 500, 300)
self.setWindowTitle('Schematic View')
self.show()
def layoutWindow(self):
window = QtGui.QMainWindow(self)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.statusBar()
menubar = window.menuBar()
switchtoSchMenu = menubar.addMenu('switchtoSCHEMATIC')
window.setGeometry(100, 600, 500, 300)
window.setWindowTitle('Layout View')
window.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = schematicWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

You need to keep a reference to the layout window in your class, (you should put self.layout_window = None in the __init__). This function now checks if the window has been initialized, makes it if it has not been, makes sure it is visible, and then sets the new window to be the active window. Something like: (this is not tested)
def layoutWindow(self):
if self.layout_window is None:
window = QtGui.QMainWindow(self)
self.layout_window = window
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.statusBar()
menubar = window.menuBar()
switchtoSchMenu = menubar.addMenu('switchtoSCHEMATIC')
window.setGeometry(100, 600, 500, 300)
window.setWindowTitle('Layout View')
else:
window = self.layout_window
window.show()
window.activateWindow()
window.raise() # just to be sure it's on top
(doc)

Related

Resize QDialog when RadioButton is Checked PyQt

not so many experience at all with pyqt..
I have designed an UI with Qt Designer with 2 Radiobuttons.
Depending on the RadioButton selected some widgets are visible and other not. What I'm not trying to do is to resize automatically the layout of the dialog depending on how many widgets are visible in the UI.
The question is very similar to this one . Here the extract of the code that might be helpful to understand the problem (I know it is just a piece of the code, but the entire dialog is pretty long):
class MyDialog(QDialog, FORM_CLASS):
..........
# connect the radioButton with a function that reloads the UI
self.radioSingle.toggled.connect(self.refreshWidgets)
.....
# dictionary with all the widgets and the values
self.widgetType = {
self.cmbModelName: ['all'],
self.cmbGridLayer: ['all'],
self.cmbRiverLayer: ['all'],
self.lineNewLayerEdit: ['all'],
self.lineLayerNumber: ['single']
}
# function that refresh the UI with the correct widgets depending on the radiobutton selected
def refreshWidgets(self, idx):
'''
refresh UI widgets depending on the radiobutton chosen
'''
tp = self.radioSingle.isChecked()
for k, v in self.widgetType.items():
if tp:
if 'all' or 'single' in v:
active = True
k.setEnabled(active)
k.setVisible(active)
else:
active = 'all' in v
k.setEnabled(active)
k.setVisible(active)
# attempt to resize the Dialog
self.setSizeConstraint()
Surely the code could be improved (not so skilled with code writing)...
You just gotta set_up your UI to resize when it's needed.
There is a lot of more beautiful way to do this, but here is a simple and fast example:
You just have to define your layout to behave the way you want. If you make use of Minimum and Maximum size it'll resize as you need.
You could also have your own personalized buttons, widgets, .... you would have a cleaner code.
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QRadioButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class CustomDialog(QDialog):
w_container = None
v_layout_container = None
v_scroll_area = None
v_layout_preview = None
def __init__(self):
"""Init UI."""
super(CustomDialog, self).__init__()
self.init_ui()
def init_ui(self):
"""Init all ui object requirements."""
self.r1 = QRadioButton("smaller")
self.r2 = QRadioButton("bigger")
style = "background: green;"
size = [200, 50]
self.small_size = [200, 250]
self.big_size = [200, 500]
self.resize(200,200)
self.w1 = QWidget()
self.w2 = QWidget()
self.w3 = QWidget()
self.w4 = QWidget()
self.w5 = QWidget()
self.w6 = QWidget()
self.w1.setStyleSheet(style)
self.w2.setStyleSheet(style)
self.w3.setStyleSheet(style)
self.w4.setStyleSheet(style)
self.w5.setStyleSheet(style)
self.w6.setStyleSheet(style)
self.w1.setFixedSize(size[0], size[1])
self.w2.setFixedSize(size[0], size[1])
self.w3.setFixedSize(size[0], size[1])
self.w4.setFixedSize(size[0], size[1])
self.w5.setFixedSize(size[0], size[1])
self.w6.setFixedSize(size[0], size[1])
self.full_container = QVBoxLayout()
self.full_container.setContentsMargins(0,0,0,0)
self.radio_widget = QWidget()
self.radio_widget.setFixedSize(200, 50)
self.radio_container = QHBoxLayout()
self.radio_container.setContentsMargins(0,0,0,0)
self.widget_widget = QWidget()
self.widget_container = QVBoxLayout()
self.radio_widget.setLayout(self.radio_container)
self.widget_widget.setLayout(self.widget_container)
self.full_container.addWidget(self.radio_widget)
self.full_container.addWidget(self.widget_widget)
self.radio_container.addWidget(self.r1)
self.radio_container.addWidget(self.r2)
self.widget_container.addWidget(self.w1)
self.widget_container.addWidget(self.w2)
self.widget_container.addWidget(self.w3)
self.widget_container.addWidget(self.w4)
self.widget_container.addWidget(self.w5)
self.widget_container.addWidget(self.w6)
self.setLayout(self.full_container)
self.r1.setChecked(True)
def paintEvent(self, event):
if self.r1.isChecked():
self.w4.hide()
self.w5.hide()
self.w6.hide()
self.setFixedSize(self.small_size[0],self.small_size[1])
elif self.r2.isChecked():
self.w4.show()
self.w5.show()
self.w6.show()
self.setFixedSize(self.big_size[0], self.big_size[1])
self.full_container.update()
super(CustomDialog, self).paintEvent(event)
def run():
app = QApplication(sys.argv)
GUI = CustomDialog()
GUI.show()
sys.exit(app.exec_())
run()
Observe that I'm using PyQt5, so if you need you just gotta change your imports to the version you are using.

How to show clickable QFrame without loosing focus from main window?

Finally I am able to create a chrome like tab in Qt/PyQt QMainWindow. After unsuccessfully trying to port this Qt C++ non client area painting code, I revise my thinking to be this way : trick visually by displaying a free floating QFrame that get resized and moved together with main window. Surely this is not a perfect solution (for example this code still don't solve when to disable topmost hint where the another application is on top of the main application window, but I think that's quite easy to solve)
What I want to ask in this page, is how to keep the click action on this QFrame window button from stealing focus from main window? At the moment I simply reactivate the main window when click action does occur. But it creates flashing effect on the mainwindow titlebar. I believe this SO page gives the answer, but I haven't been able to create a successful result from this C++ code:
HWND winHandle = (HWND)winId();
ShowWindow(winHandle, SW_HIDE);
SetWindowLong(winHandle, GWL_EXSTYLE, GetWindowLong(winHandle, GWL_EXSTYLE)
| WS_EX_NOACTIVATE | WS_EX_APPWINDOW);
ShowWindow(winHandle, SW_SHOW);
Into this PyQt code:
def no_focus(self):
import ctypes, win32con, win32gui
dc = win32gui.GetWindowDC(self.winId())
user32 = ctypes.windll.user32
user32.SetWindowLongW(dc, win32con.GWL_EXSTYLE, user32.GetWindowLongW(dc, win32con.GWL_EXSTYLE) | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_APPWINDOW)
Would love to let you see and test the fully functional code below:
__author__ = 'Eko Wibowo'
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class FSTabHeader(QFrame):
def __init__(self, parent):
super(FSTabHeader, self).__init__(None)
self.mainwindow = parent
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SplashScreen)
self.setFocusPolicy(Qt.NoFocus)
self.setAttribute(Qt.WA_ShowWithoutActivating)
layout = QHBoxLayout(self)
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 0)
tab_text = 'Click me'
self.btn_tab = QPushButton(self)
self.btn_tab.setStyleSheet('border:1px')
self.btn_tab.setContentsMargins(0,0,0,0)
self.btn_tab.setText(tab_text)
self.btn_tab.setMinimumHeight(25 + 1)
self.btn_tab.setMaximumHeight(25 + 1)
self.btn_tab.setMinimumWidth(60)
self.btn_tab.setMaximumWidth(60)
self.btn_tab.setCursor(Qt.PointingHandCursor)
self.btn_tab.clicked.connect(self.dummy)
layout.addWidget(self.btn_tab)
self.setLayout(layout)
self.show()
def dummy(self):
print 'it create flashes effect on mainwindow titlebar'
self.mainwindow.activateWindow()
def no_focus(self):
import ctypes, win32con, win32gui
dc = win32gui.GetWindowDC(self.winId())
user32 = ctypes.windll.user32
user32.SetWindowLongW(dc, win32con.GWL_EXSTYLE, user32.GetWindowLongW(dc, win32con.GWL_EXSTYLE) | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_APPWINDOW)
def adjust_position(self):
top_left = self.mainwindow.mapToGlobal(self.mainwindow.rect().topLeft())
self.move(top_left.x() + 20 + 5, top_left.y() - self.height() + 1)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.tab_header = FSTabHeader(self)
self.tab_header.no_focus()
def resizeEvent(self, *args, **kwargs):
self.tab_header.adjust_position()
def moveEvent(self, *args, **kwargs):
self.tab_header.adjust_position()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWindow = MainWindow(None)
mainWindow.show()
app.exec_()
Any suggestions?

pyqt qt4 How to add a tiny arrow/collapse button to QSplitter

You can customize the thickness and background image of the splitter handle
http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qsplitter
But is there a way to add a small graphical arrow button so when the user clicks on it, I can call splitter.setSizes([0, 1]) to collapse it.
I could use a vbox layout with a QButton but it takes up too much space and doesn't look as nice.
You could subclass QSplitter and reimplement its createHandle method. This would allow you to return an instance of your own QSplitterHandle class, with, say, a reimplemented paintEvent.
Alternatively, and more simply, you could add buttons directly to an existing splitter handle, by setting your own layout on it.
Here's a basic demo to get you started (probably needs some tweaking to make it prettier):
PyQt5:
from PyQt5 import QtCore, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.splitter = QtWidgets.QSplitter(self)
self.splitter.addWidget(QtWidgets.QTextEdit(self))
self.splitter.addWidget(QtWidgets.QTextEdit(self))
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.splitter)
handle = self.splitter.handle(1)
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
button = QtWidgets.QToolButton(handle)
button.setArrowType(QtCore.Qt.LeftArrow)
button.clicked.connect(
lambda: self.handleSplitterButton(True))
layout.addWidget(button)
button = QtWidgets.QToolButton(handle)
button.setArrowType(QtCore.Qt.RightArrow)
button.clicked.connect(
lambda: self.handleSplitterButton(False))
layout.addWidget(button)
handle.setLayout(layout)
def handleSplitterButton(self, left=True):
if not all(self.splitter.sizes()):
self.splitter.setSizes([1, 1])
elif left:
self.splitter.setSizes([0, 1])
else:
self.splitter.setSizes([1, 0])
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 300, 300)
window.show()
sys.exit(app.exec_())
PyQt4:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.splitter = QtGui.QSplitter(self)
self.splitter.addWidget(QtGui.QTextEdit(self))
self.splitter.addWidget(QtGui.QTextEdit(self))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.splitter)
handle = self.splitter.handle(1)
layout = QtGui.QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
button = QtGui.QToolButton(handle)
button.setArrowType(QtCore.Qt.LeftArrow)
button.clicked.connect(
lambda: self.handleSplitterButton(True))
layout.addWidget(button)
button = QtGui.QToolButton(handle)
button.setArrowType(QtCore.Qt.RightArrow)
button.clicked.connect(
lambda: self.handleSplitterButton(False))
layout.addWidget(button)
handle.setLayout(layout)
def handleSplitterButton(self, left=True):
if not all(self.splitter.sizes()):
self.splitter.setSizes([1, 1])
elif left:
self.splitter.setSizes([0, 1])
else:
self.splitter.setSizes([1, 0])
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 300, 300)
window.show()
sys.exit(app.exec_())

pyqt4 - singleapplication - bring up the original window in an attempt to open the app for the second time

I want only one instance of my app to be running at each time. but when the user attempts to open it the second time, I want the first window to be brought to the front (it could be just minimized or minimized to the corner of the taskbar and the user doesn't know how to open it)
I have this code that does the detection job and it doesn't allow the second instance. I have trouble with the part that it has to open the original window. I have commented out some of my attempt.
import sys
from PyQt4 import QtGui, QtCore
import sys
class SingleApplication(QtGui.QApplication):
def __init__(self, argv, key):
QtGui.QApplication.__init__(self, argv)
self._activationWindow=None
self._memory = QtCore.QSharedMemory(self)
self._memory.setKey(key)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(
self._memory.errorString().toLocal8Bit().data())
def isRunning(self):
return self._running
def activationWindow(self):
return self._activationWindow
def setActivationWindow(self, activationWindow):
self._activationWindow = activationWindow
def activateWindow(self):
if not self._activationWindow:
return
self._activationWindow.setWindowState(
self._activationWindow.windowState() & ~QtCore.Qt.WindowMinimized)
self._activationWindow.raise_()
self._activationWindow.activateWindow()
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel(self)
self.label.setText("Hello")
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.label)
if __name__ == '__main__':
key = 'random _ text'
app = SingleApplication(sys.argv, key)
if app.isRunning():
#app.activateWindow()
sys.exit(1)
window = Window()
#app.setActivationWindow(window)
#print app.topLevelWidgets()[0].winId()
window.show()
sys.exit(app.exec_())
I've made this work on Windows using the win32 api (I'm not entirely sure, but there are probably equivalent calls on macos/unix).
Add the following import to your application,
import win32gui
set the window title to a fixed name (instead of doing this, you could store its whndl in the shared memory)
window = Window()
window.setWindowTitle('Single Application Example')
window.show()
and then change your activateWindow method to something like the following:
def activateWindow(self):
# needs to look for a named window ...
whndl = win32gui.FindWindowEx(0, 0, None, "Single Application Example")
if whndl is 0:
return #couldn't find the name window ...
#this requests the window to come to the foreground
win32gui.SetForegroundWindow(whndl)
You might be interested by the solutions proposed here
For instance, I would try:
app = SingleApplication(sys.argv, key)
if app.isRunning():
window = app.activationWindow()
window.showNormal()
window.raise()
app.activateWindow()
sys.exit(1)
window = Window()
app.setActivationWindow(window)
window.setWindowFlags(Popup)
window.show()

Turning WA_TranslucentBackground off stops window repainting

I have a PyQt4.9 window where I would like to toggle the translucency on or off. The reason being is that it sometimes shows a full size phonon video control which doesn't work when the WA_TranslucentBackground attribute is set. (Due to a Qt bug https://bugreports.qt.io/browse/QTBUG-8119)
The problem I have is, after I turn WA_TranslucentBackground attribute back to false, after it has been true, the Window will no longer redraw, so it remains stuck showing the same thing from that point on. Interestingly, click events still respond.
Some example code follows. Click the increment button, and it will update the button text. Click the toggle button and then click the increment button again, and updates no longer show. Clicking the exit button closes the window, showing the events are still responding.
If anyone has any solutions, workarounds or fixes I'd appreciate them. Thanks.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Settings(QWidget):
def __init__(self, desktop):
QWidget.__init__(self)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setWindowFlags(Qt.FramelessWindowHint)
self.istransparent = True
self.count = 0
self.setWindowTitle("Transparent")
self.resize(300, 150)
self.incr_button = QPushButton("Increment")
toggle_button = QPushButton("Toggle Transparency")
exit_button = QPushButton("Exit")
grid = QGridLayout()
grid.addWidget(self.incr_button, 0, 0)
grid.addWidget(toggle_button, 1, 0)
grid.addWidget(exit_button, 2, 0)
self.setLayout(grid)
self.connect(toggle_button, SIGNAL("clicked()"), self.toggle)
self.connect(self.incr_button, SIGNAL("clicked()"), self.increment)
self.connect(exit_button, SIGNAL("clicked()"), self.close)
def increment(self):
self.count = self.count + 1
self.incr_button.setText("Increment (%i)" % self.count)
def toggle(self):
self.istransparent = not self.istransparent
self.setAttribute(Qt.WA_TranslucentBackground, self.istransparent)
if __name__ == "__main__":
app = QApplication(sys.argv)
s = Settings(app.desktop())
s.show()
sys.exit(app.exec_())
Try to replace self.setAttribute(Qt.WA_TranslucentBackground, ...) calls in __init__ and toggle with following method.
def set_transparency(self, enabled):
if enabled:
self.setAutoFillBackground(False)
else:
self.setAttribute(Qt.WA_NoSystemBackground, False)
self.setAttribute(Qt.WA_TranslucentBackground, enabled)
self.repaint()
Tested on PyQt-Py2.7-x86-gpl-4.9-1 (Windows 7)

Resources