I want to have other zones for different kind of menus and a scroll area for scrolling a 2D space, like an big image. The problem is once I use a scroll area It takes over everything. All the other widgets disappears.
Here's a demo code:
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
QVBoxLayout, QFrame, QScrollArea, QHBoxLayout)
app = QApplication(sys.argv)
layout = QVBoxLayout()
b_widget=QWidget()
b_widget.setStyleSheet("background-color: blue")
r_widget=QWidget()
r_widget.setStyleSheet("background-color: red")
g_widget=QWidget()
g_widget.setStyleSheet("background-color: green")
scroll=QScrollArea()
scroll.setWidget(g_widget)
layout.addWidget(scroll)
layout.addWidget(r_widget)
layout.addWidget(b_widget)
widget = QWidget()
widget.setLayout(layout)
main=QMainWindow()
main.setCentralWidget(widget)
main.show()
app.exec()
Thank you for your help!
Related
I am here with pyqt question again.
I think, this question was probably answered somewhere, but I can't phrase my problem properly in a way to find it in google, so sorry if it's duplicated the question.
I wrote simple code to show the problem I am facing.
import sys
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QApplication, QMenu, QWidget, QPushButton
class MainWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
self.create()
def create(self):
btn_widget_a = QWidget()
btn_layout_a = QHBoxLayout(btn_widget_a)
self.mainLayout.addWidget(btn_widget_a)
btn_a = QPushButton()
btn_layout_a.addWidget(btn_a)
btn_a.setFixedSize(60, 60)
btn_b = QPushButton()
btn_layout_a.addWidget(btn_b)
btn_b.setFixedSize(60, 60)
tool_widget = QWidget()
btn_layout_a.addWidget(tool_widget)
tool_menu= QMenu(tool_widget)
tool_menu.addMenu('Options')
btn_a.setMenu(tool_menu)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.show()
app.exec_()
Problem is this btn_layout_a.addWidget(tool_widget) when I add QMenu inside the widget in my layout, buttons don't stretch anymore when you resize your UI. Is there a way to fix it? I know that if simply delete btn_layout_a.addWidget(tool_widget) it will work properly, but thing is that unless I put it in a widget, QMenu doesn't work properly inside the software called Maya. I tried adding stretch as an argument to widget/layout and tried command addStrerch() but nothing seems to give any result. Any suggestions? Thank you!
everyone.
I want to float a widget inside a layout of a main-window.
the widget is disappered from layout but not displayed on screen
As you can see from following code. I floated two labels 'lbl_title' and 'lbl_icon'
they seems to be floated but not displayed on screen.
Here comes my code.
If you loose commented line, then the lbl_icon and title is removed from layout but not are shown on my screen
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction,QGridLayout
from PyQt5.QtGui import QIcon,QPixmap
import sys
from PyQt5.QtCore import *
class CommonLessonItem(QWidget):
def __init__(self,parent):
super(CommonLessonItem,self).__init__(parent)
self.lbl_title = QLabel(self)
self.lbl_description = QLabel(self)
self.lbl_icon = QLabel(self)
self.__initUI()
self.isChild = False
def __initUI(self):
#set layout
self.layout = QGridLayout(self)
self.layout.addWidget(self.lbl_title,0,0,1,19)
self.layout.addWidget(self.lbl_icon,0,19,1,1)
self.layout.addWidget(self.lbl_description,1,0,1,20)
self.lbl_icon.setWindowFlags(Qt.FramelessWindowHint|Qt.Window)
self.lbl_icon.move(100,100)
self.lbl_title.setWindowFlags(Qt.FramelessWindowHint|Qt.Window)
self.lbl_title.move(100,100)
#initialize info
self.setInfo("Title","Description",None)
self.setLayout(self.layout)
def setInfo(self,title,description,iconPath):
self.lbl_title.setText(title)
self.lbl_description.setText(description)
if(iconPath is not None):
self.lbl_icon.setPixmap(QPixmap(iconPath))
def moveEvent(self,event):
super().moveEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = CommonLessonItem(None)
# mw.setSize(10,200)
mw.show()
sys.exit(app.exec_())
I need your help.
Whenever a widget becomes a top level window by setting the parent to None or, like in your case, setting the Window flag (but I wouldn't suggest that approach) show() must be called.
As explained in windowFlags:
Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..
Add self.lbl_icon.show() and self.lbl_title.show() after changing their window state.
My code succeeds in displaying 'Hello World!', however the CSS styling is not applied. I don't know how to do this properly. If anyone has some good explanations on this topic it would be very much appreciated.
from PySide2.QtWidgets import QApplication, QLabel, QWidget
app = QApplication([])
window = QWidget()
window.setStyleSheet('''
p {
font-size: 100px;
}
''')
label = QLabel('<p>Hello World!</p>', parent=window)
window.show()
app.exec_()
First of all, there is a different Qt StyleSheet that applies to all widgets and the html that some widgets support.
The Qt StyleSheets are a simple way to set the style and some properties to the QWidgets and that are based on CSS 2.1 but adapted to Qt.
Instead some widgets support rich text like html, and that is the case of QLabel, in this case the style must be placed inline
Considering there are several solutions:
from PySide2.QtWidgets import QApplication, QLabel, QWidget
app = QApplication([])
label = QLabel('<p style="font-size: 100px">Hello World!</p>', parent=window)
window.show()
app.exec_()
from PySide2.QtWidgets import QApplication, QLabel, QWidget
app = QApplication([])
window = QWidget()
window.setStyleSheet('''
QLabel {
font-size: 100px;
}
''')
label = QLabel('<p>Hello World!</p>', parent=window)
window.show()
app.exec_()
from PySide2.QtWidgets import QApplication, QLabel, QWidget
app = QApplication([])
window = QWidget()
label = QLabel('<p style="font-size: 100px">Hello World!</p>', parent=window)
font = label.font()
font.setPointSize(100)
label.setFont(font)
window.show()
app.exec_()
I have a class that displays a QMessageBox each time an action is performed. I was trying to set the button colour in the QMessageBox to a silver background.
At the moment the button is blue which is the same as the background of the QMessageBox.
My question is, how, with this piece of code: QtWidgets.qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}") can i change the QPushButton colour in the QMessageBox to silver.
This is a snippet of my code. I have tried to put the above snippet into the function so that when the button is clicked, the colour of the QPushButton in the message box will be silver. Is there a problem with this as it does not seem to make any change. Where should I place this styleSheet functionality in the code?
self.canonicalAddressesButton.clicked.connect(self.canonical_data_parsed_notification)
def canonical_data_parsed_notification(self):
QtWidgets.QMessageBox.information(self.mainwindow, 'Notification', 'Canonical Address Data Has Been Parsed!', QtWidgets.QMessageBox.Ok)
QtWidgets.qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")
The setStyleSheet() method should be invoked before creating the QMessageBox. Here is the trivial example how you can do it:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, qApp, QMessageBox
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 300, 200)
button = QPushButton('Click me', self)
qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")
button.clicked.connect(self.button_clicked)
def button_clicked(self):
QMessageBox.information(self, 'Notification', 'Text', QMessageBox.Ok)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = App()
widget.show()
sys.exit(app.exec_())
How do I create a drop-down widget, such as a drop-down QLabel, drop-down QTextBrowser, etc.?
For example, I log information in a QTextBrowser, but I don't want it taking up space on the screen. So I want to be able to click a QToolbutton and have a scrollable QTextBrowser drop-down. (A QComboBox would work too, but I can't just add each event as a separate item - I need the text to wrap, not be elided. Thus a drop-down QTextBrowser.)
Or, for example, I want a drop-down QLabel containing a picture, etc...
Create a QWidgetAction for the drop-down widget, and add it to the tool-button's menu:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QHBoxLayout(self)
self.button = QtGui.QToolButton(self)
self.button.setPopupMode(QtGui.QToolButton.MenuButtonPopup)
self.button.setMenu(QtGui.QMenu(self.button))
self.textBox = QtGui.QTextBrowser(self)
action = QtGui.QWidgetAction(self.button)
action.setDefaultWidget(self.textBox)
self.button.menu().addAction(action)
layout.addWidget(self.button)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(100, 60)
window.show()
sys.exit(app.exec_())