PyQt5: Closeable tabs in QTabWidget? [duplicate] - python

This question already has an answer here:
connect function in pyqt5 does not work
(1 answer)
Closed 2 years ago.
I have been trying to implement QTabWidget section in my application, and would like to be able to close the tabs if needed.
I read that I need to set the setTabsCloseable flag to true and add a pyqtSignal but I cannot do so because QObject has no connect attribute.
On the contrary most examples I have found online, mention the use of QtCore.QObject.Connect()
here is a minimum reproducible example:
from PyQt5 import QtWidgets, QtCore, QtWidgets
import sys, os
class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super(Dialog_01,self).__init__()
mainWidget=QtWidgets.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtWidgets.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QtWidgets.QTabWidget()
self.tabWidget.setTabsClosable(True)
# QtCore.QObject.connect(self.chatView, QtCore.SIGNAL('tabCloseRequested(int)'), self.closeTab)
mainLayout.addWidget(self.tabWidget)
myBoxLayout = QtWidgets.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtWidgets.QWidget(),'Tab_01')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

You can super the QtabWidget to incorporate custom functionality and also keep things wrapped up nicely.
I have found it quite common to override classes to suit certain programs with PyQt /Pyside and I strongly suggest you get into the habit!
from PyQt5 import QtWidgets, QtCore, QtWidgets
import sys, os
class QCustomTabWidget (QtWidgets.QTabWidget):
def __init__ (self, parent = None):
super(QCustomTabWidget, self).__init__(parent)
self.setTabsClosable(True)
self.tabCloseRequested.connect(self.closeTab) # connect to method to close
for i in range(1, 10): # add tabs here
self.addTab(QtWidgets.QWidget(), 'Tab %d' % i)
def closeTab (self, currentIndex):
currentQWidget = self.widget(currentIndex)
currentQWidget.deleteLater()
self.removeTab(currentIndex)
class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super(Dialog_01,self).__init__()
mainWidget=QtWidgets.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtWidgets.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QCustomTabWidget()
mainLayout.addWidget(self.tabWidget)
myBoxLayout = QtWidgets.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtWidgets.QTextEdit(),'Tab_01') #also add tabs here
self.tabWidget.addTab(QtWidgets.QTextEdit(),'Tab_02')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

Related

pyqt5 not showing window [duplicate]

