I'm looking for the most effective way to size a QGraphicsItem based on the length of a given QString, so that the text is always contained within the QGraphicsItem's boundaries. The idea is to keep the QGraphicsItem as small as possible, while still containing the text at a legible size. Wrapping onto multiple lines at a certain width threshold would be ideal as well. For example,
TestModule::TestModule(QGraphicsItem *parent, QString name) : QGraphicsPolygonItem(parent)
{
modName = name;
// what would be the best way to set these values?
qreal w = 80.0;
qreal h = 80.0;
QVector<QPointF> points = { QPointF(0.0, 0.0),
QPointF(w, 0.0),
QPointF(w, h),
QPointF(0.0, h) };
baseShape = QPolygonF(points);
setPolygon(baseShape);
}
void TestModule::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush *brush = new QBrush(Qt::gray, Qt::SolidPattern);
painter->setBrush(*brush);
painter->drawPolygon(baseShape);
painter->drawText(QPointF(0.0, 40.0), modName);
}
what code could I add to the constructor to make my requirement work? Setting the width based on the total length of the string, making assumptions for how much pixel space each character takes up is the most obvious solution, but I'm looking for something a little more elegant. Any ideas? Thank you in advance for any help.
The QFontMetrics class has a function called boundingRect which takes the string that you're wanting to print and returns the QRect for the string, based on the QFont that you used to initalise QFontMetrics.
If you want to wrap, then you'd need to work out the maximum number of words in your string that will allow boundingRect to return a QRect that fits within your QGraphicsItem's boundingRect.
Take a look to QFontMetrics
You can ask your widget for the font
And check this snippet from QFontMetrics docs
QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();
Edit: As Merlin said in comment, use
QRect QFontMetrics::boundingRect ( const QString & text ) const
So:
int pixelsWide = fm.boundingRect("What's the width of this text?").width();
Related
I have a QChartView which displays some 2D points which are representing each one a specific project I want to label each point with the project name AND NOT with it's x,y coordinates as the default behaviour
Is there any way to achieve override the function that creates or render the labels?
Why this could be difficult to achieve without changing the Qt source code
QXYSeries::setPointLabelsFormat wouldn't be of much help to you. It does indeed allow you to change the format of the labels, but the only variable part of it are the coordinates of the points.
All the drawing is done in the private part of the Qt classes. Here is the whole story:
The labels are drawn in the private part of QXYSeries (painter->drawText(position, pointLabel);):
void QXYSeriesPrivate::drawSeriesPointLabels(QPainter *painter, const QVector<QPointF> &points,
const int offset)
{
if (points.size() == 0)
return;
static const QString xPointTag(QLatin1String("#xPoint"));
static const QString yPointTag(QLatin1String("#yPoint"));
const int labelOffset = offset + 2;
painter->setFont(m_pointLabelsFont);
painter->setPen(QPen(m_pointLabelsColor));
QFontMetrics fm(painter->font());
// m_points is used for the label here as it has the series point information
// points variable passed is used for positioning because it has the coordinates
const int pointCount = qMin(points.size(), m_points.size());
for (int i(0); i < pointCount; i++) {
QString pointLabel = m_pointLabelsFormat;
pointLabel.replace(xPointTag, presenter()->numberToString(m_points.at(i).x()));
pointLabel.replace(yPointTag, presenter()->numberToString(m_points.at(i).y()));
// Position text in relation to the point
int pointLabelWidth = fm.width(pointLabel);
QPointF position(points.at(i));
position.setX(position.x() - pointLabelWidth / 2);
position.setY(position.y() - labelOffset);
painter->drawText(position, pointLabel);
}
}
drawSeriesPointLabels is called from the paint method of ScatterChartItem (this class is not included in the official documentation):
void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
if (m_series->useOpenGL())
return;
QRectF clipRect = QRectF(QPointF(0, 0), domain()->size());
painter->save();
painter->setClipRect(clipRect);
if (m_pointLabelsVisible) {
if (m_pointLabelsClipping)
painter->setClipping(true);
else
painter->setClipping(false);
m_series->d_func()->drawSeriesPointLabels(painter, m_points,
m_series->markerSize() / 2
+ m_series->pen().width());
}
painter->restore();
}
The ScatterChartItem in turn is created in the private part of QScatterSeries and can't be substituted with a custom class:
void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
{
Q_Q(QScatterSeries);
ScatterChartItem *scatter = new ScatterChartItem(q,parent);
m_item.reset(scatter);
QAbstractSeriesPrivate::initializeGraphics(parent);
}
What you might wanna try
Hide the original labels with setPointLabelsVisible(false); The labels will be drawn separately afterwards.
Subclass QChartView and reimplement the paintEvent, invoking first QChartView::paintEvent and then calling a custom function (lets say drawCustomLabels), which is a modified version of QXYSeriesPrivate::drawSeriesPointLabels. By calling drawCustomLabels pass:
a local painter drawing on the vieport of MyChartView,
the points as returned by QXYSeries::points,
the desired offset.
Here is an example of how the drawCustomLabels might look like:
void MyChartView::drawCustomLabels(QPainter *painter, const QVector<QPointF> &points, const int offset)
{
if (points.count() == 0)
return;
QFontMetrics fm(painter->font());
const int labelOffset = offset + 2;
painter->setFont(m_pointLabelsFont); // Use QXYSeries::pointLabelsFont() to access m_pointLabelsFont
painter->setPen(QPen(m_pointLabelsColor)); // Use QXYSeries::pointLabelsColor() to access m_pointLabelsColor
for (int n(0); n < points.count(); n++) {
QString pointLabel = "..."; // Set the desired label for the n-th point of the series
// Position text in relation to the point
int pointLabelWidth = fm.width(pointLabel);
QPointF position(points.at(n));
position.setX(position.x() - pointLabelWidth / 2);
position.setY(position.y() - labelOffset);
painter->drawText(position, pointLabel);
}
}
I'm new to Qt and Qt Graphics API.
I have a larger QPixMap and a smaller QPixMap. I need to replace a portion (a QRect) of the larger one with the smaller one.
How am I supposed to achieve this?
Thanks.
UPDATE
QPainter::drawPixmap() does not update the image represented by pImage->p_PixMap.
Code
class GraphicImage : public QObject,
public QGraphicsPixmapItem
{
Q_OBJECT
public:
GraphicImage(QPixmap* oImage,GraphiItemCtrl* pParent);
virtual ~GraphicImage(void);
QPixmap* p_PixMap;
};
- - - -
GraphicImage::GraphicImage(QPixmap* oImage,GraphiItemCtrl* pParent)
:QGraphicsPixmapItem(*oImage), p_Parent(pParent)
{
p_PixMap = oImage;
}
- - - -
void GraphiItemCtrl::SetImagePortion( QString sFileName, QRect rect, QPixmap pChildPixMap )
{
GraphicImage* pImage = map_CurrentImages[sFileName];
if ( !pImage )
return;
pChildPixMap.save("test.jpg");
QPixmap* pMap = pImage->p_PixMap;
QPainter pPainter(pMap);
pPainter.drawPixmap(rect, pChildPixMap);
qDebug() << rect.topLeft();
}
pChildPixMap.save("test.jpg"); saves the required portion of the image without an issue.
NOTE :
pImage is inherited from QObject and QGraphicsPixmapItem.
pMap is not NULL
The function you are looking for is:
void QPainter::drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
It will draw the pixmap into a rectangle portion of the painter's target.
You may also want to use this one:
void QPainter::drawPixmap(const QRect &target, const QPixmap &pixmap, const QRect &source)
Which will draw a portion of the source into a portion of the target.
In both cases if the sizes mismatch the image will be scaled, so if you are getting poor results, you will additionally need to tweak the scaling method.
As established in this answer, setting setRenderHint(QPainter::SmoothPixmapTransform); by itself does not seem to produce optimal results. If you want the best quality you will need to manually scale() the pixmap and then draw it, which produces much better results than scaling it on the fly while painting.
Quick pseudocode:
QPainter painter(pixmap1);
painter.drawPixmap(QRect, pixmap2);
Take a look at the documentation here
You need to use a painter on the destination pixmap to draw the source pixmap in a given destination rectangle:
void draw(QPixmap &dst, const QRect &dstRect, const QPixmap &src) {
QPainter(dst).drawPixmap(dstRect, src);
}
If you're drawing multiple such pixmaps on one destination, you should hold on to the painter - it'd be wasteful to construct new painter over and over in a loop:
struct Replacement {
QRect dstRect;
QPixmap source;
};
void draw(QPixmap &dst, const QList<Replacement> &replacements) {
QPainter painter{dst};
for (auto & r : replacements)
painter.drawPixmap(r.dstRect, r.source);
}
A QTreeView is rendered with the help of a custom QStyledItemDelegate::paint method. The intention is to add graphical elements to the nodes, e.g. to draw (and fill) a box around the item texts. The tree items may have check boxes, or not.
The Ruby code below achieves the goal, except that I cannot obtain the coordinates of the text element. An empirical offset (x=29; y=4) serves as a workaround. The super method draws the text on top of the box.
How can I obtain the coordinates of the text element?
Is this the right approach at all, or do I have to use drawText and drawControl instead of calling the superclass paint method? In that case, how do you control the layout of the sub elements?
(This question is not Ruby specific. Answers containing C++ are welcome.)
class ItemDelegate < Qt::StyledItemDelegate
def paint(painter, option, index)
text = index.data.toString
bg_color = Qt::Color.new(Qt::yellow)
fg_color = Qt::Color.new(Qt::black)
offset = Qt::Point.new(29,4)
painter.save
painter.translate(option.rect.topLeft + offset)
recti = Qt::Rect.new(0, 0, option.rect.width, option.rect.height)
rectf = Qt::RectF.new(recti)
margin = 4
bounding = painter.boundingRect(rectf, Qt::AlignLeft, text)
tbox = Qt::RectF.new(Qt::PointF.new(-margin,0), bounding.size)
tbox.width += 2*margin
painter.fillRect(tbox, bg_color)
painter.drawRect(tbox)
painter.restore
super
end
end
Edit: Please find a self-contained example here in this Gist.
I had the same problem in C++. Unfortunately, the workaround on option.rect.* properties seems to be the only way to find the text coords.
Here the paint method of my delegate:
void ThumbnailDelegate::paint(QPainter *p_painter, const QStyleOptionViewItem &p_option, const QModelIndex &p_index) const
{
if(p_index.isValid())
{
const QAbstractItemModel* l_model = p_index.model();
QPen l_text_pen(Qt::darkGray);
QBrush l_brush(Qt::black, Qt::SolidPattern);
/** background rect **/
QPen l_pen;
l_pen.setStyle(Qt::SolidLine);
l_pen.setWidth(4);
l_pen.setBrush(Qt::lightGray);
l_pen.setCapStyle(Qt::RoundCap);
l_pen.setJoinStyle(Qt::RoundJoin);
p_painter->setPen(l_pen);
QRect l_border_rect;
l_border_rect.setX(p_option.rect.x() + 5);
l_border_rect.setY(p_option.rect.y() + 5);
l_border_rect.setWidth(p_option.rect.width() - 16);
l_border_rect.setHeight(p_option.rect.height() - 16);
QPainterPath l_rounded_rect;
l_rounded_rect.addRect(QRectF(l_border_rect));
p_painter->setClipPath(l_rounded_rect);
/** background color for hovered items **/
p_painter->fillPath(l_rounded_rect, l_brush);
p_painter->drawPath(l_rounded_rect);
/** image **/
QPixmap l_pixmap = bytearrayToPixmap(l_model->data(p_index, ImageRole).toByteArray()).scaled(150, 150, Qt::KeepAspectRatio);
QRect l_img_rect = l_border_rect;
int l_img_x = (l_img_rect.width()/2 - l_pixmap.width()/2)+l_img_rect.x();
l_img_rect.setX(l_img_x);
l_img_rect.setY(l_img_rect.y() + 12);
l_img_rect.setWidth(l_pixmap.width());
l_img_rect.setHeight(l_pixmap.height());
p_painter->drawPixmap(l_img_rect, l_pixmap);
/** label **/
QRect l_txt_rect = p_option.rect;
l_txt_rect.setX(l_border_rect.x()+5);
l_txt_rect.setY(l_border_rect.y() + l_border_rect.height() -20);
l_txt_rect.setHeight(20);
l_txt_rect.setWidth(l_txt_rect.width()-20);
QFont l_font;
l_font.setBold(true);
l_font.setPixelSize(12);
p_painter->setFont(l_font);
p_painter->setPen(l_text_pen);
QString l_text = l_model->data(p_index, TextRole).toString();
p_painter->drawText(l_txt_rect, Qt::ElideRight|Qt::AlignHCenter, l_text);
}
else
{
qWarning() << "ThumbnailDelegate::paint() Invalid index!";
}
}
I am not skilled on Ruby but, as you can see, I am using drawPath, drawPixmap and drawText.
Here is the result:
I think it is better to avoid invoking paint from the superclass, since it should be done automatically by Qt and you may break something on the UI lifecycle.
I have a custom delegate in my QTableWidget to hightlight matches if a user searches something. Unfortunately the rectangle position does often not really fit This happens on some characters or phrases or depending on the number of matches or the size of the leading string. I can't find something specific causing this. Here is one example: .
This is my paint routine (a bit messy from all the trial and error trying to fix the issue):
void custom_delegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const{
const QTableWidget* table_widget = qobject_cast<const QTableWidget*>(qstyleoption_cast<const QStyleOptionViewItemV3*>(&option)->widget);
const int cell_width = table_widget->columnWidth(index.column());
// basic table cell rectangle
QRect rect_a = option.rect;
// adjust rectangle to match text begin
QStyle* style;
if(table_widget != 0){
style = table_widget->style();
}else{
style = QApplication::style();
}
const int text_horizontal_margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, table_widget) + 1;
QRect rect_b = rect_a.adjusted(text_horizontal_margin, 0, -text_horizontal_margin, 0);
// adjust rectangle to match text height
QFont cell_font = index.model()->data(index, Qt::FontRole).value<QFont>();
cell_font.setPointSize(9);
QFontMetrics fm(cell_font);
const int height = fm.height();
rect_b.setY(rect_a.y() + (rect_a.height() - height)/2);
rect_b.setHeight(height);
// displayed text
std::string cell_text = qstrtostr(fm.elidedText(index.model()->data(index, Qt::DisplayRole).toString(),Qt::ElideRight,rect_a.width()));
int found_pos = find_ci(cell_text, this->filter_string, 0);
int old_pos = 0;
int found_width = 0;
QRect rect_c = rect_b;
// find occurence of filter string in cell_text
while(found_pos != std::string::npos){
std::string front = cell_text.substr(0, found_pos);
rect_c.setX(rect_b.x() + fm.tightBoundingRect(QString::fromStdString(front)).width());
rect_c.setWidth(fm.width(QString::fromStdString(cell_text.substr(found_pos, this->filter_string.size()))));
painter->fillRect(rect_c, Qt::yellow);
old_pos = found_pos+1;
found_pos = find_ci(cell_text, this->filter_string, old_pos);
}
}
Notes: filter_string is the string searched for, find_ci is just a wrapper for std::string::find including case-insensitivity but not important here as this test case is fully lower-case and I use std::string for non-qt stuff.
Edit: For the width calculation I tried fm.tightBoundingRect().width(), fm.boundingRect.width() and fm.width() with different but never correct results.
I use Qt 5.2
In my case I got the desired result with the following hack:
auto initialRect = fm.boundingRect(text);
auto improvedRect = fm.boundingRect(initialRect, 0, text);
It's not entirely clear why the other overload of boundingRect returns the correct result, but it may be just accidental, because as the documentation states:
The bounding rectangle returned by this function is somewhat larger than that calculated by the simpler boundingRect() function. This function uses the maximum left and right font bearings as is necessary for multi-line text to align correctly. Also, fontHeight() and lineSpacing() are used to calculate the height, rather than individual character heights.
The width method you propose also will return larger result, but it does not seem correct, as it should be used only when you need a position for a next word:
[...] width() returns the distance to where the next string should be drawn.
Also, sometimes it matters whether you pass the result of painter.device() to QFontMetrics constructor.
I create a custom QGraphicsItem. And overwrite the boundingRect() and paint().
QRectF myTile::boundingRect() const
{
return QRectF(xPos*10, yPos*10, 10, 10);
}
void myTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = boundingRect();
int gvi = value * 255;
QColor gv(gvi, gvi, gvi, 255);
QBrush brush(gv);
painter->fillRect(rec, brush);
painter->drawRect(rec);
}
Then I use addItem() to add a item to a scene. Now I want to get it from the scene by its position. I find the itemAt function. But the problem is I don't know what is the const QTransform & deviceTransform. What should I use for the QTransform?.
Because I didn't implement any transform in the QGraphicsItem. This confuses me.
QGraphicsItem * QGraphicsScene::itemAt ( const QPointF & position, const QTransform & deviceTransform ) const
Returns the topmost visible item at the specified position, or 0 if
there are no items at this position. deviceTransform is the
transformation that applies to the view, and needs to be provided if
the scene contains items that ignore transformations. This function
was introduced in Qt 4.6.
So I would say, if you have the need to transform some items and ignore the others, you can simply go with the default value of QTransform() or even better the QGraphicsView::transform() const.
soo long zai