Results 1 to 6 of 6

Thread: New item not saved to combobox when added dynamically

  1. #1
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Post New item not saved to combobox when added dynamically

    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

    Qt Code:
    1. private slots:
    2. void on_pushButton_clicked();
    3.  
    4. private:
    5. Ui::MainWindow *ui;
    6. QComboBox *comboBox;
    7. QPushButton *pushButton;
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. comboBox=ui->comboBox;
    7. pushButton=ui->pushButton;
    8. }
    9.  
    10. void MainWindow::on_pushButton_clicked()
    11. {
    12. comboBox->addItem("hari");
    13. }
    To copy to clipboard, switch view to plain text mode 


    Please help me ASAP!! this is really urgent! thank you very much!!
    Last edited by anda_skoa; 8th March 2014 at 15:23. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,532
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: New item not saved to combobox when added dynamically

    What it means "when i closed the app and open the app" ?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: New item not saved to combobox when added dynamically

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

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

    Default Re: New item not saved to combobox when added dynamically

    You can:
    -write the data into a file using either QTextStream either QDataStream(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,QSqlQuery to store and retrieve data from local database.

  5. #5
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: New item not saved to combobox when added dynamically

    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.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: New item not saved to combobox when added dynamically

    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.

Similar Threads

  1. Replies: 2
    Last Post: 3rd June 2013, 16:45
  2. Replies: 8
    Last Post: 2nd February 2012, 19:52
  3. Replies: 0
    Last Post: 25th July 2011, 14:11
  4. Automatic file extension is not added to saved file on mac os x
    By sanjayshelke in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 06:56
  5. Connecting dynamically added properties
    By stefkeB in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2008, 15:19

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.