PDA

View Full Version : Adding an item to combo box dynamiacally



madhukiranmp
31st December 2010, 09:01
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

#include "detect.h"
#include "ui_detect.h"
#include "QFileSystemWatcher"
#include "QFileInfo"
#include "QTranslator"
#include "QTextStream"
#include "QString"
#include "QComboBox"

QTranslator langTranslator;
QString line1;
detect::detect(QWidget *parent) :
QDialog(parent),
ui(new Ui::detect)
{
ui->setupUi(this);

qApp->installTranslator(&langTranslator);
system("ls ..\ >new.txt");
system("comm -13 old.txt new.txt >difference.txt");
readdifference();
QFileSystemWatcher *watcher;

watcher=new QFileSystemWatcher;
watcher->addPath("C:/Documents and Settings/20010833/My Documents/testdet");


QObject::connect(watcher, SIGNAL(directoryChanged(const QString)),this, SLOT(directorychanged(const QString)));

}

detect::~detect()
{
delete ui;
}

void detect::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void detect::on_comboBox_activated(const QString & text)
{
if(text == tr("French"))
{
langTranslator.load("arrowpad_fr","C:/Documents and Settings/20010833/Desktop/testdet" );
}
else if(text == line1)
{
langTranslator.load(line1,"C:/Documents and Settings/20010833/Desktop/testdet" );
}

}
void detect::readdifference()
{
QString path="C:/Documents and Settings/20010833/Desktop/testdet";
QFile nfile("difference.txt");

if(!nfile.open(QIODevice::ReadOnly ))
return;
QTextStream in(& nfile);
while(!in.atEnd())
{
line1=in.readLine();
if(line1.endsWith("qm"))
{

ui->comboBox->addItem(line1,QVariant::Char);
// break;
}
}
}

wysota
31st December 2010, 11:29
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().

tbscope
31st December 2010, 11:30
retranslateUi(...) will clear the combo box.

You always need to repopulate the combo box

madhukiranmp
3rd January 2011, 06:36
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?