QLabel consuming too much space - qt

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

Related

How to align two widgets in a QHBoxLayout where one is aligned far left, and one is aligned far right?

I want to have a QHBoxLayout where one QLabel is on the far left, the one QLabel is on the far right.
My Google-fu fails me. :( I cannot find the solution.
Here is a screenshot of the QHBoxLayout with two QLabel widgets:
Whatever I try, I cannot get the second QLabel widget aligned on the far right.
Roughly speaking, I tried something like this:
QHBoxLayout* const hboxLayout = new QHBoxLayout{};
hboxLayout->addWidget(m_leftLabel, 1);
hboxLayout->addStretch(1);
hboxLayout->addWidget(m_rightLabel, 0, Qt::AlignmentFlag::AlignRight);
I tried various large stretch values for the first addWidget() call and addStretch().
I also tried:
m_rightLabel->setAlignment(Qt::AlignmentFlag::AlignRight)
None of these solutions works. I am sure the solution is very simple ( ! ), but I cannot find it.
How can I do this?
My solution is to set a stretch in the middle:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QHBoxLayout *lay = new QHBoxLayout(&w);
lay->addWidget(new QLabel("Left"));
lay->addStretch();
lay->addWidget(new QLabel("Right"));
w.show();
return a.exec();
}

How to play a video on a translucent QWidget?

I want to play a movie on a QWidget that has Qt::FramelessWindowHint flag and Qt::WA_TranslucentBackground attirbute using QVideoWidget or QGraphicsVideoItem. But the video is not visible. I hear only sound. What is the problem?
Edit:
#include "videoplayer.h"
#include <QtWidgets/QApplication>
#include "qboxlayout.h"
#include "qvideowidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *w = new QWidget;
w->setWindowFlags(Qt :: Window | Qt::FramelessWindowHint );
w->setAttribute(Qt::WA_TranslucentBackground, true);
w->setMinimumSize(300,200);
QVideoWidget *videoWidget = new QVideoWidget;
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(videoWidget);
w->setLayout(controlLayout);
QMediaPlayer mediaPlayer;
mediaPlayer.setVideoOutput(videoWidget);
mediaPlayer.setMedia(QUrl::fromLocalFile("C:/1.wmv"));
videoWidget->show();
mediaPlayer.play();
w->show();
return app.exec();
}
I solve the problem .when we set the WA_TranslucentBackground flag and FramelessWindowHint attribute the QVideoWidget's QPainter going to QPainter::CompositionMode_DestinationOver mode and it cause to nothing to show or a shadow on the screen . in this case i use a custom video widget and in paintEvent after createing QPainter painter(this); add
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination); or
painter.setCompositionMode(QPainter::RasterOp_SourceAndDestination);
to change the composition mode.
I've implemented VideoWidget some time ago. The only thing you shall change is your video path and set FramelessWindowHint flag.
You can find source here.

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

How to set dock widget below widget

I want to set dock widget at place below a widget as in figure
But when I am setting geometry as start point of Y to equivalent the gap size but it is not working.
I think position of QDockWidget should be handled different way, then setting its geometry.
This example shows how QDockWidgets are positioned as you want + one more dock widget tabbed together with another one.
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Main window
QMainWindow *window = new QMainWindow;
window->setDockNestingEnabled(true);
// Anything to add as central widget
QWidget *widget = new QWidget;
window->setCentralWidget(widget);
// First dock in the left top corner
QDockWidget *dockWidget = new QDockWidget("--- 1 ---");
window->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
// Second dock below first one
QDockWidget *dockWidget2 = new QDockWidget("--- 2 ---");
window->addDockWidget(Qt::LeftDockWidgetArea, dockWidget2);
// Third in tab with second one
QDockWidget *dockWidget3 = new QDockWidget("--- 3 ---");
window->tabifyDockWidget(dockWidget2, dockWidget3);
window->show();
return a.exec();
}
QMainWindow Has several functions how to handle spliting, moving or tabbing QDockWidgets

Qt alignment in QGridLayout eliminates the resizing of its elements

Ok, so basically I have a simple table with a QWidget and two buttons as shown below:
QGridLayout *layout = new QGridLayout;
layout->addWidget(viewcontainer,0,0,1,2);
layout->addWidget(reset,1,0);
layout->addWidget(done,1,1);
This is basically what I want, where "reset" and "done" are buttons. Essentially it's a QWidget, viewcontainer, which resizes as the window size is changed by the user while the buttons' heights remains the same. But, the default for the gridlayout is to align the contents to the left. If I change this with:
layout->addWidget(viewcontainer,0,0,1,2, Qt::AlignCenter);
It does sort of what I want, but the graphicsscene no longer resizes (remains a small constant size). I'd like to retain the resizing while just aligning the widget to the center. Thanks.
I think the easiest solution which provides a clean solution is to nest 2 layouts.
Your 'outer' (parent) layout should be a QHBoxLayout and you can add your QGridLayout into it as an 'inner' (child) layout with addLayout().
Based on my experience you should avoid to set Qt::Alignment every time you can. It can really mess up your layout. For simple layouts it can work but for more complex ones you should avoid it. And you never know that you should extend your layout in the future or not so my suggestion is to use nested layouts.
Of course you can create a QWidget for the 'outer' layout and for the 'innser' layout as well but most of the times it should be fine to just nest 2 layouts.
Also you can use QSpacerItem to fine-tune your layout.
Have a look at this example code, I think it does what you want:
#include <QApplication>
#include <QPushButton>
#include <QGraphicsView>
#include <QGridLayout>
#include <QPalette>
class MyWidget : public QWidget
{
public:
MyWidget()
{
QGridLayout * layout = new QGridLayout(this);
QGraphicsView * gv = new QGraphicsView;
layout->addWidget(gv, 0,0, 1,2);
layout->setRowStretch(0, 1); // make the top row get more space than the second row
layout->addWidget(new QPushButton("reset"), 1,0);
layout->addWidget(new QPushButton("done"), 1,1);
}
};
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}

Resources