PDA

View Full Version : Saving item to QComboBox on Android



alistair
25th August 2015, 23:54
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! :)

anbu01
26th August 2015, 07:12
@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.

anda_skoa
26th August 2015, 18:39
Or write into a QSettings object

Cheers,
_

alistair
28th August 2015, 01:18
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)


QFile file(filename);
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
QString entry = in.readLine();
while (!in.atEnd())
{
ui->pic_cbox->addItem(entry);
entry = in.readLine();
}
file.close();
}


Trying to write value to text file:


QFile file(filename);
if (file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text))
{
QTextStream stream(&file);
stream << new_entry << endl;
// file.seek(0);
// file.write(new_entry+"\n");
file.close();
}


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!! :)

anda_skoa
28th August 2015, 07:52
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,
_

alistair
1st September 2015, 02:31
For writing, it's still overwriting the text at the start of the file for some reason. Code below:


QFile file(filename);
if (file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text))
{
qint64 fileSize = file.size();
file.seek(fileSize());
QTextStream stream(&file);
stream << new_entry << endl;
// file.write(new_entry+"\n");
file.close();
}


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



QFile file(filename);
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
QString entry = in.readLine();
while (!(in.atEnd()))
{
ui->pic_cbox->addItem(entry);
entry = in.readLine();
}
ui->pic_cbox->addItem(entry);
file.close();
}


Thank you for your help! :)

anda_skoa
1st September 2015, 08:29
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()?



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,
_

adutzu89
3rd September 2015, 09:46
You could also store them using a sqlite.

alistair
8th September 2015, 00:03
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!