I am trying to change the icon of a QMenu's right arrow. The following QSS rule works
QMenu::right-arrow {
image: url(icons:icon_name.svg);
}
However, it changes the right arrows of all QMenus. I want to select only QMenus that have some property/name. I tried the following things:
/* shouldHide is a boolean property set to true */
QMenu[shouldHide="true"]::right-arrow {
image: url(icons:icon_name.svg);
}
QMenu::right-arrow[shouldHide="true"] {
image: url(icons:icon_name.svg);
}
I also tried wrapping the QMenu in a hidden QWidget and setting the stylesheet on the parent
container->setStyleSheet("QMenu::right-arrow: {image: url(icons:icon_name.svg); }");
I also attempted to set the style sheet on the menu itself in the following ways:
menu->setStyleSheet("QMenu::right-arrow: {image: url(icons:icon_name.svg); }");
menu->setStyleSheet("*::right-arrow: {image: url(icons:icon_name.svg); }");
... and none of them work.
This style will be applied for all QMenu Objects:
QMenu::right-arrow
{
image: url(icons:icon_name.svg);
}
If you want only to apply to a specific object, you could use the object name of the object:
Only Object Name:
#my_object_name::right-arrow
{
image: url(icons:icon_name.svg);
}
Object Name Of A Specefic Type
QMenu#my_object_name::right-arrow
{
image: url(icons:icon_name.svg);
}
Related
I need to create a button which may contain two icons and two text label to display (on screenshots - every line such as "Email" or "Connection" is a standalone button).
From what I understood - I can't override QPushButton or QAbstractButton and I have to implement my own custom widget in order to achieve such layout. So far I'm trying to make such buttons with Qt Designer and change style of the widget using QSS and dynamic properties but for some reason stylesheet is not changing when I press the button. My code:
namespace ui
{
ViewEntryPushButton::ViewEntryPushButton(QWidget* parent)
: QWidget(parent)
, ui(new Ui::ViewEntryPushButton)
{
ui->setupUi(this);
}
ViewEntryPushButton::~ViewEntryPushButton()
{
delete ui;
}
void ViewEntryPushButton::mousePressEvent(QMouseEvent* event)
{
this->setProperty("pressed", true);
this->style()->unpolish(this);
this->style()->polish(this);
this->update();
}
void ViewEntryPushButton::mouseReleaseEvent(QMouseEvent *event)
{
this->setProperty("pressed", false);
this->style()->unpolish(this);
this->style()->polish(this);
this->update();
}
} // namespace ui
And my style sheet which I apply to the highest object in Qt Designer object view:
#ViewEntryPushButton {
background-color: transparent;
padding: 5px;
margin-left: 5px;
}
#ViewEntryPushButton[pressed="true"]{
background-color:rgb(51, 0, 255);
border-width: 3px;
border-color: rgba(255, 255, 255, 20);
border-style: inset;
}
So my questions:
1. Is my approach right or it is possible to subclass QAbstractButton and implement it like this?
2. What am I doing wrong with style sheets in this case and why style of the widget is not changing when I press my mouse?
Did you tried you change stylesheet using the setstylesheet function like this in the constructor. Well this is just another way of doing thing.
Button.setstylesheet("")
Ended up using custom widget with QStackedLayout and transparent button above all other layers, I think it is the best approach.
I created necessary labels on "low" layers of the layout and just added a button above them all with background:transparent; property in style sheets.
Also StackingMode should be set to StackAll in the layout.
I have already tried to use disabled and !enabled but it doesn't work.
Here is my QSS code :
QPushButton {
background-color:#44c767;
border-radius:5px;
border:1px solid #18ab29;
color:#ffffff;
font-family:arial;
font-size:15px;
font-weight:bold;
text-decoration:none;
padding-right:10px;
outline: 0;
}
QPushButton:hover:!pressed {
background-color:#54d777;
}
QPushButton: pressed {
background-color:#30b252;
}
QPushButton: disabled {
background-color:#ff0000;
}
QPushButton: !enabled {
background-color:#ff0000;
}
The documentation refers to a disabled pseudo state but without providing more information about it.
Edit
I am using QT5.3
Remove all spaces after colons (really ALL - because it invalidates the further css text) and it will work:
QPushButton:disabled {
background-color:#ff0000;
}
Actually, both disabled and !enabled works.
I am trying to change the TableView highlight color from the system's default color to say red. Can this be done without editing the rowDelegate? Also I tried changing the highlightedTextColor but this didn't do anything.
TableView {
rowDelegate: Rectangle {
SystemPalette {
id: systemPalette
colorGroup: SystemPalette.Active
}
color: {
var baseColor = styleData.alternate ? systemPalette.alternateBase : systemPalette.base
return styleData.selected ? "your-color-here"/*systemPalette.highlight*/ : baseColor
}
}
}
This is my QML file:
Rectangle {
width:640;
height:360;
Text {
text:qsTr("Agritrade");
anchors.centerIn:parent;
}//text
MouseArea {
anchors.fill:parent;
onClicked: {
Qt.quit();
}
}//mouse area
}
I wish to make the window borderless. How to set the properties for the Rectangle above?
It seems like impossible to set properties for Rectangle tag to make a borderless frame.
The only solution below is applicable (use 'setFlags' method):
//qml viewer
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/agritrade/main.qml"));
viewer.setFlags(Qt::Window|Qt::FramelessWindowHint);
viewer.showExpanded();
I'm trying to find a way to do a transition on a QML element, when a binding changes. Say you have a Text element, with the text property bound to something. What I want is when the data in the binding changes, the element fades out (Still displaying old data), switches and fades back in with the new data (the actual transition occurring while the element isn't visible.)
I've been searching everywhere for a way to do this but I can figure it out. I've tried using Qt Quick animations within QML, but the data itself changes before the animation runs, leaving the animation unnecessary. I've tried creating a custom QDeclarativeItem object that calls an animation within the QDeclarativeItem::paint() but I can't figure out how to get it to actually run.
I should note here that I know my bindings are working fine as the displayed data changes, I just can't get these animations to run at the proper time.
Here is what I tried with QML:
Text {
id: focusText
text: somedata
Behavior on text {
SequentialAnimation {
NumberAnimation { target: focusText; property: "opacity"; to: 0; duration: 500 }
NumberAnimation { target: focusText; property: "opacity"; to: 1; duration: 500 }
}
}
}
And here is what I tried in implementing a custom QDeclarativeItem:
// PAINTER
void AnimatedBinding::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
// Setup the pen
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setOpacity(this->opacity());
// Draw the item
if (m_bindingType == QString("text")) {
QPropertyAnimation animation(this, "opacity");
animation.setDuration(1000);
animation.setStartValue(1);
if (drawn) {
animation.setStartValue(1);
animation.setEndValue(0);
animation.start();
} else drawn = true;
painter->drawText(boundingRect(), m_data.toString());
animation.setEndValue(0);
animation.start();
} else {
qCritical() << "Error unknown binding type!";
return;
}
}
But like I said, the animation that I start within the painter never actually fires.
Any tips? Anyone ever done this before? I've been banging my head on this for about a week.
How about doing it in qml only this ways :
Define a custom element of your own type, that behaves the way you want it to.
Use this element instead of traditional element to be animated.
eg. I have create a custom 'AnimatedText' type to have the fading in and fading out behavior on the text elements whenever text related to them changes.
File 1 : AnimatedText.qml
import QtQuick 1.0
Item
{
id: topParent
property string aText: ""
property string aTextColor: "black"
property int aTextFontSize: 10
property int aTextAnimationTime : 1000
Behavior on opacity { NumberAnimation { duration: aTextAnimationTime } }
onATextChanged:
{
topParent.opacity = 0
junkTimer.running = true
}
Timer
{
id: junkTimer
running: false
repeat: false
interval: aTextAnimationTime
onTriggered:
{
junkText.text = aText
topParent.opacity = 1
}
}
Text
{
id: junkText
anchors.centerIn: parent
text: ""
font.pixelSize: aTextFontSize
color: aTextColor
}
}
and in your main.qml
import QtQuick 1.0
Rectangle
{
id: topParent
width: 360
height: 360
AnimatedText
{
id: someText
anchors.centerIn: parent
aText: "Click Me to change!!!.."
aTextFontSize: 25
aTextColor: "green"
aTextAnimationTime: 500
}
MouseArea
{
anchors.fill: parent
onClicked:
{
someText.aText = "Some random junk"
}
}
}