Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change button position in a run time?
Forum Updated to NodeBB v4.3 + New Features

How to change button position in a run time?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Unique
    wrote on last edited by
    #1

    Hello,

    I'm new to QT and I want to know, how to change button position in a run time? Plus, how can I change position inside a method of MainWindow while a button was created in a constructor?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      use button.setGeometry(x,y,width,height) method to position.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to the forums.

        To do as you ask, would be something like

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        namespace Ui {
        class MainWindow;
        }
        
        #include <QPushButton>// so it knows type
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        public slots: // notice this. it makes it a slot
            void buttonClicked(); // to be called when clicked
        private:
            Ui::MainWindow *ui;    
            QPushButton *button=NULL; // the button must be a member to be used from other functions in mainwin
        };
        
        #endif // MAINWINDOW_H
        
        

        and in .cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow) {
          ui->setupUi(this);
        
          // create button
          button = new QPushButton("Click me", this);
          // hook up its clicked signal to a function in main
          connect(button, &QPushButton::clicked, this, &MainWindow::buttonClicked);
          // move and resize it
          button->setGeometry(100, 100, 50, 50);
          // and show it
          button->show();
        }
        
        void MainWindow::buttonClicked() {
          // move to random position.
          button->move( rand() % 200, rand() % 200);
        }
        

        runnable project to play with
        https://www.dropbox.com/s/3l21boazu2uhpfl/MyFirstButtons.zip?dl=0

        Result:
        alt text

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved