QPushbutton movable Dynamically



  • Hi,

    I want to move qpushbutton to top position if my widget is resized dynamically, Please help me how to approach.

    Thanks in advance.



  • Hi! See the following links for the building blocks:



  • Hi,

    I went through the link, but I didn't how to approach my requirement.
    I am having widget and some labels and buttons in that, so when i resize my widget with mouse dragging the buttons which are placed bottom of widget should move to top position.


  • Qt Champions 2017

    @Apeksha

    When you resize the window widgets, its resizeEvent function is called.
    There you can move the button widget with move to where ever you want.

    Its unclear what top position is but if its another y position, then move should be fine.



  • Hi mrji,

    How this resizeEvent is used? Can you please guide me?

    Thanks in advance


  • Qt Champions 2017

    @Apeksha

    Hi
    You must subclass the widget and implement your own version
    like

    void MYWidget::resizeEvent(QResizeEvent* event)
    {
       MYWidget::resizeEvent(event); // make sure to call base
       // Your code here.
    }
    


  • Hi,
    I am using this resizeevent, but it is calling when binary runs.
    void MainWindow::resizeEvent(QResizeEvent* event)
    {
    qDebug("ResizeEvent");
    ui->pushButton->move(0,0);
    QMainWindow::resizeEvent(event);
    }


  • Qt Champions 2017

    @Apeksha

    hi

    • but it is calling when binary runs.

    Yes, is that not what you asked for ?
    Events only works when app is running.

    Do you mean in Designer ?



  • Hi ,

    No, it should called when user wil resize the gui using mouse.


  • Qt Champions 2017

    @Apeksha

    Sorry i do not understand what is wrong.

    The resizeEvent will be called when user resize the running app.

    Are you talking about inside Designer part, where you do widget design or where do you mean?

    is your widget a Designer plugin ?



  • Hi,

    You have said when resize event is used, it calls when user resize the running app.

    So I have done when user user resizes the app, then button have to move its Y axis, so i have written my code as follows:
    void MainWindow::resizeEvent(QResizeEvent* event)
    {
    qDebug("ResizeEvent");
    ui->pushButton->move(0,0);
    QMainWindow::resizeEvent(event);
    }

    But when my is running, without resizing app, button position is changing, my requirement is when user resizes app then only button position i.e, y-axis have to change, I hope you got my point


  • Moderators

    @Apeksha Why do you position the button manually? Why not just use layouts?



  • I am having some labels,lineedits and buttons in my gui, my requirement is when user resizes gui, then button have to change position.


  • Qt Champions 2017

    @Apeksha
    Ahhh, now i understand.
    Well, there is no way to tell if done by system at start up or by the user.
    But should it not do the same ?
    Or only if size is smaller then the start size or ? what is the rule?



  • Hi,
    There is no way to do as per my requirement, when user resizes app, then button has to change its position.


  • Qt Champions 2017

    Hi
    resizeEvent(event) is the way to do it.
    You just need to make sure then you do not react to the first resize events you get when app starts up.



  • 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?


  • Qt Champions 2017

    @Apeksha

    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 check

    I 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.



  • Hi,

    k, Assume my gui isof width and height 500,500, its resized to 400,400 then button has to change position, can u share sample code, I am not getting how to make


  • Moderators

    @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);
    }
    


  • Hi jsulm,
    Thank you so much, its very helpful.
    Again If i maximize my gui to original size, how to make button position to original position?


  • Moderators

    @Apeksha I don't know who you're going to position this button. What is the logic for positioning it?



  • @jsulm Same thing I was confused, Now If my gui is of 500,500(width,height), If i resized it to some 300,400 then button position will change. Now my doubt is if gui is maximized i.e to original size(500,500), how to move buttonto original position?



  • @Apeksha
    save coordinates


  • Qt Champions 2017

    @Apeksha

    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);
    }
    

  • Moderators

    @Apeksha I still don't understand why you position this button manually...



  • @mrjj

    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?



  • @Apeksha

    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);
    }
    


  • @Taz742

    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.


  • Qt Champions 2017

    @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 :)



  • @mrjj

    My code is wrong?

    what conditions I have to use? Can you please guide me?


  • Qt Champions 2017

    @Apeksha

    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 :)



  • @mrjj

    I have tried with ismaximized(), its not working.


  • Qt Champions 2017

    @Apeksha
    it always say false ? or in what way "not working"



  • @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.



  • @mrjj

    Yes, it always say false?


  • Qt Champions 2017

    @Apeksha

    ok didnt check it lately , i assume its because the NewSize from event has not yep been applied.

    I think i get your use case now
    the button show sort of go to next col if there is no room to be below.
    So its not on top of the other but on top in new col?
    like this
    alt text



  • @mrjj

    So what to do now? what is the solution?


  • Qt Champions 2017

    @Apeksha
    you can use
    const QSize &QResizeEvent::oldSize() const
    const QSize &QResizeEvent::size() const
    to check what size window has and had.

    Based on those numbers , you can do the right thing, store button pos
    or to restore the button to old pos.

    you can use qDebug() << "size" << event.oldSize() << " to " << event.Size();

    to get an idea of what numbers you will see.

    You can use
    http://doc.qt.io/qt-5/qdesktopwidget.html#availableGeometry

    to get how big screen is.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.