QWidgetAction not triggering - python

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

Related

PyQT: Exit/Complete Second Window Before First Window

I have the following code below:
from PyQt4 import QtGui
import sys
class Second(QtGui.QWidget):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.grid = QtGui.QGridLayout(self)
self.setGeometry(650,400,400,200)
self.widget = QtGui.QWidget()
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.grid = QtGui.QGridLayout(self)
self.setGeometry(350, 200, 1000, 700)
self.widget = QtGui.QWidget()
Button1 = QtGui.QPushButton('...', self)
Button1.clicked.connect(self.on_pushButton_clicked)
self.grid.addWidget(Button1, 0, 0, 1, 1)
def on_pushButton_clicked(self):
self.Second = Second()
self.Second.setWindowTitle('Window')
self.Second.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
When I click the button, I want to be able to finish up my action in the second window before continuing on the first. Right now, I can exit out of my first window and the second window remains open. How do you keep on the second window but keep the first window unselectable?
There are 2 possible solutions:
- Second must inherit from QDialog, pass it as parent to the first window and use exec_() instead of show:
class Second(QtGui.QDialog):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setGeometry(350, 200, 1000, 700)
self.widget = QtGui.QWidget()
self.setCentralWidget(self.widget)
grid = QtGui.QGridLayout(self.widget)
Button1 = QtGui.QPushButton('...', self)
Button1.clicked.connect(self.on_pushButton_clicked)
grid.addWidget(Button1, 0, 0, 1, 1)
#QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self.Second = Second(self)
self.Second.setWindowTitle('Window')
self.Second.exec_()
- Change the windowModality to Qt::WindowModal, activate the flag Qt::Dialog and pass it the first window as parent.
class Second(QtGui.QWidget):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setWindowModality(QtCore.Qt.WindowModal)
self.setWindowFlags(self.windowFlags() | QtCore.Qt.Dialog)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setGeometry(350, 200, 1000, 700)
self.widget = QtGui.QWidget()
self.setCentralWidget(self.widget)
grid = QtGui.QGridLayout(self.widget)
Button1 = QtGui.QPushButton('...', self)
Button1.clicked.connect(self.on_pushButton_clicked)
grid.addWidget(Button1, 0, 0, 1, 1)
#QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self.Second = Second(self)
self.Second.setWindowTitle('Window')
self.Second.show()

Add a QMenuBar to a QWidget

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

Graphics in PyQtGraph not updating after zoom

