Remember size of Dock widget on toggle view - python

I have two dock widgets in my QApplication widget.
On each widget toggle view action is called.So that it hides and shows when I check and uncheck the dock widget in menu options.`
On toggle, dock widget are not taking the size which they had before being toggled.
How can I go ahead and implement this?
following the minimal code which produces the same problem.
import sys
from PySide2.QtWidgets import QMainWindow, QAction, qApp,QApplication,QDockWidget,QWidget
from PySide2.QtGui import QIcon
from PySide2.QtCore import Qt
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
menubar = self.menuBar()
fileMenu = menubar.addMenu('&Funcionalities')
dockwindow1 = QDockWidget("dock1",self)
dockwindow1.setWidget(QWidget())
fileMenu.addAction(dockwindow1.toggleViewAction())
self.setCentralWidget(dockwindow1)
dockwindow2 = QDockWidget("dock2",self)
fileMenu.addAction(dockwindow2.toggleViewAction())
self.addDockWidget(Qt.RightDockWidgetArea, dockwindow2)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Simple menu')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())`

Related

System tray application would detect hotkey with PyQt5 - QSystemTrayIcon

I want to create an application with a system tray icon. This app should detect the hotkey and react to it, even if the app is not focused (icon tray).
Example: I want to show the window, then I press Alt + X. The window is showing.
I tried using some global hotkey libraries but whenever the window is showing by hotkey, it freezes. The Show option in my QMenu still working.
I'm new to programming, thanks!
Here is my code:
from PyQt5.QtCore import QSize, Qt
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit, QVBoxLayout, QHBoxLayout, QWidget, QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon, QKeySequence
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('GTyperP5')
self.setWindowIcon(QIcon('GTyperP5.ico'))
self.setFixedSize(QSize(192, 64))
self.setWindowFlag(Qt.WindowMinimizeButtonHint, False)
self.textInput = QLineEdit()
self.typeButton = QPushButton('Type')
self.typeButton.setFixedSize(QSize(50, 25))
self.typeButton.clicked.connect(self.cut_all)
self.textInput.returnPressed.connect(self.typeButton.click)
self.cancelButton = QPushButton('Cancel')
self.cancelButton.setFixedSize(QSize(50, 25))
self.cancelButton.clicked.connect(self.hide_window)
layout = QVBoxLayout()
layout.addWidget(self.textInput, alignment = Qt.AlignHCenter | Qt.AlignTop)
layout_ex = QHBoxLayout()
layout_ex.addWidget(self.cancelButton, alignment = Qt.AlignRight)
layout_ex.addWidget(self.typeButton, alignment = Qt.AlignLeft)
layout.addLayout(layout_ex)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.trayIcon = QSystemTrayIcon(QIcon('GTyperP5.ico'), parent = app)
self.trayIcon.setToolTip('GTyperP5')
menu = QMenu()
showAction = menu.addAction('Show')
showAction.triggered.connect(self.show_window)
exitAction = menu.addAction('Exit')
exitAction.triggered.connect(app.quit)
self.trayIcon.setContextMenu(menu)
self.trayIcon.show()
self.trayIcon.showMessage('GTyperP5 has started!', 'Thank you for using! :D', QIcon('GTyperP5.ico'), msecs = 10000)
self.trayIcon.activated.connect(self.doubleClick)
def closeEvent(self, event):
self.hide_window()
event.ignore()
def doubleClick(self, reason):
if reason == QSystemTrayIcon.DoubleClick:
self.show_window()
def cut_all(self):
clip = QApplication.clipboard()
clip.clear(mode = clip.Clipboard)
clip.setText(self.textInput.text(), mode = clip.Clipboard)
self.textInput.setText('')
self.hide_window()
def hide_window(self):
self.trayIcon.show()
self.hide()
def show_window(self):
self.trayIcon.hide()
self.show()
self.raise_()
self.textInput.setFocus()
app = QApplication([])
window = MainWindow()
window.show()
app.exec()

PyQT layouts: why does QVBoxLayout stack the buttons instead of creating the layout? [duplicate]

