QtabWidget and QMainWindow in one class - python

Is there a way to define the menubar in a QtabWidget class ?
I wrote a code for a destopapplication with pyqt5 and python 3.6. I would like to set the manuBar in the same class as the Tabs but my code returns qtabwidget has no attribute QMainWindow.
Here is my code:
import sys
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *
class main_window(QTabWidget):
def __init__(self, parent=None):
super(QTabWidget, self).__init__(parent)
self.setGeometry(50, 50, 1100, 750)
self.setWindowTitle("Programm") #
self.centralWidget = QtWidgets.QWidget()
self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))
open_new_file = QAction('New', self)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('Projekt')
fileMenu.addAction(open_new_file)
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.show()
self.tab_v1 = QtWidgets.QWidget()
self.addTab(self.tab_v1, "Tab 1")
self.tab_v2 = QtWidgets.QWidget()
self.addTab(self.tab_v2, "Tab 2")
self.openFile = QPushButton("Choose Tab ", self.tab_v1)
self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))
def main():
app = QApplication(sys.argv)
ex = main_window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

QTabWidget can not have a QMenuBar, what you have to do is put the centralwidget of a QMainWindow to the QTabWidget.
import sys
from PyQt5 import QtCore, QtWidgets
class Main_window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Main_window, self).__init__(parent)
self.setGeometry(50, 50, 1100, 750)
self.setWindowTitle("Programm")
open_new_file = QtWidgets.QAction('New', self)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('Projekt')
fileMenu.addAction(open_new_file)
self.tabWidget = QtWidgets.QTabWidget()
self.setCentralWidget(self.tabWidget)
self.tab_v1 = QtWidgets.QWidget()
self.tabWidget.addTab(self.tab_v1, "Tab 1")
self.openFile =QtWidgets.QPushButton("Choose Tab ", self.tab_v1)
self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))
self.tab_v2 = QtWidgets.QWidget()
self.tabWidget.addTab(self.tab_v2, "Tab 2")
def main():
app = QtWidgets.QApplication(sys.argv)
ex = Main_window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Related

PyQt5, how to open new window after click QButton