I have the following PyQtGraph program, which makes a red square "move" when moving a slider:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QSlider
from pyqtgraph import (
mkBrush,
mkPen,
GraphicsObject,
QtGui,
PlotWidget,
)
class SquareItem(GraphicsObject):
def __init__(self):
super().__init__()
self.position_picture = QtGui.QPicture()
def paint(self, p, *args):
p.drawPicture(0, 0, self.position_picture)
def boundingRect(self):
return QtCore.QRectF(-5, -5, 20, 10)
def update_position(self, x):
self.position_picture = QtGui.QPicture()
painter = QtGui.QPainter(self.position_picture)
painter.scale(1, -1)
painter.setBrush(mkBrush('r'))
painter.setPen(mkPen(None))
painter.drawRect(QtCore.QRectF(x, 0, 1, 1))
painter.end()
self.informViewBoundsChanged()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = SquareItem()
self.graphics.addItem(self.item)
self.slider = QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QSlider.TicksAbove)
self.slider.valueChanged.connect(self.slider_value_changed)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
def slider_value_changed(self, value):
self.item.update_position(value)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
Everything seems to work fine, but if I zoom in/out and then move the slider again the square position is no longer updated (i.e.: the square is not re-drawn).
How can I fix that?
Updates
I am using a square to simplify the problem. In reality, I do not only change position, but I can also draw different shapes, so using setPos() is not really an option.
You should not update the painting if you want to change position, you should only use setPos(). the paint() function takes boundingRect() as a reference so when moving the graph you are moving it in that coordinate system instead of the coordinate system of PlotWidget.
class SquareItem(GraphicsObject):
def paint(self, p, *args):
p.setBrush(mkBrush('r'))
p.setPen(mkPen(None))
p.drawRect(self.boundingRect())
def boundingRect(self):
return QtCore.QRectF(0, 0, 1, 1)
def update_position(self, x):
self.setPos(x, 0)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = SquareItem()
self.graphics.addItem(self.item)
self.graphics.setRange(rect=QtCore.QRectF(-10, -10, 20, 20))
self.slider = QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QSlider.TicksAbove)
self.slider.valueChanged.connect(self.slider_value_changed)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
def slider_value_changed(self, value):
self.item.update_position(value)
If you are not going to use signals it is advisable to use objects that inherit from QGraphicsItem instead of QGraphicsObject, for example you could use QGraphicsRectItem:
import sys
from PyQt5 import QtCore, QtWidgets
from pyqtgraph import (
mkBrush,
mkPen,
GraphicsObject,
QtGui,
PlotWidget,
)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = QtWidgets.QGraphicsRectItem(0, 0, 1, 1)
self.item.setBrush(mkBrush('r'))
self.item.setPen(mkPen(None))
self.graphics.addItem(self.item)
self.graphics.setRange(rect=QtCore.QRectF(-10, -10, 20, 20))
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QtWidgets.QSlider.TicksAbove)
self.slider.valueChanged.connect(self.slider_value_changed)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
def slider_value_changed(self, value):
self.item.setPos(value, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
update:
If you want to redraw you should call update():
import sys
from PyQt5 import QtCore, QtWidgets
from pyqtgraph import (
mkBrush,
mkPen,
GraphicsObject,
QtGui,
PlotWidget,
)
class SquareItem(GraphicsObject):
colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'w', 'FF0', 'AA0', '0AA']
def __init__(self):
super().__init__()
self.mColor = SquareItem.colors[0]
def paint(self, p, *args):
p.setBrush(mkBrush(self.mColor))
p.setPen(mkPen(None))
p.drawRect(self.boundingRect())
def boundingRect(self):
return QtCore.QRectF(0, 0, 1, 1)
def update_draw(self, x):
self.mColor = SquareItem.colors[x]
self.update()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Micromouse maze simulator')
self.resize(600, 600)
frame = QtWidgets.QFrame()
layout = QtWidgets.QVBoxLayout(frame)
self.graphics = PlotWidget()
self.graphics.setAspectLocked()
self.item = SquareItem()
self.graphics.addItem(self.item)
self.graphics.setRange(rect=QtCore.QRectF(-10, -10, 20, 20))
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.slider.setSingleStep(1)
self.slider.setPageStep(10)
self.slider.setRange(0, 10)
self.slider.setTickPosition(QtWidgets.QSlider.TicksAbove)
self.slider.valueChanged.connect(self.item.update_draw)
self.slider.setValue(1)
layout.addWidget(self.graphics)
layout.addWidget(self.slider)
self.setCentralWidget(frame)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

Updating pyqt widget content from another widget

