How to set QMdiAread TabBar Width - tabbar

I want to set the QMdiArea TabBar width by setStyleSheetfunction,but it can not set the width, it can set the height.Now I have no idea how to solve the problem, can you give me a idea?
Thanks a lot.Here is my code.
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(0,0,600,600)
self.mdi = QMdiArea(self)
self.mdi.setGeometry(40, 40, 500, 500)
self.mdi.setViewMode(QMdiArea.TabbedView)
self.mdi.setTabShape(QTabWidget.Triangular)
self.mdi.setTabsClosable(True)
self.mdi.setDocumentMode(False)
self.mdi.setTabsMovable(True)
self.mdi.setTabPosition(QTabWidget.North)
self.mdi.setOption(QMdiArea.DontMaximizeSubWindowOnActivation)
# setStyleSheet can not set the width,but it can set the height
self.mdi.setStyleSheet("QTabBar::tab { height: 20px; width: 20px; }");
self.setWindowTitle("MDI demo")
sub = QMdiSubWindow()
sub.resize(200, 200)
sub.setWindowTitle("11")
self.mdi.addSubWindow(sub)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = MainWindow()
demo.show()
sys.exit(app.exec_())

I have solve the problem.The main code is below.
child = self.mdi.findChild(QTabBar)
child.setExpanding(False)

Related

Indicating that a QOpenGLWidget widget is to have a translucent background does not appear to work

There is no effect when I set the WA_TranslucentBackground attribute on a widget (derived from QOpenGLWidget). I have to set the attribute on the main window instead, which doesn't seem right: the documentation clearly states that the attribute applies to the widget itself:
Indicates that the widget should have a translucent background, i.e.,
any non-opaque regions of the widgets will be translucent because the
widget will have an alpha channel
What is the correct way to make the widget itself have a translucent background? Here is the code:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget
from PyQt5.QtCore import Qt, QPointF, QLineF
from PyQt5.QtGui import QColor, QPen, QPainter, QPaintEvent
class LineWidget(QOpenGLWidget):
def __init__(self, parent, start : QPointF, end : QPointF, colour : str = 'black'):
super(LineWidget, self).__init__(parent)
self.colour = colour
self.line = QLineF(start, end)
# self.setAttribute(Qt.WA_TranslucentBackground)
def paintEvent(self, a0: QPaintEvent) -> None:
painter = QPainter(self)
painter.begin(self)
painter.setPen(QPen(QColor(self.colour), 5))
painter.drawLine(self.line)
painter.end()
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.lineWidget = LineWidget(self, QPointF(0, 0), QPointF(400, 400), 'red')
self.setCentralWidget(self.lineWidget)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

PyQt5 window opens then closes in a second

from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow
from PyQt5.QtGui import QIcon
import sys
class Main(QMainWindow):
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
self.setWindowTitle("QPushButton")
left = 300
top = 200
width = 300
height = 250
self.setGeometry(left, top, width, height)
self.show()
def main():
App = QApplication(sys.argv)
Main()
sys.exit(App.exec())
if __name__=="__main__":
main()
I don't get it, it worked before now it closes within a second of opening. I'm thinking somehow my installation got corrupted. Do you have any ideas I tried
def main():
App = QApplication(sys.argv)
Main()
App.exec_()

How to stretch QLabel in PyQt5

