QPushbutton movable Dynamically
-
I understood saving of old position and new position, but how to restore i.e hoe to set button old position to restore?
-
I have used the same, but it's not working.
void MainWindow::resizeEvent(QResizeEvent* event)
{
qDebug() << "old size" << event->oldSize() << " to " << event->size();
int oldx,oldy;
oldpos=ui->pushButton->geometry();
oldx=oldpos.x();
oldy=oldpos.y();
qDebug()<<"oldpos x is"<<oldx<<"oldpos y is:"<<oldy;
if (!firstTime) {
ui->pushButton->move(50,100);
}else { ui->pushButton->move(oldx,oldy); firstTime = false; } QMainWindow::resizeEvent(event);
}
-
I have used the same, but it's not working.
void MainWindow::resizeEvent(QResizeEvent* event)
{
qDebug() << "old size" << event->oldSize() << " to " << event->size();
int oldx,oldy;
oldpos=ui->pushButton->geometry();
oldx=oldpos.x();
oldy=oldpos.y();
qDebug()<<"oldpos x is"<<oldx<<"oldpos y is:"<<oldy;
if (!firstTime) {
ui->pushButton->move(50,100);
}else { ui->pushButton->move(oldx,oldy); firstTime = false; } QMainWindow::resizeEvent(event);
}
-
@Apeksha "it's not working" doesn't help to solve the problem.
Did you analyse why it is not working? You can print out the values oldX, oldY for example.void MainWindow::resizeEvent(QResizeEvent* event)
{
qDebug() << "old size" << event->oldSize() << " to " << event->size();
int oldx,oldy;
oldpos=ui->pushButton->geometry();
oldx=oldpos.x();
oldy=oldpos.y();
qDebug()<<"oldpos x is"<<oldx<<"oldpos y is:"<<oldy;
if (!firstTime) {
ui->pushButton->move(50,100);
}else { ui->pushButton->move(oldx,oldy); firstTime = false; } QMainWindow::resizeEvent(event);
}
-
void MainWindow::resizeEvent(QResizeEvent* event)
{
qDebug() << "old size" << event->oldSize() << " to " << event->size();
int oldx,oldy;
oldpos=ui->pushButton->geometry();
oldx=oldpos.x();
oldy=oldpos.y();
qDebug()<<"oldpos x is"<<oldx<<"oldpos y is:"<<oldy;
if (!firstTime) {
ui->pushButton->move(50,100);
}else { ui->pushButton->move(oldx,oldy); firstTime = false; } QMainWindow::resizeEvent(event);
}
-
@Apeksha Your current resizeEvent is wrong: you read CURRENT button geometry and use it as OLD geometry! You need to store the old posx/posy before and restore it later when needed.
K i uderstood , Initially I will save oldx and oldy in constructor and where actually i have to use this oldx and oldy to restore. In above code If I use this oldx and oldy , its not restoring. I am attaching code:
void MainWindow::resizeEvent(QResizeEvent* event)
{
if (!firstTime) {
ui->pushButton->move(50,100);
}else { ui->pushButton->move(oldx,oldy); ---->This oldx and oldy Iam storing in constructor. firstTime = false; } QMainWindow::resizeEvent(event);
}
-
K i uderstood , Initially I will save oldx and oldy in constructor and where actually i have to use this oldx and oldy to restore. In above code If I use this oldx and oldy , its not restoring. I am attaching code:
void MainWindow::resizeEvent(QResizeEvent* event)
{
if (!firstTime) {
ui->pushButton->move(50,100);
}else { ui->pushButton->move(oldx,oldy); ---->This oldx and oldy Iam storing in constructor. firstTime = false; } QMainWindow::resizeEvent(event);
}
-
I think, Problem seems to be with enabling of flag "firsttime".
CAN YOU TRY THIS?
int oldX,oldY,fstFlag=1;
void MainWindow::resizeEvent(QResizeEvent *event)
{
if(!fstFlag)
{
oldX=0;
oldY=0;
fstFlag=1;
}
else
{
fstFlag=0;
oldX=50;
oldY=100;
}
ui->pushbutton->move(oldX,oldY);
} -
I think, Problem seems to be with enabling of flag "firsttime".
CAN YOU TRY THIS?
int oldX,oldY,fstFlag=1;
void MainWindow::resizeEvent(QResizeEvent *event)
{
if(!fstFlag)
{
oldX=0;
oldY=0;
fstFlag=1;
}
else
{
fstFlag=0;
oldX=50;
oldY=100;
}
ui->pushbutton->move(oldX,oldY);
}