Qt UIC: Non-modal pop-up not inheriting style from parent - pyqt5

I am trying to create a non-modal (modeless) QMessageBox window with several buttons and text. To make it modeless, I am giving it the MainWindow object as its parent (which inherits from QMainWindow).
For example:
class MainWindow(QMainWindow):
...
def create_popup(self):
message_box = QMessageBox(self)
# add buttons, text, etc.
message_box.setModal(False)
message_box.show()
However, the issue arises that I am providing the styling of my MainWindow through a stylesheet, built from a .ui file (using PyQt5.uic.loadUi('file', main_window)). In the .ui file, I am specifying the stylesheet of the MainWindow as having background-color: black. Because the QMessageBox I create inherits from MainWindow, it also inherits its stylesheet, making both the background of the box black and that of the buttons.
I know I could manually style the box, but I would like to be able the remove the style applied by the parent to the children, while still maintaining the ability to use the QMessageBox as a modeless pop-up.
If I remove the parent of the QMessageBox (so I do message_box = QMessageBox(None)), calling show() does nothing - I have to use exec_(). This does result in the expected presentation of the box with the default styling, however.
If I try to manually override the background-color attribute (with message_box.setStyleSheet("background-color: grey;")), this messes up the style of the buttons, and makes them into rectangular boxes (since the buttons also inherit from the QMessageBox).
I would like to make it so that either 1) the style applied to my MainWindow is not inherited by the QMessageBox, 2) the QMessageBox can be modeless without being a child of the MainWindow, or 3) I can override the styles with the full styles that are default to my platform (including OS and dark mode preferences, like Qt normally does).

Related

VSCT: Place Search Textbox below the toolwindow toolbar

I have made a toolbar that has a Search Textbox, using the visual studio built in search for the toolwindowpane: https://learn.microsoft.com/pt-pt/previous-versions/visualstudio/visual-studio-2015/extensibility/adding-search-to-a-tool-window?view=vs-2015&redirectedfrom=MSDN
That is working just fine right now after overriding the methods, however i also added a toolbar to the toolwindow from the vsct file, however it is positioning the search and the toolbar in the same row:
Is there any way to position the search bar underneath the toolbar just like the solution explorer in visual studio? If not, is there a way to decrease the toolbar size so it uses the minimum space so it doesn't look weird like in the picture?
Thank you.
EDIT: Constructor of the class that inherits ToolWindowPane:
public CpcObjectsWindow() : base(null)
{
this.Caption = "CPC Objects";
// This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
// we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
// the object returned by the Content property.
this.Content = new CpcObjectsWindowControl();
this.ToolBar = new CommandID(new Guid(CpcExtensionPackage.guidCpcExtensionPackageCmdSet), CpcExtensionPackage.cpcObjectsToolbar);
this.ToolBarLocation = (int)VSTWT_LOCATION.VSTWT_TOP;
}
I tried to add search to a tool window , a search control appears at the top of the tool window.
I tried to search form the ToolWindowPane Class but there is not any property can set the size of search bar. I think that you can submit a feature request on DC.

PyQt5: Why does QPushButton.setDefault() ignore spacebar but work for enter/return?

