Scale QWidget height based on width - c++

I have a QLabel that contains an image. I need to ensure that the image allways takes 100% of the QLabel width, but preserving aspect ration is just as important. This means I also need to increase QLabel height so that the image fits. Like on this image, the height must be fit:
I Qt designer I see no way to specify preferred aspect ratio, so I just tried to override resize event and try to force correct height upon resize:
void QPictureLabel::resizeEvent(QResizeEvent *event)
{
qDebug()<<"Resized.";
// Current image width in pixels
float pw = myPixmap.width();
// current label width in pixels
float my_width = width();
// ratio of label and image width can decide the new required label height
float new_h = (my_width/pw)*myPixmap.height();
// This is an attempt to prevent endless loop... didn't work out
if(new_h!=height()) {
// Force new height (that works)
resize(my_width, new_h);
// Tell the layout to move other elements (that doesn't work at all!)
updateGeometry();
}
}
Before adding the updateGeometry call it just looked like this:
As you can see (I highlighted it with red frame), the label indeed expanded as necessary. But other widgets do not care about that.
I added updateGeometry call, but the result was endless loop for some reason. I need the correct way to inform the layout that QLabel requires more space.

but the result was endless loop for some reason
This is documented, read documentation on resizeEvent(). Shf's solution should work for you.

Related

QSplitter disable scrollbar on resizing QLabel

I have added 2 QScrollArea-> QLabel in QSplitter. And am displaying video on both of them. And by default, both of them have same size..
So, when I increase the size of one QLabel by adjusting QSplitter handle, the video takes new allocated space and scales accordingly. However, if I then decrease the size of QLabel, the video would still show up at previous bigger scale and scrollbars would appear.
I instead want to display video without scrollbars and with whatever scale factor that fits current size.
How can this be achieved?
Here is screenshot of my dialog.ui:
And this is my code for displaying image on QLabel
multiews[0] = ui->QImageLabel_view0;
multiews[1] = ui->QImageLabel_view1;
multiews[image_view]->setPixmap(QPixmap::fromImage(qImg).scaled(multiews[image_view]->width(),multiews[image_view]->height(),Qt::KeepAspectRatio));
Update:
I tried to manage desired behaviour by managing
void Dialog::on_splitter_2_splitterMoved(int pos, int index)
{
if(m_videos[0].video_playing){
int w= ui->scrollArea_2->width();
int h=ui->scrollArea_2->height();
ui->QImageLabel_view0->setFixedSize(w-20,h-20);
}
if(m_videos[1].video_playing){
int w= ui->scrollArea_1->width();
int h=ui->scrollArea_1->height();
ui->QImageLabel_view1->setFixedSize(w-20,h-20);
}
}
And am setting back policy to expanding when am using zoom out/zoom in.
void Dialog::PausePlayingVideo()
{ ...
m_scrollarea[active_view]->setWidgetResizable(true);
multiews[active_view]->setMaximumSize(10000,10000);
m_scrollarea[active_view]->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
multiews[active_view]->adjustSize();
multiews[active_view]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
}
However, the qLabel size is not expanding with this code. Does anybody know, what am doing wrong?

How to Reduce the noisy While Scale a Image/Picture with Qt

