QT : QGraphicsTextItem alignment char to top-left corner - qt

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();
}

Related

Set text direction for QLabel?

Is there a way to set the text direction for a QLabel? I've got a situation in which I have QLabel objects whose text is only punctuation, and I want that to be displayed either in RTL or LTR format. (For instance, parentheses or quotation marks need to reverse depending on the text direction.) I've tried calling QLabel::setLayoutDirection, but to no effect.
#include <QApplication>
#include <QLabel>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QLabel label(" :  «");
label.setFont( QFont("Times New Roman", 72) );
label.setLayoutDirection( Qt::RightToLeft );
// label.setLayoutDirection( Qt::LeftToRight );
label.show();
return app.exec();
}
A workaround at this point is to prepend 0x202E ("Right-to-Left Override") to the string, but that's obviously a bit clunky.
label.setAlignment(Qt::AlignRight);

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: QMenu with translucent background

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.

QLabel consuming too much space

I use a QLabel and QPLineEdit within a QStackedWidget , the QLable should be nearly the size of the window holding this widget.
But when I set a extra long text to QLabel , it's expanding too much , and I'm not able to reduce the window size horizontally , the minimum width was too much.
I set the size policy of these three widgets to Minimum already , it just won't work for me.
UPDATE
maybe it's better saying like this: how to let QLabel display part of the text , when there's not enough space
SAMPLE CODE
#include <QtGui>
int main ( int argc , char **argv )
{
QApplication app (argc , argv);
QWidget w;
QLabel *label = new QLabel ("Very very very long text");
label->setSizePolicy (QSizePolicy::Minimum , QSizePolicy::Fixed);
QVBoxLayout layout (&w);
layout.addWidget ( label );
w.show();
return app.exec();
}
If I understand you correctly, the simplest thing to do is simply to ignore that label's horizontal size hint.
As long as you have other widgets in there (or force a minimum width manually to the container), this should do what you want:
#include <QtGui>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QLabel *l1 = new QLabel("This very long text doesn't influence "
"the width of the parent widget");
l1->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
// Style just to make it clear that the widget is
// being resized to fit the parent, it doesn't "overflow"
l1->setFrameShape(QFrame::Box);
l1->setFrameShadow(QFrame::Raised);
l1->setAlignment(Qt::AlignHCenter);
QLabel *l2 = new QLabel("This influences the width");
l2->setFrameShape(QFrame::Box);
l2->setFrameShadow(QFrame::Raised);
QWidget w;
QVBoxLayout layout(&w);
layout.addWidget(l1);
layout.addWidget(l2);
w.show();
return app.exec();
}

Resources