How to change the following code to get the QLabel stretch to width of the window ?
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 200, 100)
self.label = QLabel('Hello World!', self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setStyleSheet('font-size: 12pt; background-color: red')
self.show()
app = QApplication([])
win = Window()
app.exec()
As the documentation about QMainWindow says, you must set a central widget for it:
Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
The problem is that you need a layout manager in order to properly adapt widget sizes inside a parent, and just manually setting widget geometries is generally discouraged.
You created the label as a direct child, so it will have no knowledge about its parents size changes.
Just set the label as central widget.
self.setCentralWidget(self.label)
Otherwise, you can use a container widget, set a layout and add the label to it, but you still must set the central widget.
central = QWidget()
layout = QVBoxLayout(central)
layout.addWidget(self.label)
self.setCentralWidget(central)
The alternative is to directly use a QWidget instead of QMainWindow as you did in your answer.
you can use sizePolicy
self.label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
Thank you very much for your answers. The problem is now solved by the following code changes
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 200, 100)
self.label = QLabel('Hello World!', self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setStyleSheet('font-size: 12pt; background-color: red')
self.box_layout = QHBoxLayout()
self.box_layout.addWidget(self.label)
self.setLayout(self.box_layout)
self.show()
app = QApplication([])
win = Window()
app.exec()
Edit: laytout -> box_layout

KIVY python: Slider inside ScrollView

I created a scroll view in which i put some labels and 2 sliders.
The scroll works perfectly, but I can't change the slider's value with my mouse...
Please run this code and see:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.slider import Slider
from kivy.uix.textinput import TextInput
class Home(BoxLayout):
def __init__(self, **kwargs):
super(Home, self).__init__(**kwargs)
self.layout = GridLayout(cols=1, padding=5, spacing=20, size_hint=(1, None))
self.layout.bind(minimum_height=self.layout.setter('height'))
for i in range(50):
if i%25==0:
btn = Slider(min=1, max=10, value=4)
else:
btn = Label(text=str(i), color=(0,0,0,1), size=(32, 32), size_hint=(1, None))
self.layout.add_widget(btn)
self.scrll = ScrollView(size_hint=(1, .6), pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
self.scrll.add_widget(self.layout)
self.add_widget(self.scrll)
class MyAppli(App):
def build(self):
Window.clearcolor = (1,1,1,1)
return Home()
if __name__ == '__main__':
MyAppli().run()
Okay when you work with slider you shall redefine the on_touch_down, on_touch_up and on_touch_move method to handle those events:
-main.py :
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import NumericProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.slider import Slider
class Home(BoxLayout):
def __init__(self, **kwargs):
super(Home, self).__init__(**kwargs)
self.b = []
self.layout = GridLayout(cols=1, padding=5, spacing=20, size_hint=(1, None))
self.layout.bind(minimum_height=self.layout.setter('height'))
for i in range(50):
if i % 25 == 0:
self.b.append(MySlider(min=1, max=10, value=4, height=32, size_hint=(1, None)))
else:
self.b.append(Label(text=str(i), color=(0,0,0,1), height=32, size_hint=(1, None)))
self.layout.add_widget(self.b[i])
self.scrll = ScrollView(size_hint=(1, .6), pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
self.scrll.add_widget(self.layout)
self.add_widget(self.scrll)
def update(self, *args):
for i in range(50):
if i % 25 == 0:
self.b[i].begin = self.b[i].pos[0]
self.b[i].len = self.b[i].size[0]
class MySlider(Slider):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
super(MySlider, self).on_touch_down(touch)
def on_touch_up(self, touch):
if self.collide_point(*touch.pos):
super(MySlider, self).on_touch_up(touch)
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
super(MySlider, self).on_touch_move(touch)
class MyAppli(App):
def build(self):
Window.clearcolor = (1,1,1,1)
return Home()
if __name__ == '__main__':
MyAppli().run()
-some outputs :
I hope this helps !

pyqt5 videowidget not showing in layout

I am writing a program with pyqt5 where pressing a button first cycles through some pictures then cycles through some videos.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
import argparse
import sys
class MainWindow(QMainWindow):
def __init__(self,args):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = CentralWidget(args)
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
QCoreApplication.instance().quit()
self.centralwidget.startvid()
class CentralWidget(QWidget):
def __init__(self,args):
super(CentralWidget, self).__init__()
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignCenter)
self.setLayout(self.layout)
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.vw = QVideoWidget()
self.player.setVideoOutput(self.vw)
def startvid(self):
self.layout.addWidget(self.vw)
url= QUrl.fromLocalFile(glob.glob("videos/*")[0])
content= QMediaContent(url)
self.player.setMedia(content)
self.player.setVideoOutput(self.vw)
self.player.play()
if __name__== "__main__":
parser = argparse.ArgumentParser()
#~ parser.add_argument("-nb","--nobox",action="store_true", help="do not wait for the box connection")
args = parser.parse_args()
app = QApplication(sys.argv)
mainwindow = MainWindow(args)
#~ mainwindow.showFullScreen()
mainwindow.show()
sys.exit(app.exec_())
I tried to paste the minimal code. The thing is, I press the button nothing shows, although I used examples like this one PyQt5 - Can't play video using QVideoWidget to test if playing the video is ok, and these work. It's as if it is not adding the widget to the layout or something. Any idea what might be wrong?
I had to use QGraphicsView to achieve what I wanted, here is a fix:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = VideoPlayer()
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
self.centralwidget.phaseQuit(2)
self.centralwidget.play()
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoItem = QGraphicsVideoItem()
self.videoItem.setSize(QSizeF(640, 480))
scene = QGraphicsScene(self)
graphicsView = QGraphicsView(scene)
scene.addItem(self.videoItem)
layout = QVBoxLayout()
layout.addWidget(graphicsView)
self.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoItem)
self.counter = 0
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
pass
else:
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(glob.glob("videos/*")[self.counter])))
self.mediaPlayer.play()
self.counter += 1
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = MainWindow()
player.show()
sys.exit(app.exec_())