Results 1 to 4 of 4

Thread: Adding an item to combo box dynamiacally

  1. #1
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    14
    Qt products
    Qt4 Qt/Embedded

    Default Adding an item to combo box dynamiacally

    Hi all,
    I have created a simple application with a single combo box and some push buttons. This is to implement multilingual feature of Qt. I want to dynamically update the combo box if any new .qm file is added to the project folder. I'm getting the file name added to the combo box. But once I select the dynamically added item in the combo box the language changes and the corresponding new item disappears. I want it to be added permanently. please help how to do this.
    Thanks in advance.

    here is the code I've used
    Qt Code:
    1. #include "detect.h"
    2. #include "ui_detect.h"
    3. #include "QFileSystemWatcher"
    4. #include "QFileInfo"
    5. #include "QTranslator"
    6. #include "QTextStream"
    7. #include "QString"
    8. #include "QComboBox"
    9.  
    10. QTranslator langTranslator;
    11. QString line1;
    12. detect::detect(QWidget *parent) :
    13. QDialog(parent),
    14. ui(new Ui::detect)
    15. {
    16. ui->setupUi(this);
    17.  
    18. qApp->installTranslator(&langTranslator);
    19. system("ls ..\ >new.txt");
    20. system("comm -13 old.txt new.txt >difference.txt");
    21. readdifference();
    22.  
    23. watcher=new QFileSystemWatcher;
    24. watcher->addPath("C:/Documents and Settings/20010833/My Documents/testdet");
    25.  
    26.  
    27. QObject::connect(watcher, SIGNAL(directoryChanged(const QString)),this, SLOT(directorychanged(const QString)));
    28.  
    29. }
    30.  
    31. detect::~detect()
    32. {
    33. delete ui;
    34. }
    35.  
    36. void detect::changeEvent(QEvent *e)
    37. {
    38. QDialog::changeEvent(e);
    39. switch (e->type()) {
    40. case QEvent::LanguageChange:
    41. ui->retranslateUi(this);
    42. break;
    43. default:
    44. break;
    45. }
    46. }
    47.  
    48. void detect::on_comboBox_activated(const QString & text)
    49. {
    50. if(text == tr("French"))
    51. {
    52. langTranslator.load("arrowpad_fr","C:/Documents and Settings/20010833/Desktop/testdet" );
    53. }
    54. else if(text == line1)
    55. {
    56. langTranslator.load(line1,"C:/Documents and Settings/20010833/Desktop/testdet" );
    57. }
    58.  
    59. }
    60. void detect::readdifference()
    61. {
    62. QString path="C:/Documents and Settings/20010833/Desktop/testdet";
    63. QFile nfile("difference.txt");
    64.  
    65. if(!nfile.open(QIODevice::ReadOnly ))
    66. return;
    67. QTextStream in(& nfile);
    68. while(!in.atEnd())
    69. {
    70. line1=in.readLine();
    71. if(line1.endsWith("qm"))
    72. {
    73.  
    74. ui->comboBox->addItem(line1,QVariant::Char);
    75. // break;
    76. }
    77. }
    78. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 31st December 2010 at 11:30.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding an item to combo box dynamiacally

    When you call retranslateUi() the method clears the combobox and fills it up again with translated content. But this content only contains items that were added to the ui file. The best thing you can do is to call your own method that will reinitialize the combobox right after the call to retranslateUi().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Adding an item to combo box dynamiacally

    retranslateUi(...) will clear the combo box.

    You always need to repopulate the combo box

  4. #4
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    14
    Qt products
    Qt4 Qt/Embedded

    Default Re: Adding an item to combo box dynamiacally

    Thanks for the reply. I called the readdifference() function to repopulate the combo box after retranslate() function is called. Now it's working fine.

    I have one more query, If I add one new .qm file to the project folder, everything works fine. But if I add two or three .qm files to the folder, the translation happens only for the last .qm file or the corresponding language. I kno the reason because, for combo box activated function I have called load function only once for dynamically added item.
    My question is How could I change my code to achieve translation for every new .qm files added?

Similar Threads

  1. Stylesheet of selected item in combo-box
    By shrawan0786 in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2010, 10:30
  2. Replies: 1
    Last Post: 29th June 2010, 17:59
  3. Adding item to scene
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 17:28
  4. QTableWidget item checkable and combo?
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 07:12
  5. Adding item to GridLayout???
    By Kapil in forum Newbie
    Replies: 23
    Last Post: 7th April 2006, 11:26

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.