Results 1 to 9 of 9

Thread: Saving item to QComboBox on Android

  1. #1
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Saving item to QComboBox on Android

    Hi,

    I have made an app for android and on that app the user can add a new item to a combobox. How can I then save that item to the combobox so that it will stay there forever instead of disappearing with each application reboot?

    Thank you for your time and help!

  2. #2
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving item to QComboBox on Android

    @alistair

    Not familiar with app in android but still there are different ways in general for saving combo box data.One easier or simpler way is to write it to XML file and read it when application starts.

  3. The following user says thank you to anbu01 for this useful post:

    alistair (28th August 2015)

  4. #3
    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: Saving item to QComboBox on Android

    Or write into a QSettings object

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    alistair (28th August 2015)

  6. #4
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Saving item to QComboBox on Android

    Thank you for your help but I think I might need a bit more. I tried using QSettings but I could figure out how to get that working at all lol. I have been trying just writing the entries to a .txt file and reading that in but nothing is functioning. Below is my code:
    Trying to read entries from .txt file into combo box: (.txt file is one word per line)
    Qt Code:
    1. QFile file(filename);
    2. if (file.open(QIODevice::ReadOnly))
    3. {
    4. QTextStream in(&file);
    5. QString entry = in.readLine();
    6. while (!in.atEnd())
    7. {
    8. ui->pic_cbox->addItem(entry);
    9. entry = in.readLine();
    10. }
    11. file.close();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Trying to write value to text file:
    Qt Code:
    1. QFile file(filename);
    2. if (file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text))
    3. {
    4. QTextStream stream(&file);
    5. stream << new_entry << endl;
    6. // file.seek(0);
    7. // file.write(new_entry+"\n");
    8. file.close();
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have tried every possible configuration I can think of for both two sections of code and still no result. The writing section does not append but just writes over existing text instead. The reading section just does not populate the combo boxes.

    Does anyone know what I'm doing wrong?

    Thank you for all of your help and time!!
    Last edited by alistair; 28th August 2015 at 05:12.

  7. #5
    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: Saving item to QComboBox on Android

    For writing, you could try to seek to the end before appending, or writing all entries.

    For reading you need to fix your loop condition. Right now it won't append the last line (stream at end after reading last line).

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    alistair (8th September 2015)

  9. #6
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Saving item to QComboBox on Android

    For writing, it's still overwriting the text at the start of the file for some reason. Code below:
    Qt Code:
    1. QFile file(filename);
    2. if (file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text))
    3. {
    4. qint64 fileSize = file.size();
    5. file.seek(fileSize());
    6. QTextStream stream(&file);
    7. stream << new_entry << endl;
    8. // file.write(new_entry+"\n");
    9. file.close();
    10. }
    To copy to clipboard, switch view to plain text mode 

    For reading in values, it just doesn't populate the combo box at all. Code below:

    Qt Code:
    1. QFile file(filename);
    2. if (file.open(QIODevice::ReadOnly))
    3. {
    4. QTextStream in(&file);
    5. QString entry = in.readLine();
    6. while (!(in.atEnd()))
    7. {
    8. ui->pic_cbox->addItem(entry);
    9. entry = in.readLine();
    10. }
    11. ui->pic_cbox->addItem(entry);
    12. file.close();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for your help!
    Last edited by alistair; 1st September 2015 at 03:43.

  10. #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: Saving item to QComboBox on Android

    Quote Originally Posted by alistair View Post
    For writing, it's still overwriting the text at the start of the file for some reason.
    And you have made sure that the value in fileSize is the same value returned by the fileSize() function you are calling there?
    What's the value of pos() after the seek()?

    Quote Originally Posted by alistair View Post
    For reading in values, it just doesn't populate the combo box at all.
    I assume you have verfied that the addItem() calls are processed and the respective "entry" values are what you have been expecting.

    So, since the combobox is populated correctly as verfied by you, maybe it is cleared elsewhere?

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    alistair (8th September 2015)

  12. #8
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Saving item to QComboBox on Android

    You could also store them using a sqlite.

  13. The following user says thank you to adutzu89 for this useful post:

    alistair (8th September 2015)

  14. #9
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Saving item to QComboBox on Android

    Hi,

    I'm sorry but the code I posted is now working and I'm not sure why it wasn't working before. Thanks heaps for everyone's help!

Similar Threads

  1. Replies: 1
    Last Post: 22nd September 2013, 14:40
  2. Saving values from Qlineedit and qcombobox?
    By prabhudev in forum Newbie
    Replies: 9
    Last Post: 23rd May 2012, 06:53
  3. QCheckBox as item of QComboBox
    By Grinchman in forum Newbie
    Replies: 6
    Last Post: 23rd June 2011, 14:44
  4. Replies: 0
    Last Post: 10th March 2011, 12:44
  5. QComboBox and item
    By drow in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2006, 10:04

Tags for this Thread

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.