How to know header text width in QStyle? - c++

I have a QTreeView and use ProxyStyle for that.
The pic above is just the header. Now I need to draw the up/down arrow (for sorting items) beside header label as in figure. In order to put the arrow in the correct postion I need to know:
the left margin = distance between the text and left border
the text width
the right margin = distance between the text and the arrow
How can I calculate the text width in this case? I thought about QFontMetrics but dont know how to receive the text to calculate.
In my style I use only drawPrimitive function
void MyStyle::drawPrimitive( PrimitiveElement p_pe, const QStyleOption *p_option, QPainter *p_painter, const QWidget *p_widget ) const
{
int leftmargin = 10;
int rightmargin = 10;
if ( p_pe == PE_IndicatorHeaderArrow )
{
if ( const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>( p_option ) )
{
QPixmap pix;
if ( header->sortIndicator & QStyleOptionHeader::SortUp )
{
pix = QPixmap( ":/sortUp.png" );
}
else if ( header->sortIndicator & QStyleOptionHeader::SortDown )
{
pix = QPixmap( ":/sortDown.png" );
}
p_painter->drawPixmap( header->rect.left() + leftmargin+ subElementRect( SE_HeaderLabel, p_option, p_widget ).width() + rightmargin, header->rect.top() + pix.height(), pix );
}
}
else
{
QProxyStyle::drawPrimitive( p_pe, p_option, p_painter, p_widget );
}
}
I use subElementRect( SE_HeaderLabel, p_option, p_widget ).width() in this case but it is wrong. How can I calculate the width of the text?

It is all contained in the QStyleOptionHeader. The text width could be obtained by calling:
int textWidth = header->fontMetrics.boundingRect(header->text).width();

Related

Qt Window incorrect size until user event

I'm creating a screen where users can add certain tiles to use in an editor, but when adding a tile the window does not correctly resize to fit the content. Except that when I drag the window or resize it even just a little then it snaps to the correct size immediately.
And when just dragging the window it snaps to the correct size.
I tried using resize(sizeHint()); which gave me an incorrect size and the following error, but the snapping to correct size still happens when resizing/dragging.
QWindowsWindow::setGeometry: Unable to set geometry 299x329+991+536 on QWidgetWindow/'TileSetterWindow'. Resulting geometry: 299x399+991+536 (frame: 8, 31, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 259x329, maximum size: 16777215x16777215).
I also tried using updateGeometry() and update(), but it didn't seem to do much if anything.
When setting the window to fixedSize it will immediately resize, but then the user cannot resize the window anymore. What am I doing wrong here and where do I start to solve it?
Edit
Minimal verifiable example and the .ui file.
selected_layout is of type Flowlayout
The flowlayout_placeholder_1 is only there because I can't place a flowlayout directly into the designer.
Edit2
Here is a minimal Visual Studio example. I use Visual Studio for Qt development. I tried creating a project in Qt Creator, but I didn't get that to work.
Edit3
Added a little video (80 KB).
Edit4
Here is the updated Visual Studio example. It has the new changes proposed by jpo38. It fixes the issue of the bad resizing. Though now trying to downsize the windows causes issues. They don't correctly fill up vertical space anymore if you try to reduce the horizontal space even though there is room for more rows.
Great MCVE, exactly what's needed to easily investigate the issue.
Looks like this FlowLayout class was not designed to have it's minimum size change on user action. Layout gets updated 'by chance' by QWidget kernel when the window is moved.
I could make it work smartly by modifying FlowLayout::minimumSize() behaviour, here are the changes I did:
Added QSize minSize; attribute to FlowLayout class
Modifed FlowLayout::minimumSize() to simply return this attribute
Added a third parameter QSize* pMinSize to doLayout function. This will be used to update this minSize attribute
Modified doLayout to save computed size to pMinSize parameter if specified
Had FlowLayout::setGeometry pass minSize attribute to doLayout and invalidate the layout if min size changed
The layout then behaves as expected.
int FlowLayout::heightForWidth(int width) const {
const int height = doLayout(QRect(0, 0, width, 0), true,NULL); // jpo38: set added parameter to NULL here
return height;
}
void FlowLayout::setGeometry(const QRect &rect) {
QLayout::setGeometry(rect);
// jpo38: update minSize from here, force layout to consider it if it changed
QSize oldSize = minSize;
doLayout(rect, false,&minSize);
if ( oldSize != minSize )
{
// force layout to consider new minimum size!
invalidate();
}
}
QSize FlowLayout::minimumSize() const {
// jpo38: Simply return computed min size
return minSize;
}
int FlowLayout::doLayout(const QRect &rect, bool testOnly,QSize* pMinSize) const {
int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
// jpo38: store max X
int maxX = 0;
for (auto&& item : itemList) {
QWidget *wid = item->widget();
int spaceX = horizontalSpacing();
if (spaceX == -1)
spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
int spaceY = verticalSpacing();
if (spaceY == -1)
spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
x = effectiveRect.x();
y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
lineHeight = 0;
}
if (!testOnly)
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
// jpo38: update max X based on current position
maxX = qMax( maxX, x + item->sizeHint().width() - rect.x() + left );
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
}
// jpo38: save height/width as max height/xidth in pMinSize is specified
int height = y + lineHeight - rect.y() + bottom;
if ( pMinSize )
{
pMinSize->setHeight( height );
pMinSize->setWidth( maxX );
}
return height;
}
I was having the same exact issue (albeit on PySide2 rather than C++).
#jpo38's answer above did not work directly, but it un-stuck me by giving me a new approach.
What worked was storing the last geometry, and using that geometry's width to calculate the minimum height.
Here is an untested C++ implementation based on the code in jpo38's answer (I don't code much in C++ so apologies in advance if some syntax is wrong):
int FlowLayout::heightForWidth(int width) const {
const int height = doLayout(QRect(0, 0, width, 0), true);
return height;
}
void FlowLayout::setGeometry(const QRect &rect) {
QLayout::setGeometry(rect);
// e-l: update lastSize from here
lastSize = rect.size();
doLayout(rect, false);
}
QSize FlowLayout::minimumSize() const {
// e-l: Call heightForWidth from here, my doLayout is doing things a bit differently with regards to margins, so might have to add or not add the margins here to the height
QSize size;
for (const QLayoutItem *item : qAsConst(itemList))
size = size.expandedTo(item->minimumSize());
const QMargins margins = contentsMargins();
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
size.setHeight(heightForWidth(qMax(lastSize.width(), size.width())));
return size;
}
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const {
int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
for (auto&& item : itemList) {
QWidget *wid = item->widget();
int spaceX = horizontalSpacing();
if (spaceX == -1)
spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
int spaceY = verticalSpacing();
if (spaceY == -1)
spaceY = wid->style()->layoutSpacing(QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
x = effectiveRect.x();
y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
lineHeight = 0;
}
if (!testOnly)
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
}
int height = y + lineHeight - rect.y() + bottom;
return height;
}