Moving on from my last question, I'm stuck once again. I'm trying to update content of parent widget from child widget. The code seems to work first time but after closing and re-opening the form widget it does not update the parent widget.
Following is the code.
from PyQt4 import QtGui, QtCore
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.main_widget()
def main_widget(self):
self.form = Form()
self.simple = Simple()
grid = QtGui.QGridLayout()
self.last_input_label = QtGui.QLabel("")
grid.addWidget(self.last_input_label, 1, 0, 3, 1)
show_form_button = QtGui.QPushButton("Show Form")
show_form_button.clicked.connect(partial(self.form.show_form, self.last_input_label))
grid.addWidget(show_form_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(self.simple.show_simple)
grid.addWidget(show_simple_button, 0, 1)
another_button = QtGui.QPushButton("Print Hello")
another_button.clicked.connect(partial(print, "Hello"))
grid.addWidget(another_button, 0, 2)
self.setLayout(grid)
self.setWindowTitle("Main Widget")
self.show()
def closeEvent(self, QCloseEvent):
QtGui.QApplication.closeAllWindows()
class Form(QtGui.QWidget):
def __init__(self):
print("form initialized")
super(Form, self).__init__()
def show_form(self, last_input_label):
print("form called")
grid = QtGui.QGridLayout()
self.last_input_label = last_input_label
label = QtGui.QLabel("Name")
grid.addWidget(label, 0, 0)
self.line_edit = QtGui.QLineEdit()
grid.addWidget(self.line_edit, 0, 1)
self.submit_button = QtGui.QPushButton("Submit")
self.submit_button.clicked.connect(self.print_form_data)
grid.addWidget(self.submit_button, 1, 1)
self.setLayout(grid)
self.setGeometry(250, 300, 250, 150)
self.setWindowTitle("Form Widget")
self.show()
def get_form_data(self):
form_data = {
"name": self.line_edit.text()
}
return form_data
def print_form_data(self):
self.x = self.get_form_data()
for item in self.x:
print(item + ": " + self.x[item])
self.last_input_label.setText(self.x[item])
return
class Simple(QtGui.QDialog):
def __init__(self):
print("simple initialized")
super(Simple, self).__init__()
def show_simple(self):
print("simple called")
self.setGeometry(300, 250, 250, 150)
self.setWindowTitle("Simple Widget")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_widget = MainWidget()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Please Help!
You're calling the initialization code each time you show the widget. Move all that into the __init__ method where it belongs and it all works.
Move everything beside this into the init method. I can't say exactly why running the init code more would break the connection. But somehow it does. Maybe someone else can fill in that detail.
def show_form(self, last_input_label):
print("form called")
self.last_input_label = last_input_label
self.show()

Simple PyQt4 showing other windows

I'm trying to make a simple widget with three buttons where for each button when clicked will show the corresponding other widgets.
Following is the code I'm trying to run but cant figure out why the new widgets are not showing.
from PyQt4 import QtGui, QtCore
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.main_widget()
def main_widget(self):
another = Another()
simple = Simple()
grid = QtGui.QGridLayout()
show_another_button = QtGui.QPushButton("Show Another")
show_another_button.clicked.connect(another.show_another)
grid.addWidget(show_another_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(simple.show_simple)
grid.addWidget(show_simple_button, 0, 1)
print_button = QtGui.QPushButton("Print Hello")
print_button.clicked.connect(partial(print, "Hello"))
grid.addWidget(another_button, 0, 2)
self.setLayout(grid)
self.show()
class Another(QtGui.QWidget):
def __init__(self):
print("another initialized")
super(Another, self).__init__()
def show_another(self):
print("another called")
grid = QtGui.QGridLayout()
self.setLayout(grid)
self.show()
class Simple(QtGui.QDialog):
def __init__(self):
print("simple initialized")
super(Simple, self).__init__()
def show_simple(self):
print("simple called")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_widget = MainWidget()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Please Help!
Modified your code slightly. The below works on my system.
from PyQt4 import QtGui
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.another = Another()
self.simple = Simple()
self.grid = QtGui.QGridLayout()
self.main_widget()
def main_widget(self):
show_another_button = QtGui.QPushButton("Show Another")
show_another_button.clicked.connect(self.another.show_form)
self.grid.addWidget(show_another_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(self.simple.show_simple)
self.grid.addWidget(show_simple_button, 0, 1)
another_button = QtGui.QPushButton("Print Hello")
another_button.clicked.connect(partial(print, "Hello"))
self.grid.addWidget(another_button, 0, 2)
self.setLayout(self.grid)
class Another(QtGui.QWidget):
def __init__(self):
super(Another, self).__init__()
print("another initialized")
def show_form(self):
print("another called")
grid = QtGui.QGridLayout()
self.setLayout(grid)
self.show()
class Simple(QtGui.QDialog):
def __init__(self):
super(Simple, self).__init__()
print("simple initialized")
def show_simple(self):
print("simple called")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWidget()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Please note: If you try to launch Another() when the Widget is still open you will get an error:
QWidget::setLayout: Attempting to set QLayout "" on Another "", which already has a layout

Categories