I clicked the button Platform Type, but Ui_Form doesn't show
I tried exec_() but Process finished with exit code -1073740791 (0xC0000409)
I want to open new QWidget window when clicked the button
Details: In Python, I want to make one more window when I press the button of the first widget. I tried to follow the other contents, but the program terminated with an error code. I would appreciate if you could let me know what the problem is.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Main_Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self._mutex = QtCore.QThread()
self.setupUi(Main_Widget)
def setupUi(self, Main_Widget):
Main_Widget.setObjectName('Main_Widget')
Main_Widget.resize(1272, 640)
self.All_GroupBox = QtWidgets.QGroupBox(Main_Widget)
self.All_GroupBox.setGeometry(QtCore.QRect(20, 10, 1231, 611))
font = QtGui.QFont()
self.All_GroupBox.setFont(font)
self.All_GroupBox.setObjectName('All_GroupBox')
self.verticalLayoutWidget = QtWidgets.QWidget(self.All_GroupBox)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 261, 331))
self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')
self.Button_VerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.Button_VerticalLayout.setContentsMargins(0, 0, 0, 0)
self.Button_VerticalLayout.setObjectName('Button_VerticalLayout')
self.PlatformType_Button = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.PlatformType_Button.setMinimumSize(QtCore.QSize(0, 45))
self.PlatformType_Button.setObjectName('PlatformType_Button')
self.Button_VerticalLayout.addWidget(self.PlatformType_Button)
def PlatformType_Clicked(self):
dialog = Ui_Form(self)
self.dialogs.append(dialog)
dialog.show()
dialog.exec_()
class Ui_Form(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
def setupUi(self, Form):
Form.setObjectName('Form')
Form.resize(422, 190)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Main_Widget = QtWidgets.QWidget()
ui = Ui_Main_Widget()
Main_Widget.show()
app.exec_()
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Main_Widget(QtWidgets.QWidget):
# def __init__(self):
# super().__init__()
# self._mutex = QtCore.QThread()
# self.setupUi(Main_Widget)
def setupUi(self, Main_Widget):
Main_Widget.setObjectName('Main_Widget')
Main_Widget.resize(1272, 640)
self.All_GroupBox = QtWidgets.QGroupBox(Main_Widget)
self.All_GroupBox.setGeometry(QtCore.QRect(20, 10, 1231, 611))
font = QtGui.QFont()
self.All_GroupBox.setFont(font)
self.All_GroupBox.setObjectName('All_GroupBox')
self.verticalLayoutWidget = QtWidgets.QWidget(self.All_GroupBox)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 261, 331))
self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')
self.Button_VerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.Button_VerticalLayout.setContentsMargins(0, 0, 0, 0)
self.Button_VerticalLayout.setObjectName('Button_VerticalLayout')
self.PlatformType_Button = QtWidgets.QPushButton("PlatformType_Button", self.verticalLayoutWidget)
self.PlatformType_Button.setMinimumSize(QtCore.QSize(0, 45))
self.PlatformType_Button.setObjectName('PlatformType_Button')
# +++
self.PlatformType_Button.clicked.connect(Main_Widget.PlatformType_Clicked) # +++
self.Button_VerticalLayout.addWidget(self.PlatformType_Button)
# def PlatformType_Clicked(self):
# dialog = Ui_Form(self)
# self.dialogs.append(dialog)
# dialog.show()
# dialog.exec_()
class Ui_Form(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
def setupUi(self, Form):
Form.setObjectName('Form')
Form.resize(422, 190)
class MyWindow(QtWidgets.QWidget): # +++
def __init__(self):
super().__init__()
self.ui = Ui_Main_Widget()
self.ui.setupUi(self)
# +++
def PlatformType_Clicked(self):
self.dialog = Ui_Form() # --- self
# self.dialogs.append(dialog)
self.dialog.show()
# self.dialog.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
hide() and show() method you can use multiple dialog what ever you want,...
def PlatformType_Clicked(self):
dialog.hide()
dialog1.show()

Have QScrollArea react on wheelEvent, also in space taken up by children?

Consider this example:
#!/usr/bin/env python
import sys,os
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import Qt
class MainWindow(QtWidgets.QMainWindow):
class ScrollAreaWheel(QtWidgets.QScrollArea): # SO:9475772
def __init__(self, parent=None):
super(MainWindow.ScrollAreaWheel, self).__init__(parent)
self.parent = parent
def wheelEvent(self, event):
print("wheelEvent", event.angleDelta().y())
def __init__(self):
#~ self.do_init = QtCore.QEvent.registerEventType()
QtWidgets.QMainWindow.__init__(self)
self.setMinimumWidth(1000)
self.setMinimumHeight(400)
self.frame1 = QtWidgets.QFrame(self)
self.frame1.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame1layout = QtWidgets.QGridLayout(self.frame1)
self.frame1layout.setSpacing(0);
self.frame1layout.setContentsMargins(0,0,0,0);
self.frame1widget = QtWidgets.QWidget()
self.frame1widget.setLayout(QtWidgets.QGridLayout())
self.frame1layout.addWidget(self.frame1widget)
self.frame1scroll = MainWindow.ScrollAreaWheel(self) #QtWidgets.QScrollArea()
self.frame1scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.frame1scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.frame1widget.layout().addWidget(self.frame1scroll, 0, 0) #, Qt.AlignCenter)
#self.frame1scrolllayout = QtWidgets.QHBoxLayout(self.frame1scroll)
self.frame1scrolllayout = QtWidgets.QGridLayout(self.frame1scroll)
self.frame1scroll.setWidget(self.frame1scrolllayout.widget())
self.frame1scroll.setWidgetResizable(True)
self.frame1scroll.setAlignment(Qt.AlignCenter)
self.frame1label = QtWidgets.QLabel()
self.frame1scrolllayout.addWidget(self.frame1label, 0, 0, Qt.AlignCenter) ##
pixmap = QtGui.QPixmap(200, 100)
pixmap.fill(Qt.red)
self.frame1label.setPixmap(pixmap)
self.frame2 = QtWidgets.QFrame(self)
self.frame2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame2layout = QtWidgets.QHBoxLayout(self.frame2)
self.frame2layout.setSpacing(0);
self.frame2layout.setContentsMargins(0,0,0,0);
self.frame2scroll = QtWidgets.QScrollArea(self)
self.frame2scroll.setWidgetResizable(True)
self.frame2widget = QtWidgets.QWidget()
self.frame2widget.setLayout(QtWidgets.QGridLayout())
self.frame2scroll.setWidget(self.frame2widget)
self.frame2layout.addWidget(self.frame2scroll)
self.mainwid = QtWidgets.QWidget()
self.mainwid.setLayout(QtWidgets.QGridLayout())
self.setCentralWidget(self.mainwid)
self.splitter1 = QtWidgets.QSplitter(Qt.Horizontal)
self.splitter1.addWidget(self.frame1)
self.splitter1.addWidget(self.frame2)
self.splitter1.setSizes([600, 600]); # equal splitter at start
self.mainwid.layout().addWidget(self.splitter1)
self.mainwid.layout().update()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
main = MainWindow()
main.show()
sys.exit(app.exec_())
It generates this (Ubuntu 18.04):
I want to use mousewheel only on the left QScrollArea, for which I've made a separate class. However, its wheelEvent fires only when I'm outside the red box, not when I hover over it. How can I make ScrollAreaWheel.wheelEvent fire even when mouse is over the child label (the red box)?
You are the QLabel placing on top of the QScrollArea instead of placing it inside, visually it is the same but at the level of events it is not.
from PyQt5 import QtCore, QtGui, QtWidgets
class ScrollAreaWheel(QtWidgets.QScrollArea):
def wheelEvent(self, event):
print("wheelEvent", event.angleDelta().y())
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setMinimumSize(1000, 400)
frame1 = QtWidgets.QFrame(frameShape=QtWidgets.QFrame.StyledPanel)
scrollarea1 = ScrollAreaWheel(widgetResizable=True)
scrollarea1.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scrollarea1.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
widget1 = QtWidgets.QWidget()
scrollarea1.setWidget(widget1)
label_lay = QtWidgets.QGridLayout(widget1)
lay1 = QtWidgets.QVBoxLayout(frame1)
lay1.addWidget(scrollarea1)
pixmap = QtGui.QPixmap(200, 100)
pixmap.fill(QtCore.Qt.red)
label = QtWidgets.QLabel(pixmap=pixmap)
label_lay.addWidget(label, 0, 0, QtCore.Qt.AlignCenter)
#==============================
frame2 = QtWidgets.QFrame(frameShape=QtWidgets.QFrame.StyledPanel)
scrollarea2 = QtWidgets.QScrollArea(widgetResizable=True)
scrollarea2.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scrollarea2.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
widget2 = QtWidgets.QWidget()
scrollarea2.setWidget(widget2)
splitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)
splitter.addWidget(frame1)
splitter.addWidget(frame2)
splitter.setSizes([600, 600])
self.setCentralWidget(splitter)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

