Mdi sub window close - python

I would like to close the MDI subwindow with a push of a button within the window instead of closing it with the x at the top. I have another file with a few classes that has all of the information about the window that is opening up in the MDI area. I have tried self.close() but that leaves the window open and clears all of the widgets from the window. I will post the code below for how I am adding the subwindow to the MDI area.
subWindow = QtWidgets.QMdiSubWindow()
New_Window = NewMDIWindow()
subWindow.setWidget(New_Window)
subWindow.setObjectName("New_Window")
subWindow.setWindowTitle("New SubWindow")
self.MainUi.mdiArea.addSubWindow(subWindow )
subWindow.show()

The X button closes the QMdiSubWindow, not the widget inscribed on it, so the button should close the subwindow:
your_QPushButton.clicked.connect(your_QMdiSubWindow.close)
Complete Example:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.centralwidget = QtWidgets.QWidget(self)
self.setCentralWidget(self.centralwidget)
self.centralwidget.setLayout(QtWidgets.QVBoxLayout(self.centralwidget))
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.centralwidget.layout().addWidget(self.mdiArea)
subWindow = QtWidgets.QMdiSubWindow(self)
widget = QtWidgets.QWidget()
widget.setLayout(QtWidgets.QVBoxLayout())
btn = QtWidgets.QPushButton("close", widget)
widget.layout().addWidget(btn)
btn.clicked.connect(subWindow.close)
subWindow.setWidget(widget)
subWindow.setObjectName("New_Window")
subWindow.setWindowTitle("New SubWindow")
self.mdiArea.addSubWindow(subWindow)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Related

Show close button (x) after hiding it (QTabBar)

I'm creating a method to hide and show the close button of a tab. I found a way to hide it. However, I don't know how to do it in reverse.
This is my existing code for hiding the close button. Using the same lines of codes, how can I show the close button of the tab?
def disable_close_button(self):
self.ui.tab_widget.tabBar().setTabButton(self.current_index(), QTabBar.RightSide, None)
def enable_close_button(self):
pass
Thanks in advance!
You are not hiding the button, you are eliminating it. So in my solution I get the button and then I hide it or show it as needed.
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
show_button = QtWidgets.QPushButton(
text="show",
clicked=self.enable_close_button
)
hide_button = QtWidgets.QPushButton(
text="hide",
clicked=self.disable_close_button
)
self.tab_widget = QtWidgets.QTabWidget(tabsClosable=True)
for i in range(4):
label = QtWidgets.QLabel(
text="label {}".format(i),
alignment=QtCore.Qt.AlignCenter
)
self.tab_widget.addTab(label , "tab-{}".format(i))
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(show_button)
lay.addWidget(hide_button)
lay.addWidget(self.tab_widget)
#QtCore.pyqtSlot()
def enable_close_button(self):
ix = self.tab_widget.currentIndex()
button = self.tab_widget.tabBar().tabButton(ix, QtWidgets.QTabBar.RightSide)
if button is not None:
button.show()
#QtCore.pyqtSlot()
def disable_close_button(self):
ix = self.tab_widget.currentIndex()
button = self.tab_widget.tabBar().tabButton(ix, QtWidgets.QTabBar.RightSide)
if button is not None:
button.hide()
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

Close and Open new Window PYQT5

I would like to press a button in a window and close that window,after that open a new window
How can I do it?
I already tried it but it sends this message the console:
QCoreApplication::exec: The event loop is already running
class Window(QWidget):
def __init__(self,parent = None):
super().__init__(parent)
self.title = 'pySim Z-eighty'
self.left = 0
self.top = 0
self.width = 1200
self.height = 3000
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.button = QPushButton("Z80")
self.button1 = QPushButton()
self.button2 = QPushButton()
self.container = QWidget()
self.layout = QGridLayout()
self.layout.addWidget(self.button1, 1, 0)
self.layout.addWidget(self.button, 1, 1)
self.layout.addWidget(self.button2, 1, 2)
self.container.setLayout(self.layout)
self.layoutPrincipal = QBoxLayout(0)
self.layoutPrincipal.addWidget(self.container)
self.setLayout(self.layoutPrincipal)
self.button.pressed.connect(self.IniciarInterfaz)
def IniciarInterfaz(self):
self.hide()
app = QApplication(sys.argv)
ex = mainWindow()
ex.setStyleSheet("background-color: #fff")
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Window()
ex.show()
sys.exit(app.exec_())
My main problem is when i pressed the button I can't open the new window
There can only be one QApplication within the PyQt application, so if you already created it, do not do it again.
Another problem is that the variables exist only within the context, in your case mainWindow, so at the end of the function StartInterface will eliminate this variable and the window, the solution is to make the mainWindow member of the class, so the context will be the class and no longer the function, so it will stay correctly.
def IniciarInterfaz(self):
self.hide()
self.ex = mainWindow()
self.ex.setStyleSheet("background-color: #fff")
self.ex.show()
PYQT No open and close method,...
hide() and show() method you can use buttons what ever you want,...
def PlatformType_Clicked(self):
dialog.hide()
dialog1.show()

pyqt4 already has a layout. How to 'detect' it or change?