Qt, QTransform rotation

I'm trying to draw text "on cylinder". It means, that I have five rows of text. Top row is rotated by the X axis for 10 degrees. The second for 5 degrees. The middle is not rotated at all. The four's row is rotated for -5 degrees. The five's is rotated for -10 degrees.
Rows 1, 2, 3 draws OK, but something is wrong with 4,5 rows. What am I doing wrong ?
I provides an image for understanding a problem and code snippet:
for( int i = 0; i < iterationsCount; ++i )
{
const QRect r( x2, y2, textWidth, itemHeight );
const QString text = sections.at( section ).values.at( index );
int rsc = 0;
p->save();
rsc = widgetHeight / 2 - y;
p->setTransform(QTransform().rotate(rsc, Qt::XAxis));
if( type == Section::DaySectionShort ||
type == Section::DaySectionLong )
{
QStringList values = text.split( QLatin1Char( ' ' ) );
p->setPen(
lighterColor( opt.palette.color( QPalette::WindowText ), 75 ) );
p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, values.at( 0 ) );
p->setPen( opt.palette.color( QPalette::WindowText ) );
p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, values.at( 1 ) );
}
else
{
p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, text );
}
p->setTransform(QTransform().rotate(-rsc, Qt::XAxis));
index = nextIndex( index, sections.at( section ).values.size() );
y += itemHeight + itemTopMargin;
p->restore();
}
My problem
As you have not provided a minimal complete code that reproduces the problem, I cannot guess what is wrong there. But the most probable reason is incorrect rsc calculation. At least the following draft works:
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
class MyWidget: public QWidget
{
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent)
{
QFont f = font();
f.setPointSize(15);
setFont(f);
}
protected:
void paintEvent(QPaintEvent *event) override
{
QWidget::paintEvent(event);
QPainter p(this);
const int itemHeight = fontMetrics().height();
const int itemTopMargin = 15;
const int xOffset = 15;
int y = itemHeight;
for (size_t i = 0; i < 5; ++i) {
// The angle is in range [-40, 40]. Remove " * 4" for [-10, 10].
const int rsc = (10 - 5 * i) * 4;
qDebug() << i << ":\t" << rsc << "\t" << y;
/*
Rotation must be performed relative to central point of the
drawn item. Transformations below are applied in reverse order.
At first translate item to make it's center in (0, 0). At
second rotate it relative to X axis. At third move the item to
desired position.
*/
QTransform transform;
transform.translate(xOffset, y + itemHeight / 2);
transform.rotate(rsc, Qt::XAxis);
transform.translate(0, - itemHeight / 2);
p.setTransform(transform);
p.drawText(QPoint(), QString("(Item no. %1)").arg(i + 1));
y += itemHeight + itemTopMargin;
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyWidget widget;
widget.setMaximumSize(200, 250);
widget.show();
return app.exec();
}
The transformation used here is complicated because of need to rotate each item relative to it's central y, not y = 0. This also may be the case.
Font and angles are increased to see considered effects better.

Custom placeholder in QLineEdit

I want to have a QLineEdit with the specific placeholder text format: it needs to have left aligned and right aligned text. Here is an example:
Any ideas?
Unfortunately, this seems to be all hard coded in void QLineEdit::paintEvent(QPaintEvent *) as follows:
if (d->shouldShowPlaceholderText()) {
if (!d->placeholderText.isEmpty()) {
QColor col = pal.text().color();
col.setAlpha(128);
QPen oldpen = p.pen();
p.setPen(col);
QRect ph = lineRect.adjusted(minLB, 0, 0, 0);
QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, ph.width());
p.drawText(ph, va, elidedText);
p.setPen(oldpen);
}
}
You could reimplement this on your own in a subclass if you wish.
Naturally, you could also "cheat" with space and font sizes, but that would require a bit more work, and would be nastier in the end, too, let alone long-term reliability.
You could also contribute to the Qt Project to make this class more flexible, but they could reject it with the reason of not being common case enough. It is up to the maintainer(s).
Thanks, #lpapp ! His advice is right. Here is the code, I created from the Qt sources suggested by #lpapp :
void LineEdit::paintEvent(QPaintEvent *e) {
QLineEdit::paintEvent(e);
if (!text().isEmpty()) {
return;
}
QPainter p(this);
QStyleOptionFrameV2 panel;
initStyleOption(&panel);
QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
r.setX(r.x() + textMargins().left());
r.setY(r.y() + textMargins().top());
r.setRight(r.right() - textMargins().right());
r.setBottom(r.bottom() - textMargins().bottom());
QFontMetrics fm = fontMetrics();
int minLB = qMax(0, -fm.minLeftBearing());
int minRB = qMax(0, -fm.minRightBearing());
int vscroll = r.y() + (r.height() - fm.height() + 1) / 2;
static const int horizontalMargin = 2; // QLineEditPrivate::horizontalMargin
QRect lineRect(r.x() + horizontalMargin, vscroll, r.width() - 2*horizontalMargin, fm.height());
QRect ph = lineRect.adjusted(minLB, 0, -minRB, 0);
QColor col = palette().text().color();
col.setAlpha(128);
p.setPen(col);
QString left = fm.elidedText("left", Qt::ElideRight, ph.width());
Qt::Alignment leftAlignment = QStyle::visualAlignment(Qt::LeftToRight, QFlag(Qt::AlignLeft));
p.drawText(ph, leftAlignment, left);
QString right = fm.elidedText("right", Qt::ElideRight, ph.width());
Qt::Alignment rightAlignment = QStyle::visualAlignment(Qt::LeftToRight, QFlag(Qt::AlignRight));
p.drawText(ph, rightAlignment, right);
}
I don't know an easy way to do this. You could try to calculate the pixel width (using QFontMetrics) of both placeholder-parts and calculate the number of spaces you need to insert between the placeholder-parts to let them appear aligned. You wuld need to set/update the calculated placeholder whenever the size of the widget changes.

