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_())
Related
from PyQt6.QtWidgets import (
QMainWindow, QApplication, QDialog, QDialogButtonBox, QLabel, QTextEdit, QPushButton, QMessageBox, QMdiArea,
QTableWidgetItem, QStackedWidget
)
from PyQt6 import uic
import sys
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi(r"C:\Users\csc\Documents\Rentour\front.ui", self)
self.show()
# define widgets
self.button = self.findChild(QPushButton, "signinButton")
self.signinButton.clicked.connect(self.OpenSignUp)
def OpenSignUp(self):
Sign_Up = Second()
widget.addWidget(Sign_Up)
widget.setCurrentIndex(widget.currentIndex()+1)
class Second(QMainWindow):
def __init__(self):
super(Second, self).__init__()
uic.loadUi(r"C:\Users\csc\Documents\Rentour\signpopup.ui", self)
# define widgets
self.button = self.findChild(QPushButton, "SubmitSignButton")
self.SubmitSignButton.clicked.connect(self.SignUpSave)
def SignUpSave(self):
email =self.EmailLine.text()
phoneno =self.PhonenoLine.text()
name =self.NameLine.text()
password = self.PasswordLine.text()
print(password)
app = QApplication(sys.argv)
mainwindow = UI()
widget = QStackedWidget()
widget.addWidget(mainwindow)
widget.show()
app.exec()
this is my code. Am trying to create a login/signup page. So when i click the signinButton, i want it to load the ui for the page which will have a bunch of line edits whose inputs im attempting to store in variables.
The ui files were made using qt designer and and i made this file from scratch. I also referred code with Hala(Youtuber). I Am trying to create a login/signup page. So when i click the signinButton, i want it to load the ui for the page which will have a bunch of line edits whose inputs im attempting to store in variables.
The problem is in your OpenSignUp function. The line widget.addWidget(Sign_Up) is not a valid command for a couple of reasons.
Also Sign_Up varable is holding a newly constructed QMainWindow, which are meant to be top level widgets and shouldn't be added to a layout.
It isn't totally clear what you are trying to achieve, but I am going to assume that you are trying to launch a secondary sign in window, In which case you want to use the open() method to launch the new window.
For example:
def OpenSignUp(self):
self.Sign_Up = Second()
self.Sign_Up.open()
Since you are using a stacked widget it is also possible that your goal is to simply switch to a different widget in the same window. In which case your Second class should probably just be a standard QWidget, and not a QMainWindow. and your stacked widget should be set as the central widget of your UI class.
So that would probably look something like this:
from PyQt6.QtWidgets import (
QMainWindow, QApplication, QDialog, QDialogButtonBox, QLabel, QTextEdit, QPushButton, QMessageBox, QMdiArea,
QTableWidgetItem, QStackedWidget
)
from PyQt6 import uic
import sys
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
# uic.loadUi(r"C:\Users\csc\Documents\Rentour\front.ui", self)
self.widget = QStackedWidget()
self.widget.addWidget(mainwindow)
self.setCentralWidget(self.widget)
self.Sign_Up = Second()
self.widget.addWidget(self.Sign_Up)
# define widgets
self.button = self.findChild(QPushButton, "signinButton")
self.signinButton.clicked.connect(self.OpenSignUp)
def OpenSignUp(self):
self.widget.setCurrentIndex(widget.currentIndex()+1)
class Second(QWidget):
def __init__(self):
super(Second, self).__init__()
uic.loadUi(r"C:\Users\csc\Documents\Rentour\signpopup.ui", self)
# define widgets
self.button = self.findChild(QPushButton, "SubmitSignButton")
self.SubmitSignButton.clicked.connect(self.SignUpSave)
def SignUpSave(self):
email =self.EmailLine.text()
phoneno =self.PhonenoLine.text()
name =self.NameLine.text()
password = self.PasswordLine.text()
print(password)
app = QApplication(sys.argv)
mainwindow = UI()
mainwindow.show()
app.exec()
Except you will need to rearrange the .ui file for UI class so that it is applied on top of the stacked widget as well.
I have a QListWidget containing one item, I want the item's texts to be partially bold, so I created a QItemWidget for it. I use a QLabel in it to display the partially bold texts, the code is as follows:
ItemWidget.py
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QHBoxLayout, QLayout, QLabel
class ItemWidget(QtWidgets.QWidget):
def __init__(self):
super(ItemWidget, self).__init__()
self.setLayout(self.__get_layout())
def __get_layout(self) -> QHBoxLayout:
label = QLabel("<b>bold</b> texts")
widget_layout = QHBoxLayout()
widget_layout.addWidget(label)
widget_layout.setSizeConstraint(QLayout.SetFixedSize)
widget_layout.setContentsMargins(0, 0, 0, 0)
return widget_layout
Main.py
from PyQt5.QtWidgets import QListWidget, QWidget, QVBoxLayout, QApplication, QListWidgetItem
from ItemWidget import ItemWidget
class Window(QWidget):
def __init__(self):
super().__init__()
list_widget = QListWidget()
list_widget.itemClicked.connect(self.on_item_clicked)
vbox = QVBoxLayout()
vbox.addWidget(list_widget)
item_widget = ItemWidget()
item = QListWidgetItem()
list_widget.addItem(item)
list_widget.setItemWidget(item, item_widget)
self.setLayout(vbox)
def on_item_clicked(self):
print("Item clicked")
app = QApplication([])
window = Window()
window.show()
app.exec_()
This is the UI:
When I click on the item in the list, Item clicked should be printed to the output console. But the problem is, it only works if I don't click on the text:
The problem seems to disappear if I give a plain text to the QLabel in ItemWidget, such as label = QLabel("text"), why? What's the problem here?
Setting rich text content causes the mouse button release event to be always accepted (I suppose it's due to the fact that rich text can contain links).
The view must receive a mouse release event in order to call its mouseReleaseEvent() and eventually emit the clicked signal, but since the event was already accepted by the label, this won't happen.
If you don't need links in that text, just reset the text interaction flags:
label.setTextInteractionFlags(Qt.NoTextInteraction)
This is entire my code:
import sys
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
v = QVBoxLayout()
h = QHBoxLayout()
for a in range(10):
button = QPushButton(str(a))
button.clicked.connect(lambda checked, a=a: self.button_clicked(a)) # error here
h.addWidget(button)
v.addLayout(h)
self.label = QLabel("")
v.addWidget(self.label)
w = QWidget()
w.setLayout(v)
self.setCentralWidget(w)
def button_clicked(self, n):
self.label.setText(str(n))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
When I run this code, I get a window like this:
Below the buttons, there is a QLabel, and I want when I click on any button, the button's label will refer to this QLabel, but I get a bunch of confusing errors in the terminal. What's wrong with my code, help me, thanks.
The clicked signal is overload so it accepts 2 signatures where it can send a bool or not. The default signature depends on the library, in this case it seems that PySide2 by default does not send the "checked" parameter, unlike PyQt5 that does.
The solution is to indicate the signature:
button.clicked[bool].connect(lambda checked, a=a: self.button_clicked(a))
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.
I have a QMainWindow that have a button, when clicked this button, another widget pops up. This widget have a button, when clicked this button an alert message pops up. When I press 'OK' on that message button, only QMessageBox is closing, small widget is still open. I want to close that widget when I press 'OK' on that message button. I couldn't figure out how can I do this. Here is my code;
from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton,
QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout,
QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar,
QTextEdit,QDialog,QFrame,QProgressBar,QHBoxLayout
)
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette,QWindow
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer, QTimer,QPoint
import PyQt5.QtWidgets,PyQt5.QtCore
import time,random,subprocess,sys,json
class cssden(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setFixedSize(600,500)
#Other widget button
self.owidget = QPushButton(self)
self.owidget.clicked.connect(self.second_widget)
self.show()
#other widget
#I want to destroy this widget when I press 'OK' on the QMessageBox
def second_widget(self):
self.w_window = QWidget()
self.w_window.setGeometry(650,300,600,300)
self.w_window.setStyleSheet("background-color: lightblue")
self.w_button = QPushButton(self.w_window)
self.w_button.setText("Alert")
self.w_button.clicked.connect(self.alert)
self.w_window.show()
#alert from second widget
def alert(self):
QMessageBox.about(self.w_window,"Alert","Alert message")
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 1px solid black}")
ex = cssden()
sys.exit(app.exec_())
I tried to connect them ('OK' button of QMessageBox and last widget) but I couldn't do it very well and I'm really confused.
I figured out the solution by myself, just make a QMessageBox(no inherit), then find the result and check if the result is something you want, close() the widget.
self.result1 = QMessageBox(QMessageBox.Information,"Alert","Alert message",QMessageBox.Ok)
self.result1.setGeometry(500,500,500,500)
self.result1.show()
result = self.result1.exec_()
if result == 1024:
self.w_window.close()
I used 'OK' button so when I printed result the value was 1024, so before you check which button clicked (if you use yes|no buttons) print the result first then find the value, then do your job.