Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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

  • Lifetime Qt Champion

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



  • Sorry, i fixed it.


  • Lifetime Qt Champion

    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?



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


  • Lifetime Qt Champion

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

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



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


  • Lifetime Qt Champion

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



  • Thank you


Log in to reply