QPushbutton movable Dynamically
-
Hi,
I am using resizeevent , but my button is moving initially when app runs, but it have to move when I resize my gui.
void MainWindow::resizeEvent(QResizeEvent* event)
{
qDebug("ResizeEvent");
ui->pushButton->move(0,0);
QMainWindow::resizeEvent(event);
}
this is the way I am doing, any correction? -
That is fine. but you will also do on first resize
to avoid that, make some if statements
the event has both oldSize and newSixe to checkI cannot tell you what to write as i dont know
what the rules are to move the button.
At what sizes etc.You can also use a timer to only allowed after say 10 secs to
avoid doing it on first resize. -
@Apeksha Just add a boolean variable to your class and set it to false in the resizeEvent:
MainWindow::MainWindow(...): firstTime(true) { ... } void MainWindow::resizeEvent(QResizeEvent* event) { qDebug("ResizeEvent"); if (!firstTime) { ui->pushButton->move(0,0); } else { firstTime = false; } QMainWindow::resizeEvent(event); }
-
-
you can just store its old position ?
QRect oldpos; // int the class
void MainWindow::resizeEvent(QResizeEvent* event) { qDebug("ResizeEvent"); if (!firstTime) { oldpos=ui->pushButton->geometry(); ui->pushButton->move(0,0); } else { firstTime = false; } QMainWindow::resizeEvent(event); }
-
QRect oldpos; // int the class
void MainWindow::resizeEvent(QResizeEvent* event)
{
qDebug("ResizeEvent");
if (!firstTime) {
oldpos=ui->pushButton->geometry();
ui->pushButton->move(0,0);
} else {
firstTime = false;
}
QMainWindow::resizeEvent(event);
}Here we are saving old geometry, again where we set this geometry?
-
void MainWindow::resizeEvent(QResizeEvent* event) { qDebug("ResizeEvent"); if (!firstTime) { oldpos=ui->pushButton->geometry(); oldX = oldpos.X; oldY = oldpos.Y; ui->pushButton->move(0,0); } else { firstTime = false; ui->pushButton->move(oldX, oldY); } QMainWindow::resizeEvent(event); }
-
void MainWindow::resizeEvent(QResizeEvent* event)
{
oldpos=ui->pushButton->geometry();
int oldx=oldpos.x();
int oldy=oldpos.y();
qDebug()<<"oldpos x is"<<oldx<<"oldpos y is:"<<oldy;
if (!firstTime) {
ui->pushButton->move(50,100);
}
else {
firstTime = false;
ui->pushButton->move(oldx,oldy);
}QMainWindow::resizeEvent(event);
}
But it's not working, button is not moving to old position again when gui is maximized.
-
@Apeksha said in QPushbutton movable Dynamically:
But it's not working, button is not moving to old position again when gui is maximized.
But that is impossible for it to know.
You must check for that situations then. and then call the
ui->pushButton->move(oldx,oldy);you can use isMaximized()
I wish i understood why you cant just use a layout :)
-
Not wrong as such, but you seem to except it can guess your thoughts :)
The resizeEvent is called when ever windows size has changes.
If you want to do anything , you must make the logic your self.
if you want to restore the button when maximized you can test with
isMaximized() to see if that is now. and if yes, then
move it back to old spot wtih ui->pushButton->move(oldx,oldy);The firstTime flag only fixed the situation that you didnt not want to move it on first resize (first time shown)
If you want to move it back then when maximized you need some code for that too :)
-
@jsulm said in QPushbutton movable Dynamically:
@Apeksha I still don't understand why you position this button manually...
I second that. I'm so curious, pretty please with sugar on top, explain your use case.
-
@Wieland
ui->contentWidget->setStyleSheet("QWidget {background-color:#C0C0C0;}");
ui->contentWidget->setMinimumSize( 820, 150 );
QHBoxLayout *ConfigurationLayout = new QHBoxLayout;
QScrollArea *scrollArea = new QScrollArea;
// scrollArea->setStyleSheet("QScrollArea {background-color:#C0C0C0;}");
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setWidget(ui->contentWidget);
scrollArea->setWidgetResizable(true);
ConfigurationLayout->addWidget(scrollArea);
mainBoxLayout->addLayout(ConfigurationLayout);This is my part of code where button movable is required, I had widget and scrollArea and some buttons.
So when my app is minimized, one of the button position should change, when gui is minized it have to display on top of widget. I hope you got my requirement.