Results 1 to 7 of 7

Thread: Rename or edit and save the content dynamically using qt.

  1. #1
    Join Date
    May 2015
    Posts
    30
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Rename or edit and save the content dynamically using qt.

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Rename or edit and save the content dynamically using qt.

    Can you rephrase that?
    Are you looking for a way to get a filename from the user? QFileDialog?

    Cheers,
    _

  3. #3
    Join Date
    May 2015
    Posts
    30
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Rename or edit and save the content dynamically using qt.

    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?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rename or edit and save the content dynamically using qt.

    Qt Code:
    1. myLabel->setText( QInputDialog::getText( this, "Label Editor", "Enter the new label text:", QLineEdit::Normal, myLabel->text() );
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 17th June 2015 at 04:34.

  5. #5
    Join Date
    May 2015
    Posts
    30
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Rename or edit and save the content dynamically using qt.

    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Rename or edit and save the content dynamically using qt.

    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:

    Qt Code:
    1. void MyWidget::showEvent( QShowEvent * ev )
    2. {
    3. // Open the configuration INI file
    4. QSettings settings( "MyConfigFile.ini", QSettings::IniFormat );
    5.  
    6. // 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
    7. QString labelText = settings.value( "LabelText", QVariant( myLabel->text() ).toString();
    8.  
    9. // Change the label to show the new text
    10. myLabel->setText( labelText );
    11. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. myLabel1->setText( QInputDialog::getText( this, "Label Editor", "Enter the new label text:", QLineEdit::Normal, myLabel1->text() );
    2. myLabel2->setText( myLabel1->text() );
    3.  
    4. // Open the configuration INI file
    5. QSettings settings( "MyConfigFile.ini", QSettings::IniFormat );
    6.  
    7. // Save the value the user has set for the label text.
    8. settings.setValue( "LabelText", QVariant( myLabel->text() );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Rename or edit and save the content dynamically using qt.

    Quote Originally Posted by prakashmaiya View Post
    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,
    _

Similar Threads

  1. QML list and dynamically loaded content
    By matsukan in forum Qt Quick
    Replies: 5
    Last Post: 21st November 2013, 08:41
  2. Rendering Content Dynamically
    By nabeel in forum Newbie
    Replies: 4
    Last Post: 29th August 2013, 09:37
  3. Replies: 12
    Last Post: 24th October 2011, 07:56
  4. Edit TableView Content
    By emorgen in forum Newbie
    Replies: 7
    Last Post: 2nd June 2010, 22:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.