I'm trying to have animation on a button click event. But somehow the animation is not working. I have referred the Qt reference docs, but could not find the root cause which is causing the issue
Below is sample code :
void MainWindow::AnimationClick()
{
// define toolbar y movement positions for animation
TOOLBAR_Y_SHOWN = 0;
TOOLBAR_Y_HIDDEN = -m_AnimatedWidget->height();
m_AnimatedWidget = new AnimatedWidget(this);
QPropertyAnimation *m_ani = new QPropertyAnimation(m_AnimatedWidget, "pos", this);
m_ani->setDuration(500);
m_ani->setEndValue(QPoint(m_AnimatedWidget->pos().x(), TOOLBAR_Y_HIDDEN));
m_ani->setEasingCurve(QEasingCurve::InBack);
m_ani->start();
}
With the above implementation nothing is happening on the click event.
Any suggestions , Thanks.
This looks wrong:
TOOLBAR_Y_HIDDEN = -m_AnimatedWidget->height();
m_AnimatedWidget = new AnimatedWidget(this);
First you access m_AnimatedWidget then you allocate it?
When you get a crash, such as segmentation fault, always run your program in a debugger. It would have helped you find this error quite easy as it would have stopped on the line of the error.
m_ani->setDuration(500);
setDuration() argument is expressed in milliseconds. You should probably put more than half a second when you are testing.
I got it. I was not allowing the m_AnimatedWidget to show upon the screen.
Below is the edited snippet.
void MainWindow::AnimationClick()
{
// define toolbar y movement positions for animation
TOOLBAR_Y_SHOWN = 0;
m_AnimatedWidget = new AnimatedWidget(this);
TOOLBAR_Y_HIDDEN = -m_AnimatedWidget->height();
QPropertyAnimation *m_ani = new QPropertyAnimation(m_AnimatedWidget, "pos", this);
m_ani->setDuration(5000);
m_ani->setEndValue(QPoint(m_AnimatedWidget->pos().x(), TOOLBAR_Y_HIDDEN));
m_ani->setEasingCurve(QEasingCurve::InBack);
m_ani->start();
m_AnimatedWidget->show();
}
Related
I created a QCamera, and a QVideoProbe, which will allow me to access each frame of the video.
The problem is that every time I close the app, I get this message in Qt Creator: 'QVideoProbe control destroyed while it's still being referenced!!!'.
I'm using Qt 5.11 on Windows 8.1. My compilator is MSVC 2015 (32-bit).
Here's my code:
Window::Window()
{
if(!checkCameraAvailability())
{
QMessageBox::critical(this, "Error", "No camera is available");
return;
}
m_camera = new QCamera;
m_camera->setCaptureMode(QCamera::CaptureVideo);
m_videoProbe = new QVideoProbe(this);
if(!m_videoProbe->setSource(m_camera))
{
QMessageBox::critical(this, "Error", "setSource");
return;
}
connect(m_videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(cameraFrameProbed(QVideoFrame)));
// For now the slot 'cameraFrameProbed' is empty
m_camera->start();
}
What did I miss?
Problem solved! I replaced this line:
m_videoProbe = new QVideoProbe(this);
with this one:
m_videoProbe = new QVideoProbe;
But I still don't understand what was wrong with the first line.
EDIT: The previous solution has a memory leak! Here's the correct one:
I forgot to mention the parent of the QCamera when constructing it, there were actually 2 memory leaks...
So the code becomes:
m_camera = new QCamera(this);
m_videoProbe = new QVideoProbe(this);
And now it works!
Thank you so much Jeremy Friesner!
I've got a button that I need to be a toggle button for the sound of a game. I'm using the MenuSpriteItem class.
auto menuSoundOn = Sprite::createWithSpriteFrameName("soundOn.png");
auto menuSoundOff = Sprite::createWithSpriteFrameName("soundOff.png");
auto menuSoundBtn = MenuItemSprite::create(menuSoundOn, menuSoundOff, CC_CALLBACK_1(LevelsLayer::shutSound, this));
menuSoundBtn->setTag(0);
_mainMenu = Menu::create(menuSoundBtn, nullptr);
this->addChild(_mainMenu);
//Then in my shutSound method
auto menuSoundBtn = _mainMenu->getChildByTag(0);
if (_ifSound){
_ifSound = false;
//Do some stuff to shut the sound
menuSoundBtn->setSelectedImage("noSound.png");
}
else{
_ifSound = true;
//Do some stuff to bring the sound back
menuSoundBtn->setSelectedImage("sound.png");
}
The problem is that getting the Btn from his parent with getChildByTag(0) method I receive a Node according with the documentation, but setSelectedImage is not part of the Node class and there is an error telling me so, so what is the right way to access MenuSpriteItems from their Parents and then manipulate them as in this case by changing the Normal Image?
Greetings.
I've got the answer and It's really powerful and simple.
auto menuSoundBtn = dynamic_cast<MenuItemSprite*>(_mainMenu->getChildByTag(0));
This is the explanation from the guy:
This code will get the child with tag 0 and turn it into a MenuItemSprite* object if it is a MenuItemSprite* object, or it returns null if the object was not a MenuItemSprite*.
Hope it helps someone. Greetings.
I have a QDialog and it pops up as a top level window in the center of my main window the first time it is opened(great, exactly what I want). However, each time I hit the "X" to close the window, when I trigger an event to cause the QDialog to pop up again(I use show() function for this)...the widget has slightly moved down and to the right(maybe 10 pixels each way, each time). Does anyone know what is causing this behavior/have a solution? Ideally I would like it to always pop up in the center of my main window(like it does the first time it opens).
Thanks in advance.
commandTimeWindow = new QDialog();
commandTimeWindow->resize(390, 180);
commandTimeWindow->setWindowTitle("Command In Progress");
commandTimeWindow->setStyleSheet("background-color: white;");
commandTimeWindow->setWindowFlags(Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
commandTimeWindow->setWindowIcon(QIcon(""));
commandTimeWindow->installEventFilter(this);
commandTimeWindow->close();
commandTimeWindow->show();
Also the standard "X" button closes the window...but I didnt write any code on this end.
You can use the widget's move(x, y) method or replace resize with setGeometry.
I was able to solve my issue using the code below:
void MyMainWindow::moveEvent(QMoveEvent* event)
{
const QPoint global = this->mapToGlobal(rect().center());
waitDialog->move(global.x() - waitDialog->width() / 2, global.y() - waitDialog->height() / 2);
}
I'm writing simple pomodoro application, which is basically countdown timer. Right now, I've got countdown working, but the weird thing is that when I add another attribute to my class (arbitrary), I get Sedmentation fault error.
Using gdb, the problem should be here:
void Status::showPomodoroTime() {
QTime time = pomodoroTime->addSecs(elapsed);
activeTime->display(time.toString("mm:ss"));
}
where activeTime is QLCDNumber widget and elapsed is int.
More context:
void Status::createDefaultIntervals()
{
pomodoroInterval = new QTime(0, 25);
pomodoroBreak = new QTime(0, 5);
pomodoroLongBreak = new QTime(0, 15);
}
void Status::run()
{
if (pomodoroActive == STOP) {
pomodoroTime = pomodoroInterval;
showPomodoroTime();
}
pomodoroActive = RUN;
updateStatusArea();
timerTick();
}
CreateDefaultInterval definitely runs before showPomodoroTime.
What bugs me, that whole application works fine. Just when I add another attribute, it starts to throw sedfault.
How can variable declaration in *.h file cause segfault in *.cpp?
If you want more code, I can put it anywhere. I just don't know, what place is persistent enough. Don't want to post it here (about 300 lines of code).
check if(pomodoro!= NULL) and then do addSecs().
pomodoroTime is probably uninitialized or deleted
So the non-QMdiArea version of my code,
MyWidget::MyWidget(QWidget* parent)
{
...
layout()->setSizeConstraint( QLayout::SetFixedSize );
}
MainWindow::MainWindow(...)
{
...
MyWidget* wgt = new MyWidget(NULL);
wgt->show();
}
works just fine and produces a widget that the user can't resize. But when the MainWindow code is replaced with
MainWindow::MainWindow(...)
{
...
MyWidget* wgt = new MyWidget(ui->mdiArea); //Or MyWidget(NULL), same result
ui->mdiArea->addSubWindow(wgt);
}
the window, now within the QMdiArea, is resizeable. It doesn't seem to be an issue of Qt::WindowFlags, they don't handle resize policy. Surely there is a way to do this? NB I cant use something like setFixedSize(ht, wd) since the size of the widget can change programmatically (subwidgets are added and removed). But the user should not be able to resize it.
Even though MyWidget is not resizeable, when you call:
ui->mdiArea->addSubWindow(wgt);
The widget is put in a QMdiSubWindow which is resizeable. All you have to do is get the window that's created and fix its size:
QMdiSubWindow* subWindow = ui->mdiArea->addSubWindow(wgt);
subWindow->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
This should work, but I haven't tried this code myself.
EDIT: well... apparently that doesn't fix the size. :(
The following worked for me:
MyWidget* wgt = new MyWidget(ui->mdiArea);
QMdiSubWindow* subWindow = ui->mdiArea->addSubWindow(wgt);
subWindow->setFixedSize(wgt->size());
wgt->show();