PDA

View Full Version : Rename or edit and save the content dynamically using qt.



prakashmaiya
4th June 2015, 13:13
Dear Expert,

Could you kindly let me know how to rename the content in qt, means it should allow to user to provide the or change the name and save it, so that later it should use. How to do the same. thanks in advance.

Regards,
Maiya

anda_skoa
4th June 2015, 14:34
Can you rephrase that?
Are you looking for a way to get a filename from the user? QFileDialog?

Cheers,
_

prakashmaiya
16th June 2015, 12:19
No. I have created one label called : Favorite1. Now I wanted to change this name to "MyFavorite". So it means that I wanted to edit this name. How to edit the name?

d_stranz
17th June 2015, 05:25
myLabel->setText( QInputDialog::getText( this, "Label Editor", "Enter the new label text:", QLineEdit::Normal, myLabel->text() );

is one way to do it. Of course, this will change the label only while the program is running. When you start the program again, it will use whatever label text is specified in the UI file. If you want the new text to appear, then you need to make it persistent (hint: QSettings) and restore it when the program starts again.

prakashmaiya
17th June 2015, 07:45
Thank you so much :).

You mean that should i restart the program if I want to gets update the new label name should appear in other scenarios, I mean just changing the label name in 1 screen (or one class), but same name should appear in another class immediately (may be in another class). Does this snippet help me to do the same?

Thank you,
Maiya

d_stranz
17th June 2015, 16:18
You mean that should i restart the program if I want to gets update

No. No. If you have set the original label name "Favorite1" using Qt Designer and save that in a .ui file, then ever time the program runs, it will set the label to "Favorite1" because that's what the .ui files says. If you allow the user to change the label, and you want the new label text to appear the next time the user runs the program, then you need to save the new name in a configuration file (using QSettings). The next time the program runs, you must read the configuration file (using QSettings) and get the new value for the label, and then set it using code, something like this:



void MyWidget::showEvent( QShowEvent * ev )
{
// Open the configuration INI file
QSettings settings( "MyConfigFile.ini", QSettings::IniFormat );

// Read the value the user has set for the label text. If it is not found, then use the default text set by the ui file
QString labelText = settings.value( "LabelText", QVariant( myLabel->text() ).toString();

// Change the label to show the new text
myLabel->setText( labelText );
}



I mean just changing the label name in 1 screen (or one class), but same name should appear in another class immediately (may be in another class).

I have no idea what you mean by this. A "class" is C++ code. You can't change code at runtime. If you mean you have two widgets with a label on them, and you want the same text to appear in both places when it is edited, then just change the text for the second label at the same time you change the text for the first one:



myLabel1->setText( QInputDialog::getText( this, "Label Editor", "Enter the new label text:", QLineEdit::Normal, myLabel1->text() );
myLabel2->setText( myLabel1->text() );

// Open the configuration INI file
QSettings settings( "MyConfigFile.ini", QSettings::IniFormat );

// Save the value the user has set for the label text.
settings.setValue( "LabelText", QVariant( myLabel->text() );

anda_skoa
20th June 2015, 12:54
No. I have created one label called : Favorite1. Now I wanted to change this name to "MyFavorite". So it means that I wanted to edit this name. How to edit the name?
Do you mean the name of the label variable or the value displayed by the label?

The latter has been thoroughly covered by d_stranz, in case of the former, just change the string (in designer if the label is created in a designer form, in the code editor if it is created in code)

Cheers,
_