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 set default geometry my widget ? Why doesn't work savegeometry() and restoregeometry() ?

How to set default geometry my widget ? Why doesn't work savegeometry() and restoregeometry() ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 1.5k Views
  • 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.
  • M Offline
    M Offline
    Mucahit
    wrote on last edited by Mucahit
    #1

    Hi all,

    I'm struggling to set my window to default geometry. I can't save geometry settings and load settings. I want to move some buttons and then set them to default position. I would be happy if anyone help. Thank you . My code is below :

           #include "kaliteform.h"
    
    	#include "ui_kaliteform.h"
    
    	#include<QSettings>
    
    	int buyutec=0;
    
    	MainWindow::MainWindow(QWidget *parent) :
    
    	QMainWindow(parent),
    
    	ui(new Ui::MainWindow)
    
    	{
    
    	ui->setupUi(this);
    
    	}
    
    
    
    	void kaliteform::on_pushButton_geometry_clicked()
    
    	{
    
    	int kalan=buyutec%2;
    
    		if(kalan==0)
    
    		{
    
    		savescreen();
    
    		ui->pushButton_test->setGeometry(10,10,10,10);
    
    		ui->pushButton_test1->setGeometry(20,20,20,20);
    
    		ui->pushButton_test2->setGeometry(20,20,20,20);
    
    		}
    
    	
    
    		else
    
    		{
    
    		loadscreen();
    
    		}
    
    	}
    
    
    
    	void kaliteform::savescreen()
    
    	{
    
    	QSettings settings("MyCompany", "MyApp");
    
    	settings.setValue("geometry", saveGeometry());
    
    	settings.setValue("windowState", saveState());
    
    	}
    
    	void kaliteform::loadscreen()
    
    	{
    
    	QSettings settings("MyCompany", "MyApp");
    
    	restoreGeometry(settings.value("myWidget/geometry").toByteArray());
    
    	restoreState(settings.value("myWidget/windowState").toByteArray());
    
    	}
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please use the code - tags for formatting so we can properly read your code.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mucahit
        wrote on last edited by
        #3

        Sorry, i fixed it.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          This is not fixed - this is even worse. Can you please take a look on what you've written and tell us how this should be readable by others? Please be a little bit more careful.

          Where do you modify buyutec?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mucahit
            wrote on last edited by
            #5

            I have only one goal. Return the button I moved to its initial default position.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              I don't see how save/restoreGeometry() should affect a child at all.

              btw: Don't use hard-coded geometry values but proper layouts.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Mucahit
                wrote on last edited by Mucahit
                #7

                Sir I'm not sure I'll use savegeometry() and loadgeometry() exactly, please don't stick with the above code. I have only one request. A button, for example, initially in position 0.0. How do I set this button to its default position after moving it to the 10,10 position first? Can i do it without using this way ? :

                oldpos=ui->pushButton_test->geometry();

                Because I have too many buttons, I can't assign each position to a variable. I'm sorry for my bad english. Please help me.

                So in summary I want to bring a button I moved back to its original position. That's all.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Mucahit said in How to set default geometry my widget ? Why doesn't work savegeometry() and restoregeometry() ?:

                  So in summary I want to bring a button I moved back to its original position. That's all.

                  You either have to use a proper layout (highly recommended) or move all buttons to the position you want to by your own.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    Mucahit
                    wrote on last edited by
                    #9

                    Thank you

                    1 Reply Last reply
                    0

                    • Login

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