I have a modal with two buttons, one Accept and one Cancel.
I set the cancel button to be the default with .setDefault() and .setAutoDefault()
Pressing return activates the cancel-button, but when I press spacebar the accept-button is activated.
Why is the application/accept-button ignoring the defaultness-configuration and activates on spacebar presses rather than the cancel button? It seems like the accept-button has focus or something despite there being a different default.
Why would the default not have focus?
If I call cancel_button.setFocus() just before showing the modal (but not earlier than that), even the spacebar will activate the Cancel-button instead of the Acccept-button, so that solves the underlying problem.
The question is why spacebar and enter do not both activate the default button.
Minimal example:
The modal shows up when the program is run, as well as when the user presses X.
Press ctrl+Q to close the application.
import sys
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QApplication, QMainWindow, QGroupBox, QHBoxLayout, QVBoxLayout, \
QWidget, QShortcut, QDialog, QPushButton
class Modal(QDialog):
def __init__(self, parent):
super().__init__(parent)
self.resize(QSize(600, 300))
self.setParent(parent)
self.setWindowModality(True)
layout = QVBoxLayout()
self.setLayout(layout)
buttons = self.create_buttons()
layout.addWidget(buttons)
# This sets focus (when pressing spacebar), and makes the modal work as expected.
# The question is why is this needed to make spacebar default to activating Cancel?
# Why is spacebar activating Accept by default without this line?:
#self.cancel_button.setFocus()
def create_buttons(self):
button_groupbox = QGroupBox()
button_box_layout = QHBoxLayout()
button_groupbox.setLayout(button_box_layout)
# Despite setting the defaultness, pressing spacebar still activates the accept-button.
# Pressing return activates the cancel-button, however, and is expected behaviour.
# Why is the Accept-button being activated when space is pressed?
accept_button = QPushButton("Accept")
accept_button.clicked.connect(self.accept)
accept_button.setDefault(False)
accept_button.setAutoDefault(False)
self.accept_button = accept_button
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.reject)
cancel_button.setDefault(True)
cancel_button.setAutoDefault(True)
self.cancel_button = cancel_button
# This does not set focus (when pressing spacebar), maybe because it has not been added yet?
#cancel_button.setFocus()
button_box_layout.addWidget(accept_button)
button_box_layout.addWidget(cancel_button)
return button_groupbox
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
shortcut.activated.connect(app.quit)
shortcut = QShortcut(QKeySequence("X"), self)
shortcut.activated.connect(self.run_modal)
self.resize(QSize(800, 600))
self.show()
def showEvent(self, event):
self.run_modal()
def run_modal(self):
self.modal = Modal(self)
self.modal.finished.connect(self.modal_finished)
self.modal.show()
def modal_finished(self, result):
if result == 0:
print("CANCEL")
elif result == 1:
print("ACCEPT")
else:
raise Exception("BAD RESULT")
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwindow = MainWindow()
sys.exit(app.exec_())
By default, widgets receive focus based on the order in which they are added to a parent. When the top level window is shown, the first widget that accepts focus, following the order above, will receive input focus, meaning that any keyboard event will be sent to that widget first.
Note that when widgets are added to a layout, but were not created with the parent used for that layout, then the order follows that of the layout insertion.
The default property of QPushButtons, instead will "press" the button whenever the top level widget receives the Return or Enter keys are pressed, no matter of the current focused widget, and as long as the focused widget does not handle those keys.
In your case, the currently focused widget is the "Accept" button (since it's the first that has been added to the window), which results in the counter-intuitive behavior you're seeing.
If you want the cancel button to react to both Return/Enter keys (no matter what is the focused widget) and the space bar upon showing, then you have to explicitly call setFocus(). But there's a catch: since setFocus() sets the focus on a widget in the active window, it can only work as long as that widget already belongs to that window.
In your case, the cancel_button.setFocus() call done within create_buttons won't work because, at that point, the button doesn't belong to the top level window yet.
It does work when you do that after layout.addWidget(buttons), because then the button is part of the window.
So, considering the above:
if you want to set the focus on a widget, that widget must already belong to the top level widget before calling setFocus();
the default button will always be triggered upon Return/Enter keypress even if another button has focus;
With your current code, you either do what you already found out (using setFocus() on the instance attribute after adding the widget), or use a basic QTimer in the create_buttons function:
QTimer.singleShot(0, cancel_button.setFocus)
Note that:
while creating separate functions can help you to better organize your code, having a separate function that is just called once is often unnecessary (other than misleading and forcing the creation of instance attributes where they're not actually necessary); just separate code blocks with empty lines, unless those functions can be overridden by further subclasses;
setting a "Cancel" button that can be activated by Return/Enter is not a very good idea, as those keys are generally used for "Accept/Apply/Commit/Write/etc." purposes;
if you want to show a dialog as soon as its parent is shown, you shall only use a QTimer: QTimer.singleShot(0, self.run_modal); the paint event is certainly not a viable option (paint events occur very, very often, and in some systems even when the widget loses focus, which can cause recursion), nor is the showEvent() since that could happen when switching virtual desktops or unminimizing the window;

Prevent el-menu-item from adding "is-active" class when clicked

Using element-ui framework, how do I prevent el-menu component's el-menu-item from adding "is-active" class when clicked?
I don't want to just override the css class but I want to completely remove it.

Custom QLineEdit, focusProxy and QDataWidgetMapper

This problem is driving me nuts so I would appreciate some assistance. Obviously done the usual googling and checking qt docs but have not found any solution.
We need to customize a standard QLineEdit. Here is a prototype:
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget* a_parent)
{
QTextEdit* textEdit = new QTextEdit(this);
textEdit->setMinimumWidth(width() / 2);
textEdit->setMaximumWidth(width() / 2);
textEdit->setMinimumHeight(30);
textEdit->setTabChangesFocus(true);
// background color red
QPalette p = textEdit->palette();
p.setColor(QPalette::Base, QColor(244,20,20));
textEdit->setPalette(p);
textEdit->setVisible(true);
setFocusProxy(textEdit);
}
};
This shows a QLineEdit with a QTextEdit on-top of it.
Using the keyboard to change focus works as expected (the TextEdit gets the focus and not the LineEdit).
Using the mouse to change focus works as expected (selecting the LineEdit forwards the focus to the TextEdit).
The MyLineEdit and regular QLineEdit instances are added to a QDataWidgetMapper. We obviously use setModel method call on the QDataWidgetMapper instance.
In the QDialog, changing focus from a QLineEdit using keyboard or mouse triggers an invocation of our implementation of QAbstractItemModel::setData.
In the same QDialog, changing focus from a MyLineEdit using keyboard or mouse does NOT trigger an invocation of our implementation of QAbstractItemModel::setData.
This problem seems to be related to the setFocusProxy API. When removing setFocusProxy line;
changing focus from a MyLineEdit (specifically the QLineEdit) using keyboard or mouse does trigger an invocation of our implementation of QAbstractItemModel::setData.
changing focus from a MyLineEdit (specifically the QTextEdit) does NOT trigger an invocation of our implementation of QAbstractItemModel::setData.
How can I use setFocusProxy and get invocation of QAbstractItemModel::setData?
What am I doing wrong?

