QMainWindow vs QWidget - python-3.x

I can't find good explanations about the different usage of QMainWindow vs. QWidget in PyQt5 (and Qt in general, I guess). From what I've read, QMainWindow inherits from QWidget, so should be able to do everything QWidget can and more. But when I try to convert an example I've found from QWidget into QMainWindow, the layout gets screwed up.
The example I'm trying to work off is from https://www.tutorialspoint.com/pyqt/pyqt_qstackedwidget.htm. (My GUI will have a StackedWidget as central widget, with a sidepanel for navigation, so getting this example to work in a QMainWidget would be a great basis for my own code.)
PyQT5-converted and slightly shortened version:
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import (QApplication, QFormLayout, QLabel, QRadioButton, QCheckBox,
QListWidget, QStackedWidget, QLineEdit,
QHBoxLayout, QGridLayout
)
from PyQt5.Qt import QWidget, QMainWindow
class StackedExample(QMainWindow):
def __init__(self):
super(stackedExample, self).__init__()
self.leftlist = QListWidget ()
self.leftlist.insertItem (0, 'Contact' )
self.leftlist.insertItem (1, 'Personal' )
self.stack1 = QWidget()
self.stack2 = QWidget()
self.stack1UI()
self.stack2UI()
self.Stack = QStackedWidget (self)
self.Stack.addWidget (self.stack1)
self.Stack.addWidget (self.stack2)
grid = QGridLayout()
self.setLayout(grid)
grid.addWidget(self.leftlist,0,0)
grid.addWidget(self.Stack,0,1)
self.leftlist.currentRowChanged.connect(self.display)
self.resize(300,100)
self.show()
def stack1UI(self):
layout = QFormLayout()
layout.addRow("Name",QLineEdit())
layout.addRow("Address",QLineEdit())
self.stack1.setLayout(layout)
def stack2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton("Male"))
sex.addWidget(QRadioButton("Female"))
layout.addRow(QLabel("Sex"),sex)
layout.addRow("Date of Birth",QLineEdit())
self.stack2.setLayout(layout)
def display(self,i):
self.Stack.setCurrentIndex(i)
def main():
app = QApplication(sys.argv)
ex = StackedExample()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This code worked fine while it was based on QWidget, but when it's based on QMainWidget (like in the above version), everything gets crammed into the upper left corner.
What changes are neccessary to show a layout that works for QWidget in a QMainWindow?

