I'm trying to integrate a QMenubar in a QWidget in Pyside2 in Python3. It seems to be easier to integrate a QMenubar to a QMainWindow but I started the project with a QWidget and not a QMainWindow and this is why I would like to have a solution for a QWidget.
When I run the code, there is no error message but the menubar doesn't appear when I run the app. I checked that stackoveflow page but it didn't help me or I wasn't able to implement it.
Code
from PySide2.QtWidgets import (QWidget, QApplication, QGraphicsView,
QGridLayout)
from PySide2 import QtCore, QtWidgets, QtGui
from PySide2.QtOpenGL import *
from PySide2.QtCore import *
from PySide2.QtGui import *
image_path_str='image.jpg'
class View(QGraphicsView):
photo_clicked = QtCore.Signal(QtCore.QPoint)
def __init__(self, parent):
super(View, self).__init__()
self.scene = QtWidgets.QGraphicsScene(self)
self.photo = QtWidgets.QGraphicsPixmapItem()
self.scene.addItem(self.photo)
self.pixmap = QtGui.QPixmap(image_path_str)
self.photo.setPixmap(self.pixmap)
self.setScene(self.scene)
self.setDragMode(QGraphicsView.ScrollHandDrag)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.view = View(self)
self.layout_contain_P1_P2 = QtWidgets.QGridLayout()
self.checkbox_P1= QtWidgets.QCheckBox("P1",self)
self.line_edit_P1_x = QtWidgets.QLineEdit(self)
self.line_edit_P1_x.setReadOnly(True)
self.line_edit_P1_y = QtWidgets.QLineEdit(self)
self.line_edit_P1_y.setReadOnly(True)
self.menubar = QtWidgets.QMenuBar()
self.archive = self.menubar.addMenu("archive")
self.menubar.setObjectName("menubar")
self.layout_contain_P1_P2.addWidget(self.checkbox_P1, 0, 0, Qt.AlignLeft)
self.grid_layout_P1_x_y = QtWidgets.QGridLayout()
self.grid_layout_P1_x_y.addWidget(self.line_edit_P1_x, 1, 0, Qt.AlignLeft)
self.grid_layout_P1_x_y.addWidget(self.line_edit_P1_y, 2, 0, Qt.AlignLeft)
self.layout_contain_P1_P2.addLayout(self.grid_layout_P1_x_y, 0, 1, 1, 1)
self.checkbox_P2 = QtWidgets.QCheckBox("P2",self)
self.line_edit_P2_x = QtWidgets.QLineEdit(self)
self.line_edit_P2_x.setReadOnly(True)
self.line_edit_P2_y = QtWidgets.QLineEdit(self)
self.line_edit_P2_y.setReadOnly(True)
self.layout_contain_P1_P2.addWidget(self.checkbox_P2, 1, 0, Qt.AlignLeft)
self.grid_layout_P2_x_y = QtWidgets.QGridLayout()
self.grid_layout_P2_x_y.addWidget(self.line_edit_P2_x, 0, 0, Qt.AlignLeft)
self.grid_layout_P2_x_y.addWidget(self.line_edit_P2_y, 1, 0, Qt.AlignLeft)
self.layout_contain_P1_P2.addLayout(self.grid_layout_P2_x_y, 1, 1, Qt.AlignLeft)
self.combo_box1 = QtWidgets.QComboBox(self)
self.combo_box1.addItem("measurements set 1")
self.combo_box1.addItem("measurements set 1")
self.combo_box2 = QtWidgets.QComboBox(self)
self.combo_box2.addItem("P1-P2")
self.combo_box2.addItem("P3-P4")
self.vertical1= QtWidgets.QVBoxLayout()
self.vertical1.addWidget(self.menubar)
self.vertical1.addWidget(self.combo_box1)
self.vertical1.addWidget(self.combo_box2)
self.vertical1.addLayout(self.layout_contain_P1_P2)
self.vertical2= QtWidgets.QVBoxLayout()
self.vertical2.addWidget(self.view)
self.horizontal= QtWidgets.QHBoxLayout()
self.horizontal.addLayout(self.vertical1)
self.horizontal.addLayout(self.vertical2)
self.setLayout(self.horizontal)
self.setWindowTitle("Image viewer")
self.setGeometry(200, 200, 1000, 800)
app = QApplication.instance()
if app is None:
app = QApplication([])
w = Window()
w.show()
w.raise_()
QApplication.setOverrideCursor(QCursor(Qt.CrossCursor))
app.exec_()
Here is screenshot of what I get:
Edit : not optimal workaround: add a QMainWindow class
Here is not optimal workaround. I had to add an extra class QMainWindow and call the Widget from within the QMainWindow
from PySide2.QtWidgets import (QWidget, QApplication, QGraphicsView,
QGridLayout, QMainWindow, QAction, QMenu)
from PySide2 import QtCore, QtWidgets, QtGui
from PySide2.QtOpenGL import *
from PySide2.QtCore import *
from PySide2.QtGui import *
image_path_str='image.jpg'
class View(QGraphicsView):
photo_clicked = QtCore.Signal(QtCore.QPoint)
def __init__(self, parent):
super(View, self).__init__()
self.scene = QtWidgets.QGraphicsScene(self)
self.photo = QtWidgets.QGraphicsPixmapItem()
self.scene.addItem(self.photo)
self.pixmap = QtGui.QPixmap(image_path_str)
self.photo.setPixmap(self.pixmap)
self.setScene(self.scene)
self.setDragMode(QGraphicsView.ScrollHandDrag)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.view = View(self)
self.layout_contain_P1_P2 = QtWidgets.QGridLayout()
self.checkbox_P1= QtWidgets.QCheckBox("P1",self)
self.line_edit_P1_x = QtWidgets.QLineEdit(self)
self.line_edit_P1_x.setReadOnly(True)
self.line_edit_P1_y = QtWidgets.QLineEdit(self)
self.line_edit_P1_y.setReadOnly(True)
self.layout_contain_P1_P2.addWidget(self.checkbox_P1, 0, 0, Qt.AlignLeft)
self.grid_layout_P1_x_y = QtWidgets.QGridLayout()
self.grid_layout_P1_x_y.addWidget(self.line_edit_P1_x, 1, 0, Qt.AlignLeft)
self.grid_layout_P1_x_y.addWidget(self.line_edit_P1_y, 2, 0, Qt.AlignLeft)
self.layout_contain_P1_P2.addLayout(self.grid_layout_P1_x_y, 0, 1, 1, 1)
self.checkbox_P2 = QtWidgets.QCheckBox("P2",self)
self.line_edit_P2_x = QtWidgets.QLineEdit(self)
self.line_edit_P2_x.setReadOnly(True)
self.line_edit_P2_y = QtWidgets.QLineEdit(self)
self.line_edit_P2_y.setReadOnly(True)
self.layout_contain_P1_P2.addWidget(self.checkbox_P2, 1, 0, Qt.AlignLeft)
self.grid_layout_P2_x_y = QtWidgets.QGridLayout()
self.grid_layout_P2_x_y.addWidget(self.line_edit_P2_x, 0, 0, Qt.AlignLeft)
self.grid_layout_P2_x_y.addWidget(self.line_edit_P2_y, 1, 0, Qt.AlignLeft)
self.layout_contain_P1_P2.addLayout(self.grid_layout_P2_x_y, 1, 1, Qt.AlignLeft)
self.combo_box1 = QtWidgets.QComboBox(self)
self.combo_box1.addItem("measurements set 1")
self.combo_box1.addItem("measurements set 1")
self.combo_box2 = QtWidgets.QComboBox(self)
self.combo_box2.addItem("P1-P2")
self.combo_box2.addItem("P3-P4")
self.vertical1= QtWidgets.QVBoxLayout()
# self.vertical1.addWidget(self.menubar)
self.vertical1.addWidget(self.combo_box1)
self.vertical1.addWidget(self.combo_box2)
self.vertical1.addLayout(self.layout_contain_P1_P2)
self.vertical2= QtWidgets.QVBoxLayout()
self.vertical2.addWidget(self.view)
self.horizontal= QtWidgets.QHBoxLayout()
self.horizontal.addLayout(self.vertical1)
self.horizontal.addLayout(self.vertical2)
self.setLayout(self.horizontal)
self.setWindowTitle("Image viewer")
self.setGeometry(200, 200, 1000, 800)
class Main_window(QMainWindow):
def __init__(self, parent=None):
super(Main_window, self).__init__(parent)
self.window = Window()
self.setCentralWidget(self.window)
self.initUI()
def initUI(self):
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
impMenu = QMenu('Import', self)
impAct = QAction('Import mail', self)
impMenu.addAction(impAct)
newAct = QAction('New', self)
fileMenu.addAction(newAct)
fileMenu.addMenu(impMenu)
app = QApplication.instance()
if app is None:
app = QApplication([])
mw = Main_window()
mw.show()
mw.raise_()
QApplication.setOverrideCursor(QCursor(Qt.CrossCursor))
app.exec_()
Related
Can someone explain to me why the 'triggered' event is not firing when i click my custom Action?
import os, sys
from PySide2 import QtGui, QtWidgets, QtCore
class ActionButton(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ActionButton, self).__init__(parent)
# controls
self.icon = QtWidgets.QToolButton()
self.icon.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
self.icon.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
self.label = QtWidgets.QLabel('Sample long name')
self.label.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
# layout
self.buttonLayout = QtWidgets.QGridLayout()
self.buttonLayout.setContentsMargins(4, 4, 4, 4)
self.buttonLayout.setSpacing(2)
self.buttonLayout.addWidget(self.icon, 0, 0, QtCore.Qt.AlignCenter)
self.buttonLayout.addWidget(self.label, 1, 0, QtCore.Qt.AlignCenter)
self.mainLayout = QtWidgets.QGridLayout()
self.mainLayout.setContentsMargins(0, 0, 0, 0)
self.mainLayout.addLayout(self.buttonLayout, 0, 0, QtCore.Qt.AlignCenter)
self.setLayout(self.mainLayout)
# method
def setText(self, value):
self.label.setText(value)
def setIcon(self, value):
self.icon.setIcon(value)
class Example(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
# controls
self.toolBar = QtWidgets.QToolBar()
# layout
self.mainLayout = QtWidgets.QHBoxLayout()
self.mainLayout.addWidget(self.toolBar)
self.setLayout(self.mainLayout)
# custom
wPreset = ActionButton(self)
wAct = QtWidgets.QWidgetAction(self)
wAct.setDefaultWidget(wPreset)
wAct.triggered.connect(self.testThis)
self.toolBar.addAction(wAct)
def testThis(self):
print('here')
def test():
app = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
pass
test()
I have prepared a little script to illustrate my problem. In fact I would like the button contained in tab 1 (Tab 1) to show me tab 4 (Tab 4). The QTabWidget is contained in the main class (CLASSE_Main) and I'm calling it from class 1 (CLASSE_1). I did some testing but nothing works.
Here is the script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports PyQt5 -----------------------------------------------------------------------
from PyQt5.QtWidgets import (QWidget, QGroupBox, QGridLayout, QVBoxLayout, QMainWindow,
QTabWidget, QWidget, QApplication, QLabel, QPushButton)
# -------------------------------------------------------------------------------------
import sys
class CLASSE_1(QWidget) :
""" """
def __init__(self, parent=None):
super(CLASSE_1, self).__init__(parent)
label_classe_1 = QLabel("I'm class 1 label")
bouton_affichage_tab_4 = QPushButton("setCurrentIndex Tab 4")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_1, 0, 0)
grid_1.addWidget(bouton_affichage_tab_4, 0, 1)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
# Signal
bouton_affichage_tab_4.clicked.connect(self.show_tab_4)
def show_tab_4(self) :
""" """
from . import CLASSE_Main
cp = CLASSE_Main()
print("dir(cp)", dir(cp))
#cp.tab_widget.setCurrentIndex(3)
print("cp.get_tabwidget(), type(cp.get_tabwidget())", cp.get_tabwidget(), type(cp.get_tabwidget()))
# --------------------------------------
cp.get_tabwidget().setCurrentIndex(3)
# --------------------------------------
for cle, valeur in cp.__dict__.items():
if cle == 'tab_widget' :
print("valeur :", type(valeur))
# --------------------------------------
valeur.setCurrentIndex(3)
# --------------------------------------
class CLASSE_2(QWidget) :
""" """
def __init__(self, parent):
super(CLASSE_2, self).__init__(parent)
label_classe_2 = QLabel("I'm class 2 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_2, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_3(QWidget) :
""" """
def __init__(self, parent=None):
super(CLASSE_3, self).__init__(parent)
label_classe_3 = QLabel("I'm class 3 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_3, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_4(QWidget) :
""" """
def __init__(self, parent):
super(CLASSE_4, self).__init__(parent)
label_classe_4 = QLabel("I'm class 4 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_4, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_5(QWidget) :
""" """
def __init__(self, parent=None):
super(CLASSE_5, self).__init__(parent)
label_classe_5 = QLabel("I'm class 5 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_5, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_6(QWidget) :
""" """
def __init__(self, parent):
super(CLASSE_6, self).__init__(parent)
label_classe_6 = QLabel("I'm class 6 label")
groupe_1 = QGroupBox()
grid_1 = QGridLayout()
grid_1.addWidget(label_classe_6, 0, 0)
groupe_1.setLayout(grid_1)
layout = QGridLayout()
layout.addWidget(groupe_1, 0, 0)
self.setLayout(layout)
class CLASSE_Main(QMainWindow):
""" Main class """
def __init__(self):
super(CLASSE_Main, self).__init__()
self.setWindowTitle('Help me please !')
self.setGeometry(20, 40, 600, 400)
self.setMinimumSize(600, 460)
self.tab_widget = QTabWidget()
self.win_widget_1 = CLASSE_1(self)
self.win_widget_2 = CLASSE_2(self)
self.win_widget_3 = CLASSE_3(self)
self.win_widget_4 = CLASSE_4(self)
self.win_widget_5 = CLASSE_5(self)
self.win_widget_6 = CLASSE_6(self)
widget = QWidget()
layout = QVBoxLayout(widget)
self.tab_widget.addTab(self.win_widget_1, "Tab 1")
self.tab_widget.addTab(self.win_widget_2, "Tab 2")
self.tab_widget.addTab(self.win_widget_3, "Tab 3")
self.tab_widget.addTab(self.win_widget_4, "Tab 4")
self.tab_widget.addTab(self.win_widget_5, "Tab 5")
self.tab_widget.addTab(self.win_widget_6, "Tab 6")
self.tab_widget.setStyleSheet("""QTabWidget::tab-bar {alignment: center;}""")
layout.addWidget(self.tab_widget)
self.setCentralWidget(widget)
def get_tabwidget(self) :
""" """
return self.tab_widget
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = CLASSE_Main()
ex.show()
sys.exit(app.exec_())
Explanation:
It is not necessary to import a class defined in the same script so remove from . import CLASSE_Main.
On the other hand, although the code no longer throws the exception, you have another problem: "cp" is not "ex" since they are 2 different objects.
Solution:
One possible solution is to create a signal that sends the information of the new index that you want to show in the QTabWidget and link that signal to the setCurrentIndex method of the correct QTabWidget:
from PyQt5.QtCore import pyqtSignal
class CLASSE_1(QWidget):
customSignal = pyqtSignal(int)
# ...
def show_tab_4(self):
self.customSignal.emit(3)
class CLASSE_Main(QMainWindow):
""" Main class """
def __init__(self):
super(CLASSE_Main, self).__init__()
# ...
self.setCentralWidget(widget)
self.win_widget_1.customSignal.connect(self.tab_widget.setCurrentIndex)
I'm having an issue combining PyQt4 scrollbars (on the MainWindow) with embedded scenes - everything works fine until I resize my window, after which my scenes begin to travel with the scrollbar off the page.
Below is the simplified, full code to illustrate this behavior (with two screenshots attached afterward to explicitly showcase this interaction):
import sys, os
from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
from traits.api import HasTraits,Instance,on_trait_change
from traitsui.api import View,Item
from mayavi import mlab
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
class Mayavi_Scene(HasTraits):
scene = Instance(MlabSceneModel, ())
#on_trait_change('scene.activated')
def update_scene(self):
Mayavi_Scene.fig1 = mlab.figure(1, bgcolor=(.5,.5,.5))
self.scene.mlab.clf(figure=Mayavi_Scene.fig1)
testPlot = mlab.test_contour3d()
view = View(Item('scene', editor = SceneEditor(scene_class=MayaviScene),
height=300, width=300, show_label=False),
resizable=True,
)
class P1(QtGui.QWidget):
def __init__(self, parent=None):
super(P1, self).__init__(parent)
layout = QtGui.QGridLayout(self)
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(10)
self.label_edge1 = QtGui.QLabel('')
self.label_edge1.setMargin(5)
self.label_edge1.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Sunken)
layout.addWidget(self.label_edge1, 0, 0, 10, 10)
self.label_edge1.show()
self.label_avgVol = QtGui.QLabel('Test')
self.label_avgVol.setMargin(5)
self.label_avgVol.setFrameStyle(QtGui.QFrame.Box | QtGui.QFrame.Sunken)
self.label_avgVol.setAlignment(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter)
layout.addWidget(self.label_avgVol, 0, 0, 1, 10)
self.label_avgVol.show()
self.viz1 = Mayavi_Scene()
self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
layout.addWidget(self.ui1, 1, 1, 1, 9)
class P2(QtGui.QWidget):
def __init__(self, parent=None):
super(P2, self).__init__(parent)
layout = QtGui.QGridLayout(self)
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(10)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(50, 50, 500, 500)
tab1 = P1(self)
tab2 = P2(self)
self.tabs = QtGui.QTabWidget(self)
self.tabs.resize(250,150)
self.tabs.addTab(tab1, 'Page 1')
self.tabs.addTab(tab2, 'Page 2')
self.setWindowTitle('SCROLLBAR ERROR EXAMPLE')
self.groupscroll = QtGui.QHBoxLayout()
self.groupscrollbox = QtGui.QGroupBox()
self.MVB = QtGui.QVBoxLayout()
self.MVB.addWidget(self.tabs)
scroll = QtGui.QScrollArea()
widget = QtGui.QWidget(self)
widget.setLayout(QtGui.QHBoxLayout())
widget.layout().addWidget(self.groupscrollbox)
scroll.setWidget(widget)
scroll.setWidgetResizable(True)
self.groupscrollbox.setLayout(self.MVB)
self.groupscroll.addWidget(scroll)
self.setCentralWidget(scroll)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication.instance()
w = MainWindow()
sys.exit(app.exec_())
Before Reframing
After Reframing
Edit: .gif to show effect (notice how object gets covered up by its own frame containing it at the end instead of moving with it; it's like the 3d scene object isn't being notified that everything else around it is changing):
I modified the place where the scroll bars are added.
import sys, os
from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
from traits.api import HasTraits,Instance,on_trait_change
from traitsui.api import View,Item
from mayavi import mlab
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
class Mayavi_Scene(HasTraits):
scene = Instance(MlabSceneModel, ())
#on_trait_change('scene.activated')
def update_scene(self):
Mayavi_Scene.fig1 = mlab.figure(1, bgcolor=(.5,.5,.5))
self.scene.mlab.clf(figure=Mayavi_Scene.fig1)
testPlot = mlab.test_contour3d()
view = View(Item('scene', editor = SceneEditor(scene_class=MayaviScene),
height=300, width=300, show_label=False),
resizable=True,
)
class P1(QtGui.QWidget):
def __init__(self, parent=None):
super(P1, self).__init__(parent)
layout = QtGui.QGridLayout(self)
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(10)
self.label_edge1 = QtGui.QLabel('')
self.label_edge1.setMargin(5)
self.label_edge1.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Sunken)
layout.addWidget(self.label_edge1, 0, 0, 10, 10)
self.label_edge1.show()
self.label_avgVol = QtGui.QLabel('Test')
self.label_avgVol.setMargin(5)
self.label_avgVol.setFrameStyle(QtGui.QFrame.Box | QtGui.QFrame.Sunken)
self.label_avgVol.setAlignment(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter)
layout.addWidget(self.label_avgVol, 0, 0, 1, 10)
self.label_avgVol.show()
self.groupscroll = QtGui.QHBoxLayout()
self.groupscrollbox = QtGui.QGroupBox()
self.viz1 = Mayavi_Scene()
self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
self.MVB = QtGui.QVBoxLayout()
self.MVB.addWidget(self.ui1)
scroll = QtGui.QScrollArea()
widget = QtGui.QWidget(self)
widget.setLayout(QtGui.QHBoxLayout())
widget.layout().addWidget(self.groupscrollbox)
scroll.setWidget(widget)
scroll.setWidgetResizable(True)
self.groupscrollbox.setLayout(self.MVB)
self.groupscroll.addWidget(scroll)
layout.addWidget(scroll, 1, 1, 1, 9)
class P2(QtGui.QWidget):
def __init__(self, parent=None):
super(P2, self).__init__(parent)
layout = QtGui.QGridLayout(self)
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(10)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(50, 50, 500, 500)
tab1 = P1(self)
tab2 = P2(self)
self.tabs = QtGui.QTabWidget(self)
self.tabs.resize(250,150)
self.tabs.addTab(tab1, 'Page 1')
self.tabs.addTab(tab2, 'Page 2')
self.setWindowTitle('SCROLLBAR ERROR EXAMPLE')
self.setCentralWidget(self.tabs)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication.instance()
w = MainWindow()
sys.exit(app.exec_())
In that condition, the scroll bars are inside the tab and not ouside. Then you get something like :
But I'm not sure if this is what you wanted.
I don't know why but my 2 custom widgets(SquareCalc, LinePainting) when i use them in QXBoxLayout(twoWidgets) , there are being superimposed each other one.
I'm using python 3.5.2 with PyQt5
This is my code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPainter, QPen
from PyQt5.QtWidgets import (QApplication, QWidget,
QGridLayout, QVBoxLayout, QHBoxLayout,QLabel, QLineEdit, QPushButton)
class SquareCalc(QWidget):
def __init__(self):
super().__init__()
def initUI(self):
self.setGeometry(0,0,100,100)
self.inputLine = QLineEdit()
self.outputLine = QLineEdit()
self.outputLine.setReadOnly(True)
self.inputLine.returnPressed.connect(self.calc)
self.calcButton = QPushButton("&Calc")
self.calcButton.clicked.connect(self.calc)
lineLayout = QGridLayout()
lineLayout.addWidget(QLabel("num"), 0, 0)
lineLayout.addWidget(self.inputLine, 0, 1)
lineLayout.addWidget(QLabel("result"), 1, 0)
lineLayout.addWidget(self.outputLine, 1, 1)
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(self.calcButton)
mainLayout = QVBoxLayout()
mainLayout.addLayout(lineLayout)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
def calc(self):
n = int(self.inputLine.text())
r = n**2
self.outputLine.setText(str(r))
class LinePainting(QWidget):
def __init__(self):
super().__init__()
def initPainting(self):
self.setGeometry(0, 0, 300, 300)
self.setWindowTitle('Pen styles')
mainLayout = QVBoxLayout()
mainLayout.addWidget(self)
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawLines(qp)
qp.end()
def drawLines(self, qp):
pen = QPen(Qt.black, 2, Qt.SolidLine)
qp.setPen(pen)
qp.drawLine(20, 40, 250, 40)
pen.setStyle(Qt.DashLine)
qp.setPen(pen)
qp.drawLine(20, 80, 250, 80)
pen.setStyle(Qt.DashDotLine)
qp.setPen(pen)
qp.drawLine(20, 120, 250, 120)
pen.setStyle(Qt.DotLine)
qp.setPen(pen)
qp.drawLine(20, 160, 250, 160)
pen.setStyle(Qt.DashDotDotLine)
qp.setPen(pen)
qp.drawLine(20, 200, 250, 200)
pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)
qp.drawLine(20, 240, 250, 240)
class twoWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.widget1 = SquareCalc()
self.widget2 = LinePainting()
mainLayout = QHBoxLayout()
mainLayout.addWidget(self.widget1)
mainLayout.addWidget(self.widget2)
self.setLayout(mainLayout)
self.setWindowTitle("Mix line/factorial")
self.widget2.initPainting()
self.widget1.initUI()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = twoWidget()
main_window.setStyleSheet(open("lol.qss", "r").read())
main_window.show()
sys.exit(app.exec_())
I find the problem , i just need no put a fixed size to my widget SquareCalc,
just add
self.setFixedSize(sizeX,sizeY)
In replace of :
self.setGeometry(0, 0, 300, 300)
Just because QPainter no need to have a minimum size , on the contrary QLineEdit() and QPushButton(), yes
I have this code:
#!/usr/bin/env python3
from PyQt5.QtWidgets import *
import sys
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)
label_1 = QLabel("label 1")
layout.addWidget(label_1, 0, 0)
label_2 = QLabel("label 2")
layout.addWidget(label_2, 0, 1)
label_3 = QLabel("label 3")
layout.addWidget(label_3, 1, 0)
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())
I have this result:
but I need this:
How can I do it?
The fourth and fifth arguments of addWidget allow you to specify how many rows and columns to span:
label_3 = QLabel("label 3")
layout.addWidget(label_3, 1, 0, 1, 2)
This is the example code for layout the QLabel. It is PyQt4, but you can try with PyQt5 with small changes.
import sys
from PyQt4 import QtGui
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.verticalLayout = QtGui.QVBoxLayout (self)
self.verticalLayout.setObjectName ('verticalLayout')
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setObjectName ('gridLayout')
self.label_1 = QtGui.QLabel(self)
self.label_1.setObjectName('label_1')
self.label_1.setText ('Label_1')
self.label_1.setStyleSheet('background-color: rgb(182, 182, 182);')
self.label_2 = QtGui.QLabel(self)
self.label_2.setObjectName('label_2')
self.label_2.setText ('Label_2')
self.label_2.setStyleSheet('background-color: rgb(182, 182, 182);')
self.label_3 = QtGui.QLabel(self)
self.label_3.setObjectName('label_3')
self.label_3.setText ('Label_3')
self.label_3.setStyleSheet('background-color: rgb(182, 182, 182);')
self.gridLayout.addWidget(self.label_1, 0, 0, 1, 1)
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.gridLayout.addWidget(self.label_3, 1, 0, 1, 2)
self.verticalLayout.addLayout(self.gridLayout)
self.resize(300, 100)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())