PDA

View Full Version : New item not saved to combobox when added dynamically



prasad1001
8th March 2014, 12:50
Hello all,
I am new to Qt, i am trying to add a new item to combobox when a pushbutton is clicked. At that time the combobox is showing the added item but when i closed the app and open the app it doesn't showing the added item i.e it is not saved to combobox.

How to save the new item to combobox

Here is my code:

mainwindow.h



private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
QComboBox *comboBox;
QPushButton *pushButton;


mainwindow.cpp



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
comboBox=ui->comboBox;
pushButton=ui->pushButton;
}

void MainWindow::on_pushButton_clicked()
{
comboBox->addItem("hari");
}


Please help me ASAP!! this is really urgent! thank you very much!!

Lesiok
8th March 2014, 16:19
What it means "when i closed the app and open the app" ?

anda_skoa
8th March 2014, 16:25
Your posting does not contain any code that would persist items in any way.

So if "when i closed the app and open the app" means you stop the program and run it again, then of course there won't be any data in the newly started instance of your program.

Cheers,
_

adutzu89
8th March 2014, 18:25
You can:
-write the data into a file using either QTextStream (http://qt-project.org/doc/qt-5/qtextstream.html) either QDataStream (http://qt-project.org/doc/qt-5/qdatastream.html)(on closing or when you add the item to the qcombobox) and when the QComboBox is loaded set it to read the file for content and add the saved content to your combobox
-use Sqlite in combination with QSqlDatabase (http://qt-project.org/doc/qt-5/qsqldatabase.html),QSqlQuery (http://qt-project.org/doc/qt-5/qsqlquery.html) to store and retrieve data from local database.

prasad1001
10th March 2014, 06:07
When i push the button, the item is added to comboBox and it is showing the added item.
But if i close the application and reopen it, the added item is not in the comboBox.

ChrisW67
10th March 2014, 06:40
As anda_skoa said, your code contains nothing that would cause the added item to persist past the current run of the program. As a result, the item does not persist between runs.

adutzu89 has some useful suggestions. QSettings might also be a suitable place to store a small number of new items between runs.