hide QLabel based on text-size - qt

I would like to write a custom QLabel subclass with some more features for responsive design. In thisexample, I want to write a QLabel which scales the text based on the useable space. This is quite easy but also has some problems because of Qt-intern stuff. (I have to scale the text to 0.9 of the useable space, otherwise resizing the window / widget gets buggy)
Now I wan't to add a way to hide the label completely when the font size is bellow a specific threshold. However, this seems to be quite a complex task.
Here is what I have sofar in the classes resizeEvent(QResizeEvent *event) function.
Right now, my function only sets the text to "" when the size would be bellow the threshold.
void CustomLabel::resizeEvent (QResizeEvent * event ) {
if(autoFontResize) {
this->setSilentText(labelText); // just the normal setText function, I overwrote it for the subclass
QFont f = this->font();
int flags = Qt::TextDontClip|Qt::TextWordWrap;
QRect fontBoundRect = this->fontMetrics().boundingRect(this->rect(), flags, this->text());
float xFactor = (float)event->size().width() / (float)fontBoundRect.width();
float yFactor = (float)event->size().height() / (float)fontBoundRect.height();
float factor = xFactor < yFactor ? xFactor : yFactor;
f.setPointSizeF(f.pointSize()*factor*0.9); //
if(minimumFontSize != 0) { // 0 = no minimum Size for the font
if(f.pointSize() < minimumFontSize) {
if(hideFontOnMinimum) { // either Hide or set to the limit size
this->setSilentText(""); //replace text
} else {
f.setPointSizeF(minimumFontSize);
}
}
}
this->setFont(f);
}
QLabel::resizeEvent(event);
}
By the way, some parts of the code are found on stackoverflow, not mine. ;)
What I would like to do is to completely hide() the label. However the label doesn't know when It can show() again since the resizeEvent doesn't seem to be called after that.
Any ideas?
Thanks!

As you've noticed, if you call hide() on the widget it fails to receive a resize event. Since you're customising the class anyway, rather than calling hide(), you could just set a class variable to note that it's hidden and overload the paintEvent function, not to draw the widget if the variable is set: -
void CustomLabel::paintEvent(QPaintEvent * event)
{
if(m_hideOnMinimum)
return;
QLabel::paintEvent(event);
}
Note that by not painting the label, it will be hidden, but the user may still be able to interact with it, so you will need to disable it or overload keyboard / mouse events too.

Related

Positioning QGraphicsItem in the middle of viewport - jittering

I have a huge QgraphicsScene and a QGraphicsItem which changes its position according to pressed key arrows. I want the item to always be in the centre of the viewport. I used QGraphicsView::centerOn() and it's exactly what i need, but there is jittering problem - and as i read it's associated with the integer precision issue. So what's the way around? Another post says to use QGraphicsView::translate() and setTransformationAnchor(QGraphicsView::NoAnchor). But it doesnt seem to work.
Game::Game()
{
player = new QGraphicsRectItem();
m_view = new QGraphicsView(this);
m_view->setSceneRect(QRectF(0,0,11800,11600));
this->setBackgroundBrush(Qt::black);
m_view->setScene(this);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
m_view->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
m_view->show();
player->setRect(0,0,100,100);
player->setBrush(Qt::red);
player->setPos(400,300);
player->setFocus();
this->addItem(player);
player->setZValue(10);
player->setTransformOriginPoint(player->boundingRect().center());
customRect = new CustomRect;
customRect->setPos(400,300);
this->addItem(customRect);
}
void Game::advance()
{
m_view->centerOn(player);
}

Inside a QWidget, Qwt plot is there then if i am trying to resize widget simultaneously plot should resize. how to apply resizeevent to QWidget

I have applied resizeEvent to a QWidget. I am trying to resize the widget inside another widget. I have a QWT plot that should also resize.
I have applied resizeEvent to the QWidget and resized to the plot:
void SampleWidget:: resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
int diffofWidth = event->size().width() - event->oldSize().width();
int diffofHeight = event->size().height() - event->oldSize().height();
QSize size = plot->size();
if ((event->oldSize().width() >= 0) && (event->oldSize().height() >= 0))
{
if (event->size().width() <= this->minimumWidth() && event->size().height() <= this->minimumHeight())
{
return;
}
else
{
plot->resize(size.width() + diffofWidth, size.height() + diffofHeight);
plot->replot();
}
}
}
The widget is able to resize, but after resizing many times, the plot is only displayed partially in the widget. I am not able to see whole content in the widget. What's going on here?
Assuming you are asking why your widget isn't resizing properly, you likely have minimum sizes set within the layouts. Be sure that your minimum size of the parent is greater than or equal to the sum of it's children, that could cause the child elements to be partially hidden.
Also, check sizehint and the minimum size set for each object, increase them as needed.

