Qt: QMenu with translucent background - qt

I use Windows and I want to set a style sheet to a QMenu to give it a translucent background. In order for that to work, I first set the FramelessWindowHint, then I set the WA_TranslucentBackground attribute. Then I set my style sheet and display the menu with the popup method. It is drawn correctly, but it behaves strangely: As soon as it has the FramelessWindowHint, it is always visible (even before calling the popup() method). It does not hide itself anymore after one of its entries has been clicked.
Here is a minimalistic example:
#include <QApplication>
#include <QMenu>
#include <QPoint>
#include <QCursor>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMenu menu;
menu.addAction("about", &a, SLOT(aboutQt()));
menu.addAction("exit", &a, SLOT(quit()));
menu.setWindowFlags(Qt::FramelessWindowHint);
menu.setAttribute(Qt::WA_TranslucentBackground);
menu.setStyleSheet("QMenu{background:rgba(255, 0, 0, 50%);}");
menu.popup(QCursor::pos());
return a.exec();
}

menu.setWindowFlags(menu.windowFlags() | Qt::FramelessWindowHint);
should solve your problem. Now you are clearing all flags already set by Qt.

Related

Not mirroring layout direction in some of Qt widgets

I have an application which should have a right-to-left layout direction. But there some widgets(e.g. a QComboBox and a QlistWidget) which i don't want to mirror layout-direction (they should have left-to-right layout-direction whatever the layout-direction of app is).
What I'm looking for is something like LayoutMirroring.enabled in qml.
Is there a solution for this?
Edit:
This is a very simplified version of my code:
file widget.h:
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
};
file widget.cpp:
Widget::Widget(QWidget *parent): QWidget(parent){
setMinimumSize(300, 300);
QLabel *label1 = new QLabel("Right to left 1");
QLabel *label2 = new QLabel("Right to left 2");
QLabel *label3 = new QLabel("Right to left 3");
QComboBox *mCombo = new QComboBox();
mCombo->setMinimumWidth(150);
mCombo->addItems(QStringList({"Left to Right 1", "Left to Right 2", "Left to Right 3"}));
mCombo->setStyleSheet("QComboBox{padding: 0 10 0 10;}");
mCombo->setLayoutDirection(Qt::LeftToRight);
QVBoxLayout *mainlayout = new QVBoxLayout();
mainlayout->setAlignment(Qt::AlignLeft);
mainlayout->addWidget(mCombo);
mainlayout->addWidget(label1);
mainlayout->addWidget(label2);
mainlayout->addWidget(label3);
setLayout(mainlayout);}
and this my main.cpp:
#include "widget.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setLayoutDirection(Qt::RightToLeft);
Widget w;
w.show();
qDebug()<<a.layoutDirection() <<w.layoutDirection();
return a.exec();
}
comment: my project uses a stylesheet file and after playing with different parts of style for QComboBox I realized that the style "QComboBox{padding: 0 10 0 10;}" was causing the problem. So I included that, here. If I remove that line the problem will be solved.
note: I also realized that theWidget->setLayoutDirection(Qt::LeftToRight); will do what I was looking though I don't know it's the proper way or not!
So, The Problem was with the stylesheet that my app is using. this line of stylesheet "QComboBox{padding: 0 10 0 10;}" was the cause of problem. I removed it and problem solved. Though I don't know the reason.
Also for a specific widget that shouldn't get the app's layout-direction, the layout-direction must be set explicitly. like: theWidget->setLayoutDirection(Qt::LeftToRight);
And I realized it from Qt documentaion

QT : QGraphicsTextItem alignment char to top-left corner

I want to draw a character aligned to the top-left corner of parent.
QGraphicsTextItem * tItem = new QGraphicsTextItem(parent);
tItem->setPlainText("a");
tItem->setPos(QPointF(0,0));
Picture below presents output of my code (grey rectangle is parent of QGraphicsTextItem)
Result:
I want to get a result like this:
My dream result:
I tried to use Qt::AlignLeft and Qt::AlignTop but to no avail.
The solution is to use setDocumentMargin(0) to the QTextDocument, if you only put the letter "a" it seems that it is not the solution since there seems to be a vertical offset, but in reality it is not an offset but the capital letters have a higher height.
Example:
#include <QApplication>
#include <QGraphicsTextItem>
#include <QGraphicsView>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
auto parent = scene.addRect(QRectF(0, 0, 100, 100), QPen(Qt::red), QBrush(Qt::blue));
auto * tItem = new QGraphicsTextItem(parent);
tItem->setPlainText("aAbB");
tItem->document()->setDocumentMargin(0);
view.show();
return a.exec();
}

Rotation should not work after applying QGraphicsItem::ItemIgnoresTransformation flag

I have three level of hierarchy of items on a GraphicsView:
An ellipse2 whose ItemIgnoresTransformations is OFF (parent GraphicsView)
an ellipse3 whose ItemIgnoresTransformations is ON (parent ellipse2)
a text Item whose ItemIgnoresTransformations is OFF (parent ellipse3)
So now I want To rotate textItem only when ellipse3 is rotated not when the whole view is rotated.
It is working fine in case of view rotation But when i rotated ellipse3 then text also rotates though its transformation is OFF Please explain me how it is possible??
#include <QApplication>
#include<QGraphicsView>
#include<QGraphicsScene>
#include<QGraphicsTextItem>
#include<QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView graphicsView;//Graphics View
QGraphicsScene graphicsScene;
graphicsView.setScene(&graphicsScene);
QGraphicsEllipseItem ellipse2(QRectF(0,0,4,4));//ellipseItem2
ellipse2.setFlags(QGraphicsItem::ItemIgnoresTransformations);
graphicsScene.addItem(&ellipse2);
QGraphicsEllipseItem ellipse3(QRectF(10,0,4,4),&ellipse2);//ellipseitem3
QGraphicsTextItem textItem("TEXT",&ellipse3);
textItem.setFlags(QGraphicsItem::ItemIgnoresTransformations);
textItem.setPos(10,0);
ellipse3.rotate(90);
graphicsView.show();
return a.exec();
}

Qt QMenu remove drop shadow

I have a QMenu with a translucent background and rounded edges (border-radius). Unfortunately, Windows 7 draws a drop shadow for this menu, which does not fit to the rounded edges. Its the shadow that would be drawn for normal rectangular menues.
Is there either
- a way to completely disable drawing drop shadows for QMenu
or
- a way to make the shadow fit to the rounded edges
?
Here is a minimalistic example where it occurs:
#include <QApplication>
#include <QPushButton>
#include <QMenu>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QPushButton b("press me");
QMenu m;
m.addAction("hello"); m.addAction("world");
m.setWindowFlags(m.windowFlags() | Qt::FramelessWindowHint);
m.setAttribute(Qt::WA_TranslucentBackground);
m.setStyleSheet("background:rgba(255,0,0,50%); border-radius:5px;");
b.setMenu(&m);
b.show();
return a.exec();
}
This should do it:
w.setWindowFlags(w.windowFlags() | Qt::NoDropShadowWindowHint);

Qt: showFullScreen() on a QWidget doesn't remove Mac OSX Menu Bar

We get our application full screen alright, but the OSX Menu bar is still shown above it...
Is there any way around this problem?
Yes, might be a bug as already suggested. Try with FramelessWindowHint, like this:
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Test!");
label.setWindowFlags(Qt::FramelessWindowHint);
label.showFullScreen();
return a.exec();
}
I tested it on Mac OS X 10.7.1 (which is not even supported) and seems to work.

Resources