A QMainWindow must have a central widget to display correctly. You need to define a central widget and then add any layouts to it instead of the main window.
class StackedExample(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.leftlist = QListWidget()
self.leftlist.insertItem(0, 'Contact' )
self.leftlist.insertItem(1, 'Personal' )
self.central_widget = QWidget() # define central widget
self.setCentralWidget(self.central_widget) # set QMainWindow.centralWidget
self.stack1 = QWidget()
self.stack2 = QWidget()
self.stack1UI()
self.stack2UI()
self.Stack = QStackedWidget (self)
self.Stack.addWidget (self.stack1)
self.Stack.addWidget (self.stack2)
grid = QGridLayout()
self.centralWidget().setLayout(grid) # add the layout to the central widget
grid.addWidget(self.leftlist,0,0)
grid.addWidget(self.Stack,0,1)
self.leftlist.currentRowChanged.connect(self.display)
self.resize(300,100)
self.show()
There are also other areas besides centralWidget defined for a QMainWindow. Here is the documentation for QMainWindow. It explains this in more detail.

Related

QMainWindow mouseDoubleClickEvent override not working

I'm using the below code to try and capture double clicks within the main window"
import sys
from PySide6 import QtCore
from PySide6.QtWidgets import (
QApplication,
QGridLayout,
QMainWindow,
QSplitter,
QTreeView,
QWidget,
)
from PySide6.QtWebEngineWidgets import QWebEngineView
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.windowLayout = QGridLayout()
self.splitter = QSplitter()
self.webView = QWebEngineView()
self.webView.setUrl(QtCore.QUrl("http://127.0.0.1:8080"))
self.tree = QTreeView()
self.splitter.addWidget(self.webView)
self.splitter.addWidget(self.tree)
self.windowLayout.addWidget(self.splitter)
self.mainWidget = QWidget()
self.mainWidget.setLayout(self.windowLayout)
self.setCentralWidget(self.mainWidget)
def mouseDoubleClickEvent(self, e):
print('test')
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(1500, 1000)
window.show()
app.exec()
And I get the following console output:
qt.pointer.dispatch: delivering touch release to same window QWindow(0x0) not QWidgetWindow(0x600003bb8e40, name="MainWindowClassWindow")
qt.pointer.dispatch: skipping QEventPoint(id=1 ts=0 pos=0,0 scn=749.346,451.159 gbl=749.346,451.159 Released ellipse=(1x1 ∡ 0) vel=0,0 press=-749.346,-451.159 last=-749.346,-451.159 Δ 749.346,451.159) : no target window
however, when I comment out self.setCentralWidget(self.mainWidget) I obviously don't see anything, but the double click triggers. Any suggestions?
EDIT: update to min repro, also note that clicking anywhere not inside the QWebEngineView correctly results in mouseDoubleClickEvent triggering. However, the above error results when I click in the QWebEngineView, and nothing happens when I click inside the QTreeView
As per musicamante's very useful comments (see original post), the solution I ended up using was
...
self.tree.doubleClicked.connect(self.treeDoubleClicked)
...
def treeDoubleClicked(self, index):
self.selectedFilepath = index.model().filePath(index)

How can i use QGroupBox with two views?

i have two views (first and second) vertical aligment
import sys
from PyQt5.QtWidgets import QMainWindow,QSplitter,QGroupBox, QApplication, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt
from .First import First
from .Second import Second
class LateralMenu(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.plotview = QGroupBox("Group of views")
self.text = "Menu Lateral"
self.layout = QSplitter(Qt.Vertical)
self.layout_plotview = QVBoxLayout()
self.First = First()
self.Second = Second()
self.layout.addWidget(self.First)
self.layout.addWidget(self.Second)
self.layout_plotview.addWidget(self.layout)
self.plotview.setLayout(self.layout_plotview)
self.setLayout(self.plotview)
i want to put those views in a QGroupBox, but i am getting this error: self.setLayout(self.plotview)
TypeError: setLayout(self, QLayout): argument 1 has unexpected type 'QGroupBox'
what is the problem?
The problem is exactly what the error reports: you're trying to set a layout (which has to be a subclass of QLayout, such as QGridLayout, etc), but the provided argument is a QGroupBox, which is a subclass of QWidget.
If you want to add the QGroupBox to the current widget, you should set a layout for that widget and then add the groupbox to that layout:
def initUI(self):
self.main_layout = QVBoxLayout()
self.setLayout(self.main_layout)
# ...
self.main_layout.addWidget(self.plotview)
If the groupbox is going to be the only "main" widget, there's no need for that, though: you can just subclass from QGroupBox (instead of QWidget):
class LateralMenu(QGroupBox):
# ...
def initUi(self):
self.setTitle("Group of views")
self.text = "Menu Lateral"
self.layout_plotview = QVBoxLayout()
self.setLayout(self.layout_plotview)
self.splitter = QSplitter(Qt.Vertical)
self.First = First()
self.Second = Second()
self.splitter.addWidget(self.First)
self.splitter.addWidget(self.Second)
self.layout_plotview.addWidget(self.splitter)
Note that I've changed some names (most importantly, self.layout has become self.splitter), and that's for clarity: while QSplitter behaves similarly to a layout, it is not a layout, but a QWidget; naming it "layout" might create a lot of confusion.
Also, you should not overwrite existing class properties (such as, indeed, self.layout).
Finally, avoid using capitalized names for variables and attributes: both First and Second instances should be lower case (read more about this in the Style Guide for Python Code).

How can I put the RadioBoxes over the Layout?

I designed the following window. However, when running the code, the RadioBoxes stay behind the layout that contains the frame and the string. Could someone please tell me how to avoid this?
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Window')
self.setFixedSize(550,440)
self.LaySelf = QGridLayout()
self.initWidgets()
self.initUI()
self.show()
def initWidgets(self):
self.Panel = QFrame()
self.Panel.setFrameStyle(QFrame.StyledPanel)
self.Panel.setLineWidth(2)
self.Panel.setStyleSheet('background-color:#f4f2f1')
self.Btt = QRadioButton('Radio',self)
self.Label = QLabel(' '*40+'Hi')
def initUI(self):
self.LaySelf.addWidget(self.Panel,0,0,-1,6)
self.LaySelf.addWidget(self.Label,0,0,-1,6)
self.setLayout(self.LaySelf)
self.Btt.move(200,200)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
In order to make radio button on top of QFrame you need to add it to the Frame. Currently the Radio Buttons stay behind Frame which is covering the layout.
Replace QRadioButton('Radio',self) with QRadioButton('Radio',self.Panel)
Also here is a link to another thread which demostrates Frame Usage with multiple QWidgets

Qt pass focus between windows

In the following example I have a Qt button which opens a pop-out window. However, the pop-out window takes the focus entirely. Is it possible to modify this such that I can still interact with the main window by moving the mouse over it, e.g. to change the value of QDoubleSpinBox even when the pop-out window is open? I think I may need to use the QtHoverEvent class put I can't find a good example of how to do this.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QGridLayout, QDoubleSpinBox,\
QLabel
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = QPushButton('Button', self)
btn.move(50, 50)
btn2 = QDoubleSpinBox(self)
btn2.move(50,100)
self.sideWindowTest = sideWindowTest(self)
btn.clicked.connect(lambda: self.sideWindowTest.setupWindow())
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Main')
self.show()
class sideWindowTest(object):
myWdg = None
def __init__(self, parent):
super().__init__()
self.viewer = parent
def initiateMenuBar(self):
self.myWdg.setWindowTitle('Phasing')
self.myWdg.setWindowModality(Qt.ApplicationModal)
MenuBar = QVBoxLayout()
self.labels = {
'phase 0': QLabel('Phase 0', self.myWdg),
}
self.inputs = {
'phase 0': QDoubleSpinBox(self.myWdg),
}
for i in self.inputs.values():
i.installEventFilter(self.myWdg)
self.inputs['phase 0'].setValue(0)
MenuBar.addWidget(self.labels['phase 0'])
MenuBar.addWidget(self.inputs['phase 0'])
MenuBar.addStretch(1)
return MenuBar
def setupWindow(self):
if not self.myWdg:
self.myWdg = QWidget()
MenuBar = self.initiateMenuBar()
grid = QGridLayout()
grid.setSpacing(10)
grid.addLayout(MenuBar, 0, 0, 1, 2)
self.myWdg.setLayout(grid)
self.myWdg.setGeometry(0, 0, 400, 100)
self.myWdg.show()
self.myWdg.activateWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Yes, but you have to explicitly wrap your QWidget's in a QDialog or a QMainWindow. By default, if you create a orphan QWidget (as you are doing), Qt will wrap it in a QDialog.
Just change your class type to QDialog. Also, you should probably pass in the initial main window as the parent.
btn.clicked.connect(lambda: self.sideWindowTest.setupWindow(self))
...
def setupWindow(self, parent=None):
if not self.myWdg:
self.myWdg = QDialog(parent)

PyQt5 - Add image in background of MainWindow layout

New to PyQt5... Here is a very basic question.
I would like to add an image inside the layout of a widget. This widget is the Main Window / root widget of my application. I use the following code, but I get an error message.
import sys
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(300,300,300,220)
self.setWindowTitle("Hello !")
oImage = QImage("backgound.png")
oLayout = QVBoxLayout()
oLayout.addWidget(oImage)
self.setLayout(oLayout)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
oMainwindow = MainWindow()
sys.exit(app.exec_())
TypeError: QBoxLayout.addWidget(QWidget, int stretch=0, Qt.Alignment alignment=0): argument 1 has unexpected type 'QImage'
Apparently a QLayoutWidget does not accept a QImage as an input. Is there a workaround to have an image appear as a brackground in a QWidget ?
The QVBoxLayout class lines up widgets vertically.
documentation QVBoxLayout
QImage is no widget.
on many widgets e.g. QmainWindow, QLabel you can use
widget.setStyleSheet(„ background-image: url(backgound.png);“)
on my machine this doesn't work with QWidget. In this case you can use the following rewrite of your code:
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QImage, QPalette, QBrush
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setGeometry(100,100,300,200)
oImage = QImage("test.png")
sImage = oImage.scaled(QSize(300,200)) # resize Image to widgets size
palette = QPalette()
palette.setBrush(QPalette.Window, QBrush(sImage))
self.setPalette(palette)
self.label = QLabel('Test', self) # test, if it's really backgroundimage
self.label.setGeometry(50,50,200,50)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
oMainwindow = MainWindow()
sys.exit(app.exec_())

Resources