Qt - How to show gif(animated) image in QGraphicsPixmapItem - c++

I am trying to use one blinking image in QGraphicsPixmapItem. The image has shown without the animation effect. The below is the original image, the following is the QGraphicsScene which uses this image in QGraphicsPixmapItem. Can anybody say how to achieve this?.

use this code
QGraphicsScene scene;
QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie(image);
gif_anim->setMovie(movie);
movie->start();
QGraphicsProxyWidget *proxy = scene.addWidget(gif_anim);

Related

Change border color of QWidget using animation

I have tried to change border color of Qwidget for two days but Doesn't work.
so I refered to another code which is posted on the stackoverflow about moving widget. it worked perpectly but I can't change border color using QPropertyAnimation. give some tips for this problem.
QPropertyAnimation *animation = new QPropertyAnimation(ui.defectView, "border-color");
animation->setDuration(2000);
animation->setStartValue(QColor(0, 0, 0));
animation->setEndValue(QColor(240, 240, 240));
animation->start();
There is no QWidget property called "border-color". All it's properties are shown in QWidget documentation. But you can change this color using QSS and animate it. Also consider custom paintEvent depending on your needs.

Change QT layout background

Since QVBoxLayout has no a setStylesheet method, I thought this would made the trick:
QWidget *window = new QWidget(this);
window->setStyleSheet("background-image:url(:/images/sky.jpg);font-size:18px;");
QVBoxLayout * layout = new QVBoxLayout(window);
layout->addWidget(widg1);
layout->addWidget(widg2);
setLayout(layout);
Sadly, only a small rectangle of background image appears, not covering entire window. How could I do it?
You can set stylesheet to the central widget of your main window. In the example you can put window into some other layout.

How do I just put header and some content under it in Qt Designer?

QtCreator's designer allows you to edit user interface graphically. I was just trying to make some sense of it - what I wanted was a header text centered in the middle and some widget under it, like this:
But my results look like this, when using vertical layout:
I placed QLabel on top and QOpenGLWidget on bottom - I only used QOpenGlWidget because it has black background on screenshot. What I really plan on doing is using another QWidget. I used vertical layout then. So how do I get the result on the first image, using QLabel and QWidget?
Qt's use of XML is not a replacement for HTML.
HTML is designed for marking up web pages. Qt's widgets are not web pages!
It appears that you're looking at the box with the text 'content' and thinking of a generic widget. I see that and see a QLabel, which is derived from QWidget after all.
It's probably easier to explain in code how I would go about this, which you can then translate to doing the same in Qt Creator.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a root widget (this could also be a QMainWindow, or any other widget)
QWidget* pWidget = new QWidget;
// Layout to arrange the widgets vertically
QVBoxLayout* pBoxLayout = new QVBoxLayout;
pWidget->setLayout(pBoxLayout);
// The header widget
QLabel* pHeader = new QLabel("Header");
pHeader->setAlignment(Qt::AlignCenter);
pHeader->setMinimumSize(200, 20);
pHeader->setMaximumSize(200, 20);
QFont font = pHeader->font();
font.setBold(true);
font.setPixelSize(16);
pHeader->setFont(font);
// the content widget
QLabel* plabel = new QLabel("content");
plabel->setMinimumSize(200, 200);
plabel->setMaximumSize(200, 200);
plabel->setStyleSheet("background-color: rgb(182, 182, 182); border: 5px solid black;");
plabel->setAlignment(Qt::AlignCenter);
pBoxLayout->addWidget(pHeader);
pBoxLayout->addWidget(plabel);
pWidget->show();
return a.exec();
}
As you can see here, I've styled the content widget with the use of a Style Sheet. This is really the easiest method, after a bit of practice with them.
The resulting code produces a widget which looks like this: -
You can play with dimensions and fonts to match your original image exactly.

How to apply a drop shadow effect to QGraphicsItem?

I am new to QT and I want to draw shadow for my QGraphicsRectItem but it doesn't seem to be working
I use following code in my QGraphicsScene
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(50);
QGraphicsRectItem * item = new QGraphicsRectItem(100,100,50,50);
item->setBrush(QBrush(Qt::cyan));
item->setGraphicsEffect(effect);
scene->addItem(item);
However, this works fine if I apply this to my graphicsView like this
ui->graphicsView->setGraphicsEffect(effect);
What am I doing wrong?
Probably you create effect locally and this pointer is erased, so move declaration of effect to header file.

Qt: issue with QGraphicscene to show an imge

I am trying to develop very simple image viewer using QT. I am using the following code to show the image on QGraphicsScene widget:
QImage image(fileName);
firstPicture = new QGraphicsScene();
firstPicture->addPixmap(QPixmap::fromImage(image));
ui->graphicsView->setScene(firstPicture);
ui->graphicsView->fitInView(firstPicture->sceneRect() ,Qt::KeepAspectRatio);
I am getting the following output:
How can I fit the image into the GraphicsScene?
This approach works only when view showed your scene. In your case you did not call show(). Solution:
Use your approach when scene already shown.
You can reimplement showEvent and use your approach here.
You can scale image by yourself and set this scaled image to scene.
Also you can try to use: Qt::IgnoreAspectRatio instead of Qt::KeepAspectRatio.
You can make your custom class which inherits from QGraphicsView. You should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like:
void MyView::resizeEvent(QResizeEvent *event)
{
fitInView(firstPicture->sceneRect(), Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(event);
}
This way the view will always display the whole scene. I.e. if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.