I use the code just like:
void MyLabel::paintEvent(QPaintEvent *event){
QPainter painters(this);
/*对img进行平滑缩放*/
int image_width,image_height;
image_width = width();
image_height = height();
QImage result = img.scaled(image_width<<2,
image_height<<2).scaled(image_width,
image_height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation
);
painters.drawPixmap(0,0,image_width,image_height,QPixmap::fromImage(img));
}
I want to scale a image to the size what I want, but when I scale it to Non equal proportion , terrible things happened ,noisy is huge ,and the image/picture just like this:
And i want to know how to get a way scale a image/picture like this instead:
I suspect it is because you call QImage::scaled() twice, first to upscale to 4× the desired size and second to your actual desired size. The first call uses the default value for the transformation mode (which is Qt::FastTransformation). So essentially you're combining a non-smooth and a smooth transformation. Note that the first transformation should be unnecessary anyway. Just scale to the desired size immediately, using Qt::SmoothTransformation.
QImage result = img.scaled(image_width, image_height,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
EDIT: Just noticed something else. Look at your drawPixmap line:
painters.drawPixmap(0,0,image_width,image_height,QPixmap::fromImage(img));
You're drawing the original img when you meant to draw result:
painters.drawPixmap(0,0,image_width,image_height,QPixmap::fromImage(result));

Set slider size of QScrollBar correspond with content

I am doing a software with a drawing surface that represent a plot (like a sin function) (A child of QWidget) and I would like to have a QScrollBar acting like a QScrollArea. So if my drawing widget show show 750 dots (my plot is made of dots), but there are 1000 dots, I would like the slider of the ScrollBar to fill 75% of the available space.
I can't use a QScrollArea because the scroll is proportionnal with the size of the widget it contains. In my case, the scroll must be proportionnal with the number of dots on the screen. I know how to get my ratio, but I don't know how to setup correctly the QScrollBar
Example: I edited the value of PageStep, but I don't understand how this work. I can set pageStep to 100 with a range of [0,99] and it will fill the half of the QScrollBar.
My interface:
QWidget (Vertical Layout) //Main Widget
Drawing Surface (Child of QWidget)
QScrollBar (Horizontal)
Well, I think I am able to do something with this:
http://harmattan-dev.nokia.com/docs/library/html/qt4/qscrollbar.html
The relationship between a document length, the range of values used in a scroll bar, and the page step is simple in many common situations. The scroll bar's range of values is determined by subtracting a chosen page step from some value representing the length of the document. In such cases, the following equation is useful: document length = maximum() - minimum() + pageStep().
So in my case the length is the number of dots and I can set minimum() to 0. So, as you can see on the picture, to do something like QScrollArea. The proportion is: PercentageVisible = PageStep / Length and another equation is Length = PageStep + Max.
I have two equations, two missing values (PageStep and Maximum) and two known values (PercentageVisible and Length).
Example: I have 1024 dots, but only 75% of them are shown.
0.75 = PageStep / 1024 ----------> PageStep = 768
1024 = Max + 768 ----------------> Max = 256
You can try it in your software and it will works. I know there's not so much people that will needs to reproduce this because QScrollArea will do the job in most of the case.
By example, this code is in a slot reacting from a resize event:
ui.sbarRange->setPageStep(u64SampleCount * dRatio);
ui.sbarRange->setMaximum(u64SampleCount - ui.sbarRange->pageStep());
You can create a new QWidget subclass and reimplement sizeHint and paintEvent. In the paintEvent you can use event->rect() to determine which area is currently exposed and needs to be drawn. Note that paintEvent must be fast if you don't want your window to freeze. Also you need to put created widget in QScrollArea.
Here is a simple example that draws a sinusoid:
class SinWidget : public QWidget {
public:
QSize sizeHint() const {
return QSize(10000, 200);
}
void paintEvent(QPaintEvent* event) {
QPainter painter(this);
for(int x = event->rect().left(); x <= event->rect().right(); x++) {
painter.drawPoint(x, 100.0 + qSin(0.05 * x) * 20.0);
}
}
};
QScrollArea area;
area.setWidget(new SinWidget());
area.show();
This example will work fine with very large widget size (e.g. 100 000 pixels). So full repaint or memory allocation doesn't happen. You only need to keep your paintEvent fast.

Qt: heightForWidth for word-wrapped text

I have a box with a varying width and a word-wrapped text. I need to set new height every time user changes box's width. The box is displayed by QPainter inside paintEvent(QPaintEvent *) function. There is several solutions, for example current (not very smart, i do this in resizeEvent(QResizeEvent *)):
unsigned new_height = 0; // the height i want to find out.
unsigned given_width = width();
QPainter painter (this); // i need painter, because i want to ask it's default font.
QLabel lab; // the widget that can do word-wrap.
lab.setText( "A word wrapped text" ); // the text
lab.setFont( painter.font() ); // set QPainter's default font.
lab.setWordWrap( true ); // enable word-wrap
new_height = lab.heightForWidth( given_width ); // tada! :)
But the code is overkill:
1) Creading QPainter is not good outside paintEvent(QPaintEvent *);
2) BUT i need QPainter to request what font is default for it to ask metrics for that font.
Should i change my code and do this operation with help of QPainter::boundingRect() inside the paintEvent(QPaintEvent *) function? But i'd like to reduce CPU consumption inside the paintEvent(QPaintEvent *) and calculate new height only when width changed, but not every time it displayed.
What is other solutions for the purpose of subject? QFontMectircs?
I think you have the right idea of using QFontMetrics. The whole idea of the class is to assist the situations like you have here. Take a look at QFontMetricsF::boundingRect()
Use your target paint rectangle as the input rect, but set the height to the max that your widget height. I'd just put something like INT_MAX in it just to be sure. :)

Calculate QGraphicsTextItem font size based on scale