This question already has answers here:
PyQt: app.exec_() stops all following code from running
(4 answers)
Should I use `app.exec()` or `app.exec_()` in my PyQt application?
(3 answers)
Closed 4 years ago.
I really hope someone can help me out on this one.
Im trying to get started with using pyqt5, and have pretty much copied this code from a course that i am taking. the code seems to execute without any problems, but the window that i should be seeing is simply not appearing, what am I doing wrong?
Im working on ubuntu 18 by the way
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Page(QWidget):
def __init__(self, parent=None):
super(Page, self).__init__(parent)
my_label = QLabel("This is my labet")
layout = QVBoxLayout()
layout.addWidget(my_label)
mainLayout = QGridLayout()
mainLayout.addLayout(layout, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("my first Qt app")
if __name__ == "__mongo__":
import sys
print("LOEREE")
app = QApplication(sys.argv)
window = Page()
window.show()
In your example there were some typos and at the end there was no line sys.exit (app.exec _ ()) which starts the main loop of the application. From here begins the processing of events.
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Page(QWidget):
def __init__(self, parent=None): # __init__
super(Page, self).__init__(parent) # __init__
my_label = QLabel("This is my labet")
layout = QVBoxLayout()
layout.addWidget(my_label)
mainLayout = QGridLayout()
mainLayout.addLayout(layout, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("my first Qt app")
if __name__ == '__main__': #
import sys
print("LOEREE")
app = QApplication(sys.argv)
window = Page()
window.show()
sys.exit(app.exec_()) # !!!

How can i create Multi-page applications in PyQt4?

I had a little research regarding this question, but i couldn't find any simple answer or tutorial to do this.
I'm trying to find a widget, that will make transition between pages using QPushButton. I have heard of QStackWidget, But i'm not exactly sure how to use it, I have found many tutorials, but all of them were hard to understand.
So for example, i have a window class:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
How can i add QStackWidget to this class? (Without any layouts)
If i do so, how can i switch to specific page using QPushButton? (With adding multiple widgets in one index)
extra: is it possible to add some kind of effect on transition? like fading, etc.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.stacked_widget = QtGui.QStackedWidget()
self.button = QtGui.QPushButton("Next")
self.button.clicked.connect(self.__next_page)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.stacked_widget)
layout.addWidget(self.button)
widget = QtGui.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.stacked_widget.addWidget(QtGui.QLabel("Page 1"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 2"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 3"))
def __next_page(self):
idx = self.stacked_widget.currentIndex()
if idx < self.stacked_widget.count() - 1:
self.stacked_widget.setCurrentIndex(idx + 1)
else:
self.stacked_widget.setCurrentIndex(0)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

PYQT Adding extra menu items to an existing UI before it opens

Hi I have designed a basic GUI in QT and created a .py file from it.
When the window starts up I want to add another menu item. I have tried a few pieces of code I found on google but nothing seems to work. The code will need to go in the method addAdminMenu()
from PyQt4 import QtGui
import sys
from supplypy.core.windows.main_window import Ui_MainWindow
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
sys.exit(self.app.exec_())
def addAdminMenu(self):
pass
#####Add code here to create a Admin menu####
if __name__ == '__main__':
srm = SRM()
It should be as simple as accessing the menuBar() of the QMainWindow and adding an item, for example: (I removed the Ui_MainWindow lines just because I don't know what it's for -- a Windows requirement?)
from PyQt4 import QtGui
import sys
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.menubar = self.MainWindow.menuBar()
self.MainWindow.show()
self.addAdminMenu()
sys.exit(self.app.exec_())
def addAdminMenu(self):
self.menubar.addMenu('&Admin');
if __name__ == '__main__':
srm = SRM()

Howto embed a video via mpylayer into a QWidget frame?

Is it possible in PyQt4 to embed a video via mpylayer into a QWidget (or into a subclass of it). If so, could you provide a minimal working example.
For a complete example of a Qt Widget that embeds MPlayer, try qmpwidget.
But here's a minimal PyQt demo to get you started:
import mpylayer
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.container = QtGui.QWidget(self)
self.container.setStyleSheet('background: black')
self.button = QtGui.QPushButton('Open', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.container)
layout.addWidget(self.button)
self.mplayer = mpylayer.MPlayerControl(
'mplayer', ['-wid', str(self.container.winId())])
def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName()
if not path.isEmpty():
self.mplayer.loadfile(unicode(path))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
(NB: this demo has only been tested on Linux)
You have to get the handle (id) of the widget → http://qt-project.org/doc/qt-4.8/qwidget.html#winId
And pass it to the -wid option of the MPlayer.
I can't provide you an example with Qt, simply because I don't know Qt, but I already wrote an MplayerCtrl for wxPython: https://bitbucket.org/dav1d/mplayerctrl
Relevant Code: https://bitbucket.org/dav1d/mplayerctrl/src/c680a1d99ad2/MplayerCtrl.py#cl-873

Problem in understanding connectSlotsByName() in pyqt?

I couldn't understand the connectSlotsByName() method which is predominently used by pyuic4.. As far the class is single in a PyQt file it's ok since we can use self which will be associated with a single object throughout.. But when we try to use various classes from different files the problem and the need to use connectSlotsByName() arises.. Here's what i encountered which is weird..
I created a stacked widget..
I placed my first widget on it.. It
has a button called "Next >".
On clicking next it hides the current
widget and adds another widget which has the "click me" button..
The problem here is the click event for "click me" button in second is not captured.. It's a minimal example that i can give for my original problem.. Please help me..
This is file No.1..(which has the parent stacked widget and it's first page). On clicking next it adds the second page which has "clickme" button in file2..
from PyQt4 import QtCore, QtGui
import file2
class Ui_StackedWidget(QtGui.QStackedWidget):
def __init__(self,parent=None):
QtGui.QStackedWidget.__init__(self,parent)
self.setObjectName("self")
self.resize(484, 370)
self.setWindowTitle(QtGui.QApplication.translate("self", "stacked widget", None, QtGui.QApplication.UnicodeUTF8))
self.createWidget1()
def createWidget1(self):
self.page=QtGui.QWidget()
self.page.setObjectName("widget1")
self.pushButton=QtGui.QPushButton(self.page)
self.pushButton.setGeometry(QtCore.QRect(150, 230, 91, 31))
self.pushButton.setText(QtGui.QApplication.translate("self", "Next >", None, QtGui.QApplication.UnicodeUTF8))
self.addWidget(self.page)
QtCore.QMetaObject.connectSlotsByName(self.page)
QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL('clicked()'),self.showWidget2)
def showWidget2(self):
self.page.hide()
obj=file2.widget2()
obj.createWidget2(self)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_StackedWidget()
ui.show()
sys.exit(app.exec_())
Here's file2
from PyQt4 import QtGui,QtCore
class widget2():
def createWidget2(self,parent):
self.page = QtGui.QWidget()
self.page.setObjectName("page")
self.parent=parent
self.groupBox = QtGui.QGroupBox(self.page)
self.groupBox.setGeometry(QtCore.QRect(30, 20, 421, 311))
self.groupBox.setObjectName("groupBox")
self.groupBox.setTitle(QtGui.QApplication.translate("self", "TestGroupBox", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton = QtGui.QPushButton(self.groupBox)
self.pushButton.setGeometry(QtCore.QRect(150, 120, 92, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText(QtGui.QApplication.translate("self", "Click Me", None, QtGui.QApplication.UnicodeUTF8))
self.parent.addWidget(self.page)
self.parent.setCurrentWidget(self.page)
QtCore.QMetaObject.connectSlotsByName(self.page)
QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL('clicked()'),self.printMessage)
def printMessage(self):
print("Hai")
Though in both the widgets(i mean pages)
QtCore.QMetaObject.connectSlotsByName(self.page)
the clicked signal in second dialog isn't getting processed. Thanks in advance.. Might be a beginner question..
A better question is "Why not just use new-style signals and slots?". They're much simpler and don't require any weird naming conventions:
from sys import argv, exit
from PyQt4 import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self._layout = QtGui.QVBoxLayout()
self.setLayout(self._layout)
self._button = QtGui.QPushButton()
self._button.setText('Click NOW!')
self._layout.addWidget(self._button)
self._button.clicked.connect(self._printMessage)
#QtCore.pyqtSlot()
def _printMessage(self):
print("Hai")
if __name__ == "__main__":
app = QtGui.QApplication(argv)
main = MyWidget()
main.show()
exit(app.exec_())
At first, here is the minimal working example:
from sys import argv, exit
from PyQt4 import QtCore, QtGui
class widget2(QtGui.QWidget):
def __init__(self, args):
self.app = MainApp(args)
QtGui.QWidget.__init__(self)
self.setObjectName('I')
self._layout = QtGui.QVBoxLayout(self)
self.setLayout(self._layout)
self.pushButtoninWidget2 = QtGui.QPushButton(self)
self.pushButtoninWidget2.setObjectName("pushButtoninWidget2")
self.pushButtoninWidget2.setText('Click NOW!')
self._layout.addWidget(self.pushButtoninWidget2)
QtCore.QMetaObject.connectSlotsByName(self)
#QtCore.pyqtSlot()
def on_pushButtoninWidget2_clicked(self):
print("Hai")
class MainApp(QtGui.QApplication):
def __init__(self, args):
QtGui.QApplication.__init__(self, args)
if __name__ == "__main__":
main = widget2(argv)
main.show()
exit(main.app.exec_())
When you trying to connect slots by name, you must give proper names to the slots and then someone (moc, uic, or you by calling connectSlotsByName) must connect them. Proper name for such a slot is: "on_PyQtObjectName_PyQtSignalName".
Note, that, if I'd omitted #QtCore.pyqtSlot() in the example, slot would be executed once for every appropriate overload (twice in this case).
You DO need to call connectSlotsByNames directly, cause there is no moc, which do it for you when you use QT in C++, and you do not use uic and .ui file. If you want to connect slots implicitly (I'm always doing so, except slots, connected directly in .ui), you'd better use more pytonish syntaxe: button.clicked.connect(self._mySlot).
And take a look at https://riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html#connecting-slots-by-name
You do not need to call connectSlotsByName(), just remove those lines.
In file2, calling QtCore.QMetaObject.connectSlotsByName(self.page) tries to do this:
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL('clicked()'), self.on_pushButton_clicked())
That will not work for you since self.on_pushBotton_clicked() slot is not defined.
I find it is easiest to create your own connections in PyQt... I recommend removing the calls to connectSlotsByName from your both classes... you do not need it.
Also, your wdiget1 class should set the name of it's pushButton (preferably something other then "pushButton" to avoid confusion with the button in widget2).
Thank you so much jcoon for your reply.. But after a very long time banging my head against the wall i found the solution..
The problem was..
self.obj=test_reuse_stacked1.widget2()
self.obj.createWidget2(self)
instead of obj..
Here is #MarkVisser's QT4 code updated to QT5:
from sys import argv, exit
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self._layout = QtWidgets.QVBoxLayout()
self.setLayout(self._layout)
self._button = QtWidgets.QPushButton()
self._button.setText('Click NOW!')
self._layout.addWidget(self._button)
self._button.clicked.connect(self._print_message)
#QtCore.pyqtSlot()
def _print_message(self):
print("Hai")
if __name__ == "__main__":
app = QApplication(argv)
main = MyWidget()
main.show()
exit(app.exec_())
Another minimal working example with Qt for Python aka PySide2/6.
Key ingredients:
widget to connect MUST have .setObjectName
function to connect MUST be decorated with #QtCore.Slot()
both objects (function AND widget) MUST be members of passed object (self here)
from PySide2 import QtCore, QtWidgets
# or from PySide6 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
layout = QtWidgets.QVBoxLayout(self)
self.button = QtWidgets.QPushButton(self)
self.button.setObjectName('button')
self.button.setText('Click Me!')
layout.addWidget(self.button)
QtCore.QMetaObject.connectSlotsByName(self)
#QtCore.Slot()
def on_button_clicked(self):
print(f'Hai from {self.sender()}')
if __name__ == '__main__':
app = QtWidgets.QApplication([])
main = Widget()
main.show()
app.exec_()
I couldn't get mit any smaller really 🤔

Categories

Resources