Stretching a QLabel in a QGridLayout

I am manually creating a set of QLabels that are being put into a QGridLayout, and should be distributed evenly. When I create a test form using the QT Designer and add a series of labels, and put them in a QGridLayout, the labels occupy the full size of their cells in the grid. When I do this manually in c++, the labels don't expand and say the minimum size for the text. I am able to get these labels to expand vertically, but not horizontally.
Here is how I'm creating the QGridLayout:
m_layout = new QGridLayout;
m_layout->setHorizontalSpacing( 1 );
m_layout->setVerticalSpacing( 1 );
m_layout->setContentsMargins(0,0,0,0);
m_layout->setMargin(0);
m_layout->setSizeConstraint( QGridLayout::SetDefaultConstraint );
//m_layout->setSizeConstraint( QGridLayout::SetMaximumSize );
When I change the size constraint, it does not affect the size of the labels at all, so then I assumed that the issue may lie on how I'm creating the labels, which happens like this:
QLabel *label = new QLabel;
label->setAlignment( Qt::AlignCenter );
label->setFrameStyle( QFrame::Raised );
label->setMinimumSize( QSize(0,0) );
label->setMaximumSize( QSize(16777215, 16777215) );
label->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
Right now, all the QLabels are the same size, but eventually they will have differing sizes, which is why I'm using a packed-bin algorithm. I designed the algorithm around classes like this:
struct ButtonFittingNode
{
ButtonFittingNode( QRect rectangle, QGridLayout *layout )
: used( false )
, node( rectangle )
, down( NULL )
, right( NULL )
, m_layout( layout )
{ }
~ButtonFittingNode()
{
if ( down ) delete down;
if ( right ) delete right;
}
ButtonFittingNode *findNode( QLabel *btn )
{
int w = 1;
int h = 1;
if ( this->used )
{
ButtonFittingNode *bfn = NULL;
if ( this->right ) bfn = this->right->findNode( btn );
if ( this->down && !bfn ) bfn = this->down->findNode( btn );
return bfn;
}
else if ( ( w <= node.width() ) && ( h <= node.height() ) )
{
qDebug() << "Placing at " << node.x() << node.y();
m_layout->addWidget( btn, node.y(), node.x(), w, h, Qt::AlignJustify );
this->used = true;
this->splitNode( QSize( w, h ) );
return this;
}
return NULL;
}
void splitNode( QSize sz )
{
int w = 0, h = 0;
// Create a down node
w = this->node.width();
h = this->node.height() - sz.height();
if ( h > 0 )
{
QRect n( this->node.x(), this->node.y() + sz.height(), w, h );
this->down = new ButtonFittingNode( n, m_layout );
}
// Create a right node
w = this->node.size().width() - sz.width();
h = sz.height();
if ( w > 0 )
{
QRect n( this->node.x() + sz.width(), this->node.y(), w, h );
this->right = new ButtonFittingNode( n, m_layout );
}
}
bool used;
QRect node;
ButtonFittingNode *down;
ButtonFittingNode *right;
private:
QGridLayout *m_layout;
};
Using a 3x2 grid, I get the following output:
Sorry, I had to blur the text, since this is a work related project, but grey is background, white is the label's background. As you can see, they are tall, but skinny. I want these labels to be tall and fat. Am I setting some of the attributes wrong?
This is all really simple. The size constraint of the layout has got nothing to do with what you're seeing. You're also including a bunch of useless boilerplate code. Get rid of explicit setting of minimum and maximum sizes unless you have some non-default sizes in mind. This is completely useless:
label->setMinimumSize( QSize(0,0) );
label->setMaximumSize( QSize(16777215, 16777215) );
You need to set the label's sizePolicy to Expanding in the horizontal direction. That's all there's to it:
label->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
I see what you want from your bin packing system, but it won't work very well as-is, since the column/row sizes in the grid will vary as the grid gets resized. Your packer will choose some column/row spans based on then current row/column sizes. Upon resizing, the packer's decisions won't be adequate anymore.
What you really want is a custom layout that does all the packing on the fly. The flow layout example may be very close to what you want.