I'm making an application in PyQt4 and this is my code so far:
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUi()
def initUi(self):
self.setWindowTitle('Main Menu')
self.setFixedSize(1200, 625)
self.firstWidgets()
self.show()
def firstWidgets(self):
self.vbox1 = QtGui.QVBoxLayout()
self.task1 = QtGui.QLabel('Check 1', self)
self.task1CB = QtGui.QCheckBox(self)
self.hbox1 = QtGui.QHBoxLayout()
self.hbox1.addWidget(self.task1)
self.hbox1.addWidget(self.task1CB)
self.vbox1.addLayout(self.hbox1)
self.setLayout(self.vbox1)
def main():
application = QtGui.QApplication(sys.argv)
gui = MainWindow()
sys.exit(application.exec_())
if __name__=='__main__':
main()
My problem is in MainWindow.firstWidgets(). I try to set a layout but I get an error even though that's my first time using .setLayout for that form, which confuses me.
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "",
which already has a layout
You can't set a QLayout directly on the QMainWindow. You need to create a QWidget and set it as the central widget on the QMainWindow and assign the QLayout to that.
wid = QtGui.QWidget(self)
self.setCentralWidget(wid)
layout = QtGui.QVBoxLayout()
wid.setLayout(layout)
NOTE: This is for Qt4 -- see the other answer on this question for the Qt5 updated code.
Just an update to Brenden Abel's answer:
QWidget and QVBoxLayout (for Python3, PyQt5) are now contained in the PyQt5.QtWidgets module and not the PyQt5.QtGui module.
So updated code:
wid = QtWidgets.QWidget(self)
self.setCentralWidget(wid)
layout = QtWidgets.QVBoxLayout()
wid.setLayout(layout)
This is an example using PyQt5
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('My App')
# Cannot set QxxLayout directly on the QMainWindow
# Need to create a QWidget and set it as the central widget
widget = QWidget()
layout = QVBoxLayout()
b1 = QPushButton('Red' ); b1.setStyleSheet("background-color: red;")
b2 = QPushButton('Blue' ); b2.setStyleSheet("background-color: blue;")
b3 = QPushButton('Yellow'); b3.setStyleSheet("background-color: yellow;")
layout.addWidget(b1)
layout.addWidget(b2)
layout.addWidget(b3)
widget.setLayout(layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

PyQt5 Layouts not moving button [duplicate]

I'm making an application in PyQt4 and this is my code so far:
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUi()
def initUi(self):
self.setWindowTitle('Main Menu')
self.setFixedSize(1200, 625)
self.firstWidgets()
self.show()
def firstWidgets(self):
self.vbox1 = QtGui.QVBoxLayout()
self.task1 = QtGui.QLabel('Check 1', self)
self.task1CB = QtGui.QCheckBox(self)
self.hbox1 = QtGui.QHBoxLayout()
self.hbox1.addWidget(self.task1)
self.hbox1.addWidget(self.task1CB)
self.vbox1.addLayout(self.hbox1)
self.setLayout(self.vbox1)
def main():
application = QtGui.QApplication(sys.argv)
gui = MainWindow()
sys.exit(application.exec_())
if __name__=='__main__':
main()
My problem is in MainWindow.firstWidgets(). I try to set a layout but I get an error even though that's my first time using .setLayout for that form, which confuses me.
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "",
which already has a layout
You can't set a QLayout directly on the QMainWindow. You need to create a QWidget and set it as the central widget on the QMainWindow and assign the QLayout to that.
wid = QtGui.QWidget(self)
self.setCentralWidget(wid)
layout = QtGui.QVBoxLayout()
wid.setLayout(layout)
NOTE: This is for Qt4 -- see the other answer on this question for the Qt5 updated code.
Just an update to Brenden Abel's answer:
QWidget and QVBoxLayout (for Python3, PyQt5) are now contained in the PyQt5.QtWidgets module and not the PyQt5.QtGui module.
So updated code:
wid = QtWidgets.QWidget(self)
self.setCentralWidget(wid)
layout = QtWidgets.QVBoxLayout()
wid.setLayout(layout)
This is an example using PyQt5
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('My App')
# Cannot set QxxLayout directly on the QMainWindow
# Need to create a QWidget and set it as the central widget
widget = QWidget()
layout = QVBoxLayout()
b1 = QPushButton('Red' ); b1.setStyleSheet("background-color: red;")
b2 = QPushButton('Blue' ); b2.setStyleSheet("background-color: blue;")
b3 = QPushButton('Yellow'); b3.setStyleSheet("background-color: yellow;")
layout.addWidget(b1)
layout.addWidget(b2)
layout.addWidget(b3)
widget.setLayout(layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

QTabWidget insert a QSplitter can't switch when the splitter disabled

I insert a QFrame and QTabWidget in the QSplitter. And I wanna forbidden to adjust the size of elements in QSplitter. So I call method of 'setDisabled' in QSplitter. It's useful for disabling resizing the elements. But I also can't switch tab of QTabWidget. Who can give me some suggestions? Thanks very much......
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QSplitter, QHBoxLayout, QFrame, QTabWidget
from PyQt5.QtCore import Qt
class Example1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle("Demo")
self.layout = QHBoxLayout()
top_frame = QFrame()
top_frame.setFrameShape(QFrame.StyledPanel)
bottom_frame = QTabWidget(self)
tab1 = QWidget()
tab2 = QWidget()
bottom_frame.setTabText(0, "Generic")
bottom_frame.setTabText(1, "Other")
bottom_frame.addTab(tab1, "Tab 1")
bottom_frame.addTab(tab2, "Tab 2")
splitter = QSplitter()
splitter.setOrientation(Qt.Vertical)
splitter.addWidget(top_frame)
splitter.addWidget(bottom_frame)
splitter.setSizes([300, 300])
**splitter.setDisabled(True)**
self.layout.addWidget(splitter)
self.setLayout(self.layout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example1()
sys.exit(app.exec_())
the running result of the program
When you disable a widget you also disable its children, so disabling the QSplitter also disables the QTabWidget.
A possible solution is enable or disable the handles:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QFrame,
QHBoxLayout,
QSplitter,
QSplitterHandle,
QTabWidget,
QWidget,
)
class CustomSplitter(QSplitter):
#property
def enabled(self):
if not hasattr(self, "_enabled"):
self._enabled = True
return self._enabled
#enabled.setter
def enabled(self, d):
self._enabled = d
for i in range(self.count()):
self.handle(i).setEnabled(self.enabled)
def createHandle(self):
handle = super().createHandle()
handle.setEnabled(self.enabled)
return handle
class Example1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle("Demo")
self.layout = QHBoxLayout()
top_frame = QFrame()
top_frame.setFrameShape(QFrame.StyledPanel)
bottom_frame = QTabWidget(self)
tab1 = QWidget()
tab2 = QWidget()
bottom_frame.setTabText(0, "Generic")
bottom_frame.setTabText(1, "Other")
bottom_frame.addTab(tab1, "Tab 1")
bottom_frame.addTab(tab2, "Tab 2")
splitter = CustomSplitter()
splitter.setOrientation(Qt.Vertical)
splitter.addWidget(top_frame)
splitter.addWidget(bottom_frame)
splitter.setSizes([300, 300])
splitter.enabled = False
self.layout.addWidget(splitter)
self.setLayout(self.layout)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example1()
sys.exit(app.exec_())
I haven't used a QSplitter before but are the .setFixedHeight(300) .setFixedWidth(300), or .setFixedSize(300, 300) methods not applicable here?
QSplitter lets you get hold of its handles, which are the GUI element visible to the user. If you have two widgets in a splitter, you have a single visible handle; the handle at index 0 is always invisible.
You can manipulate that widget explicitely, e.g. disable it. Try:
splitter.handle(1).enabled = False
This disables only said GUI element, while the rest of the splitter (your two content widgets) will stay enabled.

How to add actions menu in a toolbar?

I want to add a menu from an item in a toolbar.
For example, from the following code:
import sys
from PyQt5.QtWidgets import QAction, QMainWindow, QApplication
class Menu(QMainWindow):
def __init__(self):
super().__init__()
colors = QAction('Colors', self)
exitAct = QAction('Exit', self)
self.statusBar()
toolbar = self.addToolBar('Exit')
toolbar.addAction(colors)
toolbar.addAction(exitAct)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
menu = Menu()
sys.exit(app.exec_())
I get:
I want to press on 'Colors' and get a list of options (like Qmenu, but for the toolbar).
How can I achieve this?
If you wish to add a QMenu to a QToolBar item you must add a widget that supports it, for example a QPushButton:
import sys
from PyQt5 import QtWidgets
class Menu(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
colorButton = QtWidgets.QPushButton("Colors")
exitAct = QtWidgets.QAction('Exit', self)
toolbar = self.addToolBar("Exit")
toolbar.addWidget(colorButton)
toolbar.addAction(exitAct)
menu = QtWidgets.QMenu()
menu.addAction("red")
menu.addAction("green")
menu.addAction("blue")
colorButton.setMenu(menu)
menu.triggered.connect(lambda action: print(action.text()))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
menu = Menu()
menu.show()
sys.exit(app.exec_())

Categories