PyQt: How to create a scrollable window

I think it should be much easier to create a scrollable window in PyQt.
I have a list of labels that goes out of the window and I would like to scroll down to view them. At the moment the code does not give me an error, but the window just doesn't appear:
class Example(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
lbl_arr = makeLabelArr()
for i in range(1,8):
qb = lbl_arr[i]
# qb.setFixedWidth(300)
layout.addWidget(qb)
layout.setAlignment(Qt.AlignTop)
scroll = QScrollArea()
scroll.setWidget(self)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout.addWidget(scroll)
self.setLayout(layout)
self.setGeometry(0, 0, 600, 220)
self.setWindowTitle('SnP watchlist')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
#print(QDesktopWidget().availableGeometry())
ex = Example()
sys.exit(app.exec_())
Make the window itself a QScrollArea, like this:
class Window(QScrollArea):
def __init__(self):
super(Window, self).__init__()
widget = QWidget()
layout = QVBoxLayout(widget)
layout.setAlignment(Qt.AlignTop)
for index in range(100):
layout.addWidget(QLabel('Label %02d' % index))
self.setWidget(widget)
self.setWidgetResizable(True)
There is an example here: https://www.learnpyqt.com/tutorials/qscrollarea/
from PyQt5.QtWidgets import (QWidget, QSlider, QLineEdit, QLabel, QPushButton, QScrollArea,QApplication,
QHBoxLayout, QVBoxLayout, QMainWindow)
from PyQt5.QtCore import Qt, QSize
from PyQt5 import QtWidgets, uic
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.scroll = QScrollArea() # Scroll Area which contains the widgets, set as the centralWidget
self.widget = QWidget() # Widget that contains the collection of Vertical Box
self.vbox = QVBoxLayout() # The Vertical Box that contains the Horizontal Boxes of labels and buttons
for i in range(1,50):
object = QLabel("TextLabel: "+str(i))
self.vbox.addWidget(object)
self.widget.setLayout(self.vbox)
#Scroll Area Properties
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.setCentralWidget(self.scroll)
self.setGeometry(600, 100, 1000, 900)
self.setWindowTitle('Scroll Area Demonstration')
self.show()
return
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You should set layout after adding the scroll bar widget.
class Example(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
lbl_arr = makeArrayOfLabelsHTML()
for i in range(1,8):
qb = lbl_arr[i]
layout.addWidget(qb)
layout.setAlignment(Qt.AlignTop)
scroll = QScrollArea()
scroll.setWidget(self)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
layout.addWidget(scroll)
# set layout after adding scroll bar
self.setLayout(layout)
self.setGeometry(0, 0, 600, 220)
self.setWindowTitle('SnP watchlist')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
#print(QDesktopWidget().availableGeometry())
ex = Example()
sys.exit(app.exec_())

How to add a tab to QTabWidget using the button on the form?

I decided to write a visual form for my script.
The idea is to have a button that will add new tabs to QTabWidget. It does not work and I can not find a good example. I use PyQt5. Here's a piece of what I've tried:
import sys
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore, QtWidgets
class mainForm(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.runUi()
def runUi(self):
self.resize(250, 150)
self.move(300, 300)
self.setWindowTitle('Let\'s Rock!')
self.setWindowIcon(QIcon('icon.png'))
self.setMaximumSize(QtCore.QSize(560, 522))
self.setMinimumSize(QtCore.QSize(560, 522))
groupBoxGD = QtWidgets.QGroupBox('Соединение с ГД', self)
groupBoxGD.setGeometry(QtCore.QRect(10, 10, 541, 151))
hrLWGDLink = QtWidgets.QWidget(groupBoxGD)
hrLWGDLink.setGeometry(QtCore.QRect(10, 10, 521, 31))
hrLGD = QtWidgets.QHBoxLayout(hrLWGDLink)
hrLGD.setContentsMargins(0, 0, 0, 0)
btnAddTab = QtWidgets.QPushButton(hrLWGDLink)
btnAddTab.setText('Add tab')
hrLGD.addWidget(btnAddTab)
tabWidget = QtWidgets.QTabWidget(groupBoxGD)
tabWidget.setGeometry(QtCore.QRect(10, 170, 541, 351))
btnAddTab.clicked.connect(self.addProjectTab)
self.show()
def addProjectTab(self):
tab = QtWidgets.QWidget()
#how add tab at this line?
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = mainForm()
sys.exit(app.exec_())
You have to use the addTab() function, but to do so from another class the QTabWidget object must be a member of the class. Also I made some changes in the design because the button was on the QTabWidget, covering the tabs.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class mainForm(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.runUi()
def runUi(self):
self.resize(250, 150)
self.move(300, 300)
self.setWindowTitle('Let\'s Rock!')
self.setWindowIcon(QtGui.QIcon('icon.png'))
self.setMaximumSize(QtCore.QSize(560, 522))
self.setMinimumSize(QtCore.QSize(560, 522))
layout = QtWidgets.QVBoxLayout(self)
groupBoxGD = QtWidgets.QGroupBox('Соединение с ГД', self)
layout2 = QtWidgets.QVBoxLayout(groupBoxGD)
hrLWGDLink = QtWidgets.QWidget(groupBoxGD)
hrLGD = QtWidgets.QVBoxLayout(hrLWGDLink)
hrLGD.setContentsMargins(0, 0, 0, 0)
btnAddTab = QtWidgets.QPushButton(hrLWGDLink)
btnAddTab.setText('Add tab')
hrLGD.addWidget(btnAddTab)
self.tabWidget = QtWidgets.QTabWidget(hrLWGDLink)
hrLGD.addWidget(self.tabWidget)
layout2.addWidget(hrLWGDLink)
layout.addWidget(groupBoxGD)
btnAddTab.clicked.connect(self.addProjectTab)
def addProjectTab(self):
tab = QtWidgets.QWidget()
self.tabWidget.addTab(tab, "tab")
app = QtWidgets.QApplication(sys.argv)
w = mainForm()
w.show()
sys.exit(app.exec_())
Screenshot:

PyQt Change Direction of QWidget from right to left

I would like to change a direction of widget From Right to left by using SetLyouatDirection but does not work
This my result :
this my code :
for item in listConcrdance:
c+=1
widgitItem = QtGui.QListWidgetItem()
widget = QtGui.QWidget()
widgetText=QtGui.QLabel(str(c)+". "+item[1]+" ("+self.process.convertNumberToNameOFSorat(item[0][1])+":"+item[0][2]+")")
widgetText.setLayoutDirection(QtCore.Qt.RightToLeft)
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widgetLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
widget.setLayout(widgetLayout)
self.listWidgetQuran.addItem(widgitItem)
widgitItem.setSizeHint(widget.sizeHint())
self.listWidgetQuran.setItemWidget(widgitItem, widget)
Use [your_label].setAlignment(QtCore.Qt.AlignRight):
from PyQt4 import QtCore
from PyQt4 import QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.listWidget = QtGui.QListWidget(self)
self.verticalLayout.addWidget(self.listWidget)
for item in range(10):
widgitItem = QtGui.QListWidgetItem(self.listWidget)
widget = QtGui.QWidget()
widgetText = QtGui.QLabel(str(item))
if item % 2 == 0:
widgetText.setAlignment(QtCore.Qt.AlignRight)
else:
widgetText.setAlignment(QtCore.Qt.AlignLeft)
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widget.setLayout(widgetLayout)
widgitItem.setSizeHint(widget.sizeHint())
self.listWidget.setItemWidget(widgitItem, widget)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Output:

Categories

Resources