I'm trying to set a layout manager. But getting the message:
QLayout: Attempting to add QLayout "" to Window "", which already has a layout
How can I change or detect which type the layout is? I'd like to use the boxlayout as it seems to be prefered.
import sys
from PyQt4 import QtGui as qt
class Window(qt.QMainWindow):
def __init__(self):
super(Window, self).__init__()
#Lav widgets
self.CreateWidgets()
def CreateWidgets(self):
btn = qt.QPushButton("Fetch", self)
btn.clicked.connect(self.GetData)
self.layout = qt.QVBoxLayout(self)
self.setGeometry(560, 240, 800, 600)
self.setWindowTitle("We do not sow")
self.show()
def GetData(self):
print("Hello World!")
app = qt.QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())
The QMainWindow class has built-in support for toolbars and dock-widgets, and a menubar and statusbar - so it has to have a fixed layout. Therefore, rather than adding child widgets to the main window itself, you must set its central widget, and then add the child widgets to that:
def CreateWidgets(self):
btn = qt.QPushButton("Fetch", self)
btn.clicked.connect(self.GetData)
widget = qt.QWidget(self)
layout = qt.QVBoxLayout(widget)
layout.addWidget(btn)
self.setCentralWidget(widget)
self.setGeometry(560, 240, 800, 600)
self.setWindowTitle("We do not sow")

PyQt add widget to second window

I've got the following python code which opens a second window. I can't figure out how to add a label or pushbutton to this second window. I thought it would be easy but nothing I try seems to work. Thanks!
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
menu = self.menuBar().addMenu(self.tr('View'))
action = menu.addAction(self.tr('New Window'))
action.triggered.connect(self.handleNewWindow)
def handleNewWindow(self):
window = QtGui.QMainWindow(self)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.setWindowTitle(self.tr('New Window'))
window.show()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(300, 300)
window.show()
sys.exit(app.exec_())
If the two windows are different, it makes more sense to create two class.
I guess the second one doesn't need to be a QMainWindow (= it doesn't need a menu and a toolbar and a status bar etc), so let's just make it a QWidget.
class SecondWindow(QtGui.QWidget):
def __init__(self,parent):
QtGui.QWidget.__init__(self,parent)
self.button=QtGui.QPushButton("my button !")
layout=QtGui.QHBoxLayout()
layout.addWidget(self.button)
self.setLayout(layout)
self.show()
In your main window, you cretae and instance of the class SecondWindow:
class FirstWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
...
self.show()
def handleNewWindow(self):
self.childWindow = SecondWindow(self)
If you just want a TopLevel window, using QtGui.QDialog seems to be more appropriate. To add button and label, you can do something like this:
def handleNewWindow(self):
window = QtGui.QMainWindow(self)
window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
window.setWindowTitle(self.tr('New Window'))
button = QtGui.QPushButton("MY BUTTON!") #create button
label = QtGui.QLabel("MY LABEL!") # create label
CentralWidget = QtGui.QWidget() # create an empty widget
CentralWidgetLayout = QtGui.QHBoxLayout() # create a layout
CentralWidgetLayout.addWidget(label) # add your label to the layout
CentralWidgetLayout.addWidget(button) # add your button to the layout
CentralWidget.setLayout(CentralWidgetLayout) # assign your layout to the empty widget
window.setCentralWidget(CentralWidget) #make the assigned widget CentralWidget
window.show()

PyQt QScrollArea within QScrollArea

I am trying to use multiple horizontal sub QScrollAreas with text and one vertical container QScrollArea. The idea being that the text area in the horizontal sub QScrollAreas will always have equivalent vertical heights and I would like to have one vertical QScrollArea to control the data within them.
The code below shows that the horizontal sub QScrollAreas work, but the vertical QScrollArea doesn't detect that the line edits within the widget inside it don't fit vertically. If I change
scroll.setWidgetResizable(True)
for the vertical QScrollArea to False, the vertical QScrollArea detects the widget inside doesn't fit but I want to be able to scroll all the lineEdits up and down not the parent widget. Also I would like all scrollbars to be always visible. Is this possible?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent= None):
super(Widget, self).__init__()
self.setGeometry(100, 100, 400, 400)
baseWidget = QWidget()
hBox = QHBoxLayout()
hBox.addWidget(self.getWidget())
hBox.addWidget(self.getWidget())
baseWidget.setLayout(hBox)
scroll = QScrollArea()
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
#when set to False all scrolls are not visible and can only scroll parent widget not the data areas
scroll.setWidgetResizable(True)
scroll.setWidget(baseWidget)
vBox = QHBoxLayout()
vBox.addWidget(scroll)
self.setLayout(vBox)
def getWidget(self):
widget = QWidget()
layout = QVBoxLayout()
for i in range(20):
lineEdit = QLineEdit("row: "+str(i)+" data: "+str(list(range(10))))
lineEdit.setMinimumWidth(250)
layout.addWidget(lineEdit)
widget.setLayout(layout)
scroll = QScrollArea()
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll.setWidgetResizable(False)
scroll.setWidget(widget)
return scroll
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Widget()
dialog.show()
The answer could be found here:
PyQt4 : is there any signal related to scrollbar?
Just needed to sync vertical scrollbars and hide all but one:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent= None):
super(Widget, self).__init__()
self.setGeometry(100, 100, 200, 200)
baseWidget = QWidget()
hBox = QHBoxLayout()
lscrollArea = self.getWidget(False)
rScrollArea = self.getWidget(True)
rScrollArea.verticalScrollBar().valueChanged.connect(
lscrollArea.verticalScrollBar().setValue)
hBox.addWidget(lscrollArea)
hBox.addWidget(rScrollArea)
baseWidget.setLayout(hBox)
vBox = QHBoxLayout()
vBox.addWidget(baseWidget)
self.setLayout(vBox)
def getWidget(self, vScrollOn):
widget = QWidget()
layout = QVBoxLayout()
for i in range(20):
lineEdit = QLineEdit("row: "+str(i)+" data: "+str(list(range(10))))
lineEdit.setMinimumWidth(250)
layout.addWidget(lineEdit)
widget.setLayout(layout)
scroll = QScrollArea()
if vScrollOn:
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
else:
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll.setWidgetResizable(False)
scroll.setWidget(widget)
return scroll
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Widget()
dialog.show()
app.exec_()

Categories