Printing QTableView using render method

I am trying to print a table view. To fill a table view I have created my own model. To print table I am doing following:
QPrinter printer;
QPrintDialog printDialog( &printer, 0);
if( QDialog::Accepted == printDialog.exec() ) {
if( QPrinter::Landscape != printer.orientation() ) {
printer.setOrientation(QPrinter::Landscape);
}
QPoint startPoint = QPoint(20, 20);
QRegion printRegion = QRegion( 20, 20, printer.paperRect().width(),printer.paperRect().height() );
for( int i = 0; i < m_tables.size(); ++i ) {
tableView->render( &printer, startPoint, printRegion, QWidget::DrawChildren );
}
}
The issue is that I am printing into PDF file and there I am able to see only a small part of the table. I thought that changing the region parameter could help, but in the fact not. Any suggestions how to fix this?
Ok, here is my solution. Would be nice to hear your opinion.
PrintTableModel* pTableModel = new PrintTableModel();
QTableView* pTableView = new QTableView;
pTableView->setModel(pTableModel);
int width = 0;
int height = 0;
int columns = pTableModel->columnCount();
int rows = pTableModel->rowCount();
pTableView->resizeColumnsToContents();
for( int i = 0; i < columns; ++i ) {
width += pTableView->columnWidth(i);
}
for( int i = 0; i < rows; ++i ) {
height += pTableView->rowHeight(i);
}
pTableView->setFixedSize(width, height);
pTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
pTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
This code helped me. To print the table correctly, you just can perform a render call:
pTableView->render(printer);
You might try void QPrinter::setResolution ( int dpi ) to force a number of widget pixels per printer inches, effectively zooming your widget on the printout.