Qt QLabel fails to resize

I implemented QLabel much like Qt's ImageViewer example, except I use QGridLayout for positioning my widgets. I also implemented similar lines for scaling my QLabel using QScrollBar, but QLabel just doesn't scale like it should inside the QScrollArea. I am not sure if it is related to some kind of GridLayout management issue or something else. I have been reading everywhere and trying different things for 3 days now. Below I list the relevant portion of my code.
In my Viewer class constructor:
{
imageLabel1 = new QLabel;
imageLabel1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
imageLabel1->setScaledContents(true);
scrollArea1 = new QScrollArea;
scrollArea1->setWidget(imageLabel1);
scrollArea1->setWidgetResizable(true);
....
QGridLayout *centralLayout = new QGridLayout;
centralLayout->addWidget(scrollArea1, 0, 0);
...}
and my scaleImage method:
void Viewer::scaleImage1(int factor)
{
Q_ASSERT(imageLabel1->pixmap());
scaleFactor *= (1 + factor);
//imageLabel1->resize(scaleFactor* imageLabel1->pixmap()->size());
imageLabel1->pixmap()->toImage().scaled(scaleFactor* imageLabel1->pixmap()->size(), Qt::KeepAspectRatio, Qt::FastTransformation);
imageLabel1->adjustSize();
adjustScrollBar(scrollArea1->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea1->verticalScrollBar(), factor);
imageLabel1->update();
}
My scaleImage1 function is a public slot, and it receives signal from a scrollbar that goes between 0 and 2 so that, into the scaleFactor, the imageLabel1 is designed to be capable of being zoomed in up to 3 times its original size. But when I run the code, I don’t observe the imageLabel becoming enlarged inside the QScrollArea, which I saw in the imageViewer demo. The imageLabel1 simply retains the original size as it is loaded and does not respond to the valueChange() of scrollbar.
I'd appreciate your advice/tips very much.
I think is because you set QSizePolicy::Minimum to the imageLabel, try with MinimumExpanding or other that better fit your needs.

Don't repaint instantly after setVisble in Qt

I'm using QPushButton in my mineSweeping game.
After changing from easy mode to hard mode, the number of QPushButton is supposed to change from 9x9 to 30x16.
So, I add QPushButton with the largest number(which is of hard mode) to GridLayout in constructor of MainWindow.
btnArr = new QPushButton[HARD_WIDTH * HARD_HEIGHT]; // member element
int index = 0;
for (int i = 0; i < HARD_HEIGHT; ++i) {
for (int j = 0; j < HARD_WIDTH; ++j) {
ui->mainGrid->addWidget(&btnArr[index], i, j, 1, 1,
Qt::AlignCenter);
++index;
}
}
Then if the user change mode(e.g.: easy mode to hard mode), resetBtn(HARD_WIDTH, HARD_HEIGHT); will be called.
void MainWindow::resetBtn(const int width, const int height)
{
int index = 0;
for (int i = 0; i < HARD_HEIGHT; ++i) {
for (int j = 0; j < HARD_WIDTH; ++j) {
if (j < width && i < height) {
btnArr[index].setVisible(true);
} else {
btnArr[index].setVisible(false);
}
++index;
}
}
}
The problem is that it seems the widget repaints each time setVisible is called. So in the hard mode case, it will be called 30x16 times, which caused strange effect like this:
So how can I set the widget not repaint during this loop?
Thanks in advance.
I think that you are trying to solve the wrong problem. You shouldn't be updating the widgets like this. If you necessarily want to, then hiding the parent widget of the layout before the change and showing it again afterwards should work.
A better approach is to use QStackedWidget and have all the boards prepared initially. Switching to a different board is then simply a matter of switching the active widget.
The total number of QPushButton is really big: 30x16 = 480!!! I don't use to make people change their programming logic, but in this case I think that using QPushButtons is not the better approach. The layout must have a really bad time trying to move the objects as they are added, and perhaps you are reaching some internal limit in refresh time for the layout to be repainted.
What I would have done is a custom widget with a custom paintEvent method. There you can divide its width and height in the number of columns and rows that you wish and paint the cells with pixmaps as the game is played.
For the mouse interaction, the best would have been to override the mousePressEvent with a custom logic that calculates the mouse position in the grid and calls the corresponding methods or emits signals indicating the position of the event. Not very hard to code. You can also use the event->buttons() method to know which mouse button was pressed and emit different signals if you wish.
I don't use to answer telling that it is better to change your whole program, but in this case I think you are going "the hard way". I know this is not the kind of answer you are looking for, but consider this possibility.
You could try calling setUpdatesEnabled(false) on the parent widget before doing those "massive" changes, and re-enable it once all is done.
Maybe I'm wrong but as far as I know Qt doesn't render the widget right after setVisible() is called. Rendering happens as a result of a 'render' event, except if you call render() manually.
From the official Qt doc (http://qt-project.org/doc/qt-4.8/qwidget.html#paintEvent):
Qt also tries to speed up painting by merging multiple paint events
into one. When update() is called several times or the window system
sends several paint events, Qt merges these events into one event with
a larger region (see QRegion::united()). The repaint() function does
not permit this optimization, so we suggest using update() whenever
possible.
My instincts tell me that it's not a painting problem rather a layouting (not enough space to present every button in 'hard mode').
Also I think you shouldn't use Qt::AlignCenter when you add your buttons to the layout, it will try to centerize every button in the layout. You should rather centerize the parent widget of the layout (if you don't have one create one and centerize it) and set size-policies correctly (QWidget setSizePolicy).
But as #Mat suggested if this really is a painting problem you can use setUpdatesEnabled(false/true) (if setUpdatesEnabled solves your problem please accept #Mat 's solution)
Try to enabling/disabling instead of visible/invisible:
void MainWindow::resetBtn(const int width, const int height)
{
int index = 0;
for (int i = 0; i < HARD_HEIGHT; i++)
for (int j = 0; j < HARD_WIDTH; j++)
btnArr[index++].setEnabled(j < width && i < height);
}