I have QGraphicsTextItem objects on a QGraphicsScene. The user can scale the QGraphicsTextItem objects by dragging the corners. (I am using a custom "transformation editor" to do this.) The user can also change the size of the QGraphicsTextItem by changing the font size from a property panel. What I would like to do is unify these so that when the user scales the object by dragging the corner with the mouse, behind the scenes it actually is calculating "What size font is necessary to make the resulting object fit the target size and keep the scale factor at 1.0?"
What I am doing now is letting the object scale as normal using QGraphicsItem::mouseMoveEvent and then triggering a FinalizeMapScale method in QGraphicsItem::mouseReleaseEvent once the mouse scale is complete. This method should then change the font to the appropriate size and set the scale back to 1.0.
I have a solution that appears to be working, but I'm not crazy about it. I'm relatively new to both Qt and C++, so would appreciate any comments or corrections.
Is there a better way to architect this whole thing?
Are there Qt methods that already do this?
Is my method on the right track but has some Qt or C++ errors?
Feel free to comment on my answer below on submit your own preferred solution. Thanks!
[EDIT] As requested in comment, here is the basics of the scaling code. We actually went a different direction with this, so this code (and the code below) is no longer being used. This code is in the mouseMoveEvent method, having previously set a "scaling_" flag to true in mousePressEvent if the mouse was clicked in the bottom-right "hot spot". Note that this code is in a decorator QGraphicsItem that holds a pointer to the target it is scaling. This abstraction was necessary for our project, but is probably overkill for most uses.
void TransformDecorator::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
...
if (scaling_) {
QGraphicsItem *target_item = target_->AsQGraphicsItem();
target_item->setTransformOriginPoint(0.0, 0.0);
QPointF origin_scene = mapToScene(target_item->transformOriginPoint());
QPointF scale_position_scene = mapToScene(event->pos());
qreal unscaled_width = target_item->boundingRect().width();
qreal scale_x = (scale_position_scene.x() - origin_scene.x()) / unscaled_width;
if (scale_x * unscaled_width < kMinimumSize) {
scale_x = kMinimumSize / unscaled_width;
}
target_item->setScale(scale_x);
} else {
QGraphicsObject::mouseMoveEvent(event);
}
}
Please no holy wars about the loop-with-exit construct. We're comfortable with it.
void MapTextElement::FinalizeMapScale() {
// scene_document_width is the width of the text document as it appears in
// the scene after scaling. After we are finished with this method, we want
// the document to be as close as possible to this width with a scale of 1.0.
qreal scene_document_width = document()->size().width() * scale();
QString text = toPlainText();
// Once the difference between scene_document_width and the calculated width
// is below this value, we accept the new font size.
const qreal acceptable_delta = 1.0;
// If the difference between scene_document_width and the calculated width is
// more than this value, we guess at the new font size by calculating a new
// scale factor. Once it is beneath this value, we creep up (or down) by tiny
// increments. Without this, we would sometimes incur long "back and forth"
// loops when using the scale factor.
const qreal creep_delta = 8.0;
const qreal creep_increment = 0.1;
QScopedPointer<QTextDocument> test_document(document()->clone());
QFont new_font = this->font();
qreal delta = 0.0;
// To prevent infinite loops, we store the font size values that we try.
// Because of the unpredictable (at least to me) relationship between font
// point size and rendering size, this was the only way I could get it to
// work reliably.
QList<qreal> attempted_font_sizes;
while (true) {
test_document->setDefaultFont(new_font);
delta = scene_document_width - test_document->size().width();
if (std::abs(delta) <= acceptable_delta ||
attempted_font_sizes.contains(new_font.pointSizeF())) {
break;
}
attempted_font_sizes.append(new_font.pointSizeF());
qreal new_font_size = 0.0;
if (std::abs(delta) <= creep_delta) {
new_font_size = delta > 0.0 ? new_font.pointSizeF() + creep_increment
: new_font.pointSizeF() - creep_increment;
} else {
new_font_size = new_font.pointSizeF()
* scene_document_width
/ test_document->size().width();
}
new_font.setPointSizeF(new_font_size);
}
this->setFont(new_font);
this->setScale(1.0);
}
Another way to look at the problem is: Qt has scaled the font, what is the effective font size (as it appears to the user, not the font size set in the text item) that I need to display to the user as their choice of new font size? This is just an alternative, you still need a calculation similar to yours.
I have a similar problem. I have a text item that I want to be unit size (one pixel size) like my other unit graphic items (and then the user can scale them.) What font (setPointSize) needs to be set? (Also what setTextWidth and what setDocumentMargin?) The advantage of this design is that you don't need to treat the scaling of text items different than the scaling of any other shape of graphics item. (But I don't have it working yet.)
Also, a user interface issue: if the user changes the font size, does the item change size? Or does it stay the same size and the text wrap differently, leaving more or less blank space at the end of the text? When the user appends new text, does the font size change so all the text fits in the size of the shape, or does the shape size grow to accommodate more text? In other words, is it more like a flowchart app (where the shape size is fixed and the font shrinks), or like a word processor app (where the font size is constant and the shape (number of pages) grows?