Titanium: How to remove background of Search bar?

How can I remove the background of search bar ? I tried by changing background color but it also changes cancel button's color !!!
Thanks...
The best alternative to this is creating a custom search bar with Ti.UI.textField and Ti.UI.button. Add them both to a view and customize it as you please. Finally, just add an event listener to the button click, and voila!
Take a look at this Module: https://github.com/viezel/NappUI
It extends the properties for several UI Elements, including SearchBar, here is the list.
SearchField BackgroundImage
Custom Cancel button
barColor - background gradient of the button. (similar to navbar)
color - color of the button title
title - change the default Cancel text
font - set the font of the button
Appearance of the keyboard
Disable the search icon
To install it, I recommend you to use the new gitTio command line, this will automatically download the module, install it on the modules folder on Application Support folder and add the proper config line on tiapp.xml.
gittio install -g dk.napp.ui
And here is an example of a SearchBar using the new properties enabled by this Module
var searchBar = Ti.UI.createSearchBar({
searchFieldBackgroundImage:"searchbg.png",
showsScopeBar:true,
scopeButtonTitles:["hello", "yes"],
customCancel:{
barColor:"#333",
color:"#ddd",
title:"Hit me",
font:{
fontSize:16,
fontWeight:"bold",
fontFamily:"Georgia"
}
},
appearance:Titanium.UI.KEYBOARD_APPEARANCE_ALERT,
barColor:"transparent",
disableSearchIcon:true //disables the search icon in the left side
});
If you are talking about the gradient blue, I removed it on my app with:
var searchBox = Ti.UI.createSearchBar({
barColor: '#eee'
});
Hope this helps.
Unfortunately 'barColor' doesn't work. Ti seems to change the color by changing the opacity or hue or something. DannyM's workaround is the best.
I must have wasted a zillion hours making sense of Titanium's background colors, background images, bar colors and their active/inactive cousins.
Conclusion: "Free" software is costly if you count the time you waste on silly bugs and lack of useful documentation.