Forcing an Aspect Ratio when resizing a main window

I understand that in Qt4, there is no single flag you can set to have the mouse driven resizing of the window maintain a certain aspect ratio (say, 1:1). Is there a way to force a new size for the window while in "resizeEvent (QResizeEvent * event)"? the parameters of the event don't seem to support change or give the user a chance to inject a new size.
My goal: GUI/Mouse resizing of a certain window will maintain its current aspect ratio, regardless of where you resize it from (sides, top, bottom, corners).
Quoting a message in the thread referenced by #blueskin:
This is surprisingly difficult to do. Most of the window systems do not
allow the application to set constraints on the window other than the
max and min sizes and that it can be fixed size.
The most common way to attempt this is to call resize from within the resize event. This often causes recursion problems or looks strange because the widget resizes in ways the user has not requested.
Since most window systems do not allow the main window to be resized appropriately, the best solution is often to constrain a child widget rather than the parent. One such way to do this is with a class provided by libqxt.
libqxt has support for keeping a child widget at a certain aspect ratio through its QxtLetterBoxWidget. #blueskin's answer provides one good attempt at doing what was originally requested.
If you're interested I'd recommend you read the source for the resizeWidget() function and observe the places in which it gets called. As background, libqxt uses the pimpl idiom and qxt_d() gets at the private member.
Duplicate Question?
How to maintain widgets aspect ratio in Qt?
Citing the answer by 'df' in the above link
Calling resize() from within resizeEvent() has never worked well for
me -- at best it will cause flickering as the window is resized twice
(as you have), at worst an infinite loop.
I think the "correct" way to maintain a fixed aspect ratio is to
create a custom layout. You'll have to override just two methods,
QLayoutItem::hasHeightForWidth() and QLayoutItem::heightForWidth().
Also checkout Sam Dutton's solution # http://lists.trolltech.com/qt-interest/2007-01/msg00204.html
void MyWindow::resizeEvent(QResizeEvent * /*resizeEvent*/)
{
int containerWidth = _myContainerWidget->width();
int containerHeight = _myContainerWidget->height();
int contentsHeight = containerHeight ;
int contentsWidth = containerHeight * _aspectRatio;
if (contentsWidth > containerWidth ) {
contentsWidth = containerWidth ;
contentsHeight = containerWidth / _aspectRatio;
}
resizeContents(contentsWidth, contentsHeight);
}
Another quick and simpler way to do it:
http://developer.qt.nokia.com/forums/viewthread/5320/#31866
#include <QWidget>
#include <QApplication>
#include <QSizePolicy>
class MyWidget:public QWidget
{
public:
MyWidget():QWidget(){};
~MyWidget(){};
virtual int heightForWidth ( int w ) const { return w*9/16;};
};
int main (int argv, char** argc)
{
QApplication a(argv,argc);
MyWidget w;
QSizePolicy qsp(QSizePolicy::Preferred,QSizePolicy::Preferred);
qsp.setHeightForWidth(true);
w.setSizePolicy(qsp);
w.show();
a.exec();
}

Resources