I'm trying to test animations in Qt desktop application. I just copied example from help. After button click, new button just appear in left top corner without animation (even end position is wrong). Am I missing something?
Qt 5.0.1, Linux Mint 64bit, GTK
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
QPropertyAnimation animation(button, "geometry");
animation.setDuration(10000);
animation.setStartValue(QRect(0, 0, 100, 30));
animation.setEndValue(QRect(250, 250, 100, 30));
animation.start();
}
Edit: Solved. Animation object must be as global reference. For example in section private QPropertyAnimation *animation. Then QPropertyAnimation = New(....);
You don't need to make a slot specifically for deleting the mAnimation variable. Qt can do it for you if you use QAbstractAnimation::DeleteWhenStopped:
QPropertyAnimation *mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start(QAbstractAnimation::DeleteWhenStopped);
You just didn't copied the example, you also made some changes that broke it. Your animation variable is now a local variable that is destroyed at the end of on_pushButton_clicked function. Make the QPropertyAnimation instance a member variable of the MainWindow class and use it like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), mAnimation(0)
{
ui->setupUi(this);
QPropertyAnimation animation
}
MainWindow::~MainWindow()
{
delete mAnimation;
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("Animated Button", this);
button->show();
mAnimation = new QPropertyAnimation(button, "geometry");
mAnimation->setDuration(10000);
mAnimation->setStartValue(QRect(0, 0, 100, 30));
mAnimation->setEndValue(QRect(250, 250, 100, 30));
mAnimation->start();
}
Related
I'm using this qt class to play .mp4 files as a brackground but it doesn't allow me to add buttons or anything else above.
well, it was already placed for QMediaPlayer to inherit 'centralwidget and widget' but without success, below is the code
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
videoWidget = new QVideoWidget(ui->centralwidget);
player = new QMediaPlayer;
player->setVideoOutput(videoWidget);
player->setSource(QUrl::fromLocalFile("C:/Users/felli/Desktop/teste/videoplayback (2).mp4"));
player->setLoops(QMediaPlayer::Infinite);
player->play();
videoWidget->resize(1280, 720);
videoWidget->show();
pb = new QProgressBar(videoWidget);
pb->setGeometry(QRect(370, 370, 118, 23));
pb->setValue(30);
pb->show();
The issue is that you parent the progress bar to the video widget so that it gets tied to that rather than shown on top as you seem to would like it.
This code works fine for me:
#include <QApplication>
#include <QProgressBar>
#include <QMediaPlayer>
#include <QVideoWidget>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QVideoWidget *videoWidget = new QVideoWidget();
QMediaPlayer *player = new QMediaPlayer;
player->setVideoOutput(videoWidget);
player->setSource(QUrl::fromLocalFile("C:/Users/felli/Desktop/teste/videoplayback (2).mp4"));
player->setLoops(QMediaPlayer::Infinite);
player->play();
videoWidget->resize(1280, 720);
videoWidget->show();
// Not parent it to the video widget to show it on top
QProgressBar *pb = new QProgressBar();
pb->setGeometry(QRect(370, 370, 118, 23));
pb->setValue(30);
pb->show();
return app.exec();
}
A qsplitter has been used in my application, and OpaqueResize is set to 'false',while dragging the splitter handle, the widget becoming smaller is repainted continuously, this is not right. What I thought is that the QWidgets on both sides of the splitter handle should not receive any signals about repaint or resize until mouseRelease.This issue troubles me several days, if anyone knows about how to deal with it, please give me a hand, thank you.
Qt5.11.2 on Windows10 64-bits
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// left
ui->widget_2->bgColor = QColor(255,0,0);
ui->widget_2->tag = 0;
// right
ui->widget_3->bgColor = QColor(0,0,255);
ui->widget_3->tag = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
bgColor = QColor(255,255,255);
tag = 0;
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent( event );
QPainter painter(this);
QBrush brush( bgColor );
painter.fillRect( event->rect(), brush );
qDebug() << "paintEvent:tag="<<tag;
}
Hi I am trying a very simple QGraghicsScene code in QT. But the addRect function is not working, as I try to add it to specific coordinate. It is always at the centre. I also have tried with setPos. But same result.
MainWindow::MainWindow(QWidget * parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui -> setupUi(this);
scene = new QGraphicsScene(this);
ui -> graphicsView -> setScene(scene);
QBrush greenBrush(Qt::green);
QBrush blueBrush(Qt::gray);
QPen outlinePen(Qt::black);
outlinePen.setWidth(2);
scene -> update();
rectangle = scene -> addRect(20,200,100,100,outlinePen,greenBrush);
scene - >update();
}
MainWindow::~MainWindow()
{
delete ui;
}
I'm using the answer to this SO question to make a custom image widget which automatically scales correctly. It works fine but now I am trying to center the image widget instance at the center of my main window.
My idea was to create a QHBoxLayout, add the image widget to that and then add the hBox instance to the ui->verticalLayout.
Doesn't work. The image still displays flush left with the error message: QLayout: Attempting to add QLayout "" to MainWindow "MainWindow", which already has a layout
I then tried a few variations on 'setAlignment` but then the image doesn't appear at all. My simple test code is below.
What am I missing here?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix;
pix.load("/Users/home/Desktop/test.jpg");
ImageLabel2* image = new ImageLabel2(this);
image->setPixmap(pix);
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addWidget(image);
// hbox->setAlignment(image,Qt::AlignCenter);
hbox->setAlignment(Qt::AlignHCenter);
// ui->verticalLayout->addLayout(hbox);
ui->verticalLayout->addLayout(hbox);
// ui->verticalLayout->addWidget(image);
// ui->verticalLayout->setAlignment(image,Qt::AlignCenter);
// ui->verticalLayout->setAlignment(Qt::AlignHCenter);
}
Try this:
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addStretch();
hbox->addWidget(image);
hbox->addStretch();
None of the suggestions here worked for me. Don't know why and it seems hard to debug layout problems
However a reply to my question on the Qt Project.org site works perfectly. So not my solution but I am posting it here since this "centering/resizing image" issue seems a common problem.
class CustomWidget : public QWidget {
public:
CustomWidget(const QPixmap &p, QWidget* parent = 0)
: QWidget(parent), pixmap(p) {}
void paintEvent(QPaintEvent * e)
{
QRect srcRect(QPoint(), pixmap.size());
QSize dstSize = srcRect.size().scaled(
e->rect().size(), Qt::KeepAspectRatio);
QRect dstRect(QPoint((width() - dstSize.width())/2,
(height() - dstSize.height())/2), dstSize);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.drawPixmap(dstRect, pixmap, srcRect);
}
private:
QPixmap pixmap;
};
and then in the main window:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(new CustomWidget(QPixmap("test.png")));
}
main window(QMainwindow) API setCentralWidget to align the widget on center.
Note:Concept of centrewidget, is to differentiate the docking area( Left, Right, Bottom, Top ).
with out a centre how some one know who is where. when we are developing with QMainWindow, you can see in UI_MainWindow.h, setting Centrewidget with dummy QWidget
Below code will work
#include "mainwindow.h"
#include <QLabel>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
QPixmap pix;
pix.load("C:\\Users\\user\\Desktop\\Uninstallation failure2.png");
//Replace with ImageLabel2
QLabel* image = new QLabel(this);
image->setPixmap(pix);
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addWidget(image);
QWidget* centreWidget = new QWidget();
//QMainwindow, having a feature called centreWidget, to set the layout.
centreWidget->setLayout( hbox );
setCentralWidget( centreWidget );
}
MainWindow::~MainWindow()
{
}
hi i create a dockwidget which i am starting it as hidden. The problem is that afterwards i cannot show it, while i can get the status correctly from the isHidden() function. The weird thing is that if i start the dockwidget not hidden, it works perfect. I am including an example that reproduces this strange behaviour.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void showDock();
private:
Ui::MainWindow *ui;
QDockWidget *dock;
QPushButton *button;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// QMainWindow mainWindow;
// QDockWidget *dock = new QDockWidget(&mainWindow);
QDialog *dockDialog = new QDialog(this); // <---------edit: you need to create a parent widget for the dock
dock = new QDockWidget(dockDialog);
dock->setStyleSheet("QDockWidget { font: bold }");
dock->setFloating(true);
dock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
QAbstractButton *floatButton = dock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
if(floatButton)
floatButton->hide();
dock->setAllowedAreas( Qt::NoDockWidgetArea );
dock->setWindowTitle("Tools");
this->addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
QMainWindow *window = new QMainWindow(dock); // <------edit: set the dock to be the parent for the window
window->setWindowFlags(Qt::Widget);
QToolBar *bar = new QToolBar(window);
bar->setMovable(false);
bar->addAction("Select");
bar->addAction("Polygon");
bar->addAction("Brush");
bar->addAction("Erazer");
bar->addSeparator();
bar->addAction("Mark");
bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
bar->setOrientation(Qt::Vertical);
window->addToolBar(Qt::LeftToolBarArea, bar);
window->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
window->setParent(dock);
dock->setWidget(window);
dock->hide(); // <------------ comment this line and it will work, edit: you do not need to do that anymore, it is working nicely now
button = new QPushButton("show", this);
button->setCheckable(true);
QObject::connect(button, SIGNAL(clicked()), this, SLOT(showDock()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showDock()
{
// qDebug() << "hello";
if(button->isChecked()){
if(dock->isHidden()){
qDebug() << "hidden";
dock->setFloating(true); // <-----edit: you need to add these lines in order to be able to see the dialog that contains the dock widget, i do not know why i need to that again since in the initialization i already specifying that my dock is floatable
QAbstractButton *floatButton = dock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton"); // <---------edit: add this lines in order to get rid off the floating button
if(floatButton)
floatButton->hide(); // <----edit: the same as previous
dock->show();
}
}
if(!button->isChecked()){
if(!dock->isHidden()){
qDebug() << "not hidden";
dock->hide();
}
}
}
as it is now the above code the dockwidget is not appearing in the screen. If you try to comment the line that i am specifying in the mainwindow.cpp it works, but the point is that i want to start the dockwidget hidden. Does someone have any idea, of what is happening.
Thanks.
Parent of the QDock control should be your window widget, and not object of the MainWindow class.
So, you need to replace this:
dock = new QDockWidget(this);
with this:
QMainWindow *window = new QMainWindow(0); // I smell a potential leak here (set the parent!)
dock = new QDockWidget(window);
Control is visible when you press Show button in the first case, too, but it is somewhere outside the window, so you may not see it. Also, there is no need to create separate instance of QMainWindow inside class that is already inheriting QMainWindow.
I see you show()/hide() your dockwidget. I haven't tried it myself but maybe you should hide and show the dockwdiget with the QDockWidget::toggleViewAction method.