I am trying to make an app with live translation of text in a large tree model menu structure, in the same manner as: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/itemviews/simpletreemodel?h=5.15
The item's "data" is a QString that is translated like
root = new MenuObject(tr("Main menu"));
And children are appended like:
root->appendChild(new MenuObject(tr("Test 1")))
.appendChild(new MenuObject(tr("Test 2")))
I am using QML to show these, with a qmllistpoprerty to show these menus like:
Q_PROPERTY(QQmlListProperty<MenuObject> list READ getList NOTIFY listChanged);
The QML is a simple ListView with a delegate Label showing the MenuObjects's description with the q_property:
Q_PROPERTY(QString description READ getDescription CONSTANT);
To change language i am using a function getting the translation file into the translator, followed by:
installTranslator(translator);
engine.retranslate();
Now this does work for simple q_properties like:
Q_PROPERTY(QString header READ getHeader NOTIFY listChanged);
Where
QString MainMenu::getHeader(){
return tr("Header");
}
But I CANNOT get the translations to work for the items in the treemodel. Any help is appreciated.
If your description prop never fires an updated signal, then your UI will never refresh it.
The reason it works for Q_PROPERTY(QString header READ getHeader NOTIFY listChanged); is because presumably the listChanged() signal is fired whenever header is supposed to change also.
To fix it, you need to declare an appropriate NOTIFY signal for your description, and of course it is no longer a CONSTANT.
Related
I have two forms one is trainee_view.ui
and other is enter_new_trainee.ui
so for that i have trainee_view.cpp,trainee_view.h to see the list of Trainee in DB
and enter_new_trainee.cpp,enter_new_trainee.h to enter new trainee details
now in trainee_view.ui i have a push button "ADD Trainee"
so if i click this button it will go to "enter_new_trainee.ui"
void trainee_view::on_pushButton_2_clicked()
{
newtrainee=new enter_new_trainee(this);
newtrainee->setWindowFlags(Qt::Window);
newtrainee->show();
// connect(newtrainee, SIGNAL(destroyed()), this, SLOT(refresh_form()));
}
so by using connect() i am trying to refresh the trainee_view after entering the new trainee details. so how can i emmit the signal from
2nd form to 1st form such that i call refresh_form() method in 1st form .
I tried to use destroyed() signal on newtrainee but could not refresh my trainee_view form.
To be MOre simple . i just want to get an object is destroyed or not so if destroyed i can call refresh() method to load back the changes done on widget
for that i opted connect() method so how should i call that. becoz if i call
connect(newtrainee, SIGNAL(destroyed()), this, SLOT(refresh_form()));
there is no effect i.e nothing is loading into the view.
am newbie to qt so pls try to help me.
Thank YOu.
I'm not sure if I correctly understand your app, but I think you misunderstand the concept of Signals and Slots. Look here for some examples. In some simplification you can look at signal and slots this way: connect() command is a place which will not do anything - it just stay and keep listening for a signal. So you should place it in trainee_view.cpp. That's the first part and I see you did it correct, or almost correct. But you need also something that will send the signal, and this is exactly what emit() command do - it should be placed in enter_new_trainee.cpp just after description of generation new entry. For example, let assume user input new entry in LineEdit in UI:
[...]
QString newEntry = ui->LineEdit->text(); //Save entry to variable
emit(newEntry); //Emit it to signal slot
[...]
I have a cascades project where I use the MediaPlayer class in cpp.
I have defined a handler class, which handles metaDataChanged event, but when I set the source url and call mediaPlayer.prepare() method, it doesn't retrieve anything in metadata, so it's simply empty QVariantMap.
What's interesting is that defined event handler for metaDataChaned event is not even called.
I think there could be something that I can add here to be able to get the metadata, however prepare() method workds sucessfully, so I don't know what's the problem
here is a piece of code I've tried.
bb::multimedia::MediaPlayer* mp = new bb::multimedia::MediaPlayer();
mp->setSourceUrl(resultString);
mp->prepare();
MetaDataReader metaDataReader(mp);
and a class
MetaDataReader::MetaDataReader(bb::multimedia::MediaPlayer* mediaPlayer) : QObject(NULL)
{
connect(mediaPlayer, SIGNAL(metaDataChanged(const QVariantMap&)), this, SLOT(onMetaDataChanged(const QVariantMap&)));
}
void MetaDataReader::onMetaDataChanged(const QVariantMap& metaData)
{
someCode
// It doesn't reach this SLOT
}
How can I get the metadata here?
thanks in advance
It's a bit odd, but you may not get metadata until you start playback for the file. Try starting playback, and you should see the metaDataChanged signal get fired shortly after.
I am a student programmer using Qt to build a reader Table for my company. This reader is both an editor and converter. It reads in a .i file allows table editing of a text document and then puts out a .scf file which is essentially a separated value file stacked under a legend built with headers. I digress... Basically the file format imported is really hard to scan and read in(mostly impossible) so what I'd like to is modify the open file preBuilt QFileDialog to include an additional drop down when older file types are selected to declare their template headers.
When the user selects .i extension files(option 2 file type) I would like to enable an additional drop down menu to allow the user to select which type of .i file it is(template selected). This way I don't have to deal with god knows how many hours trying to figure out a way to index all the headers into the table for each different type. Currently my importFile function calls the dialog using this:
QString fileLocation = QFileDialog::getOpenFileName(this,("Open File"), "", ("Simulation Configuration File(*.scf);;Input Files(*.prp *.sze *.i *.I *.tab *.inp *.tbl)")); //launches File Selector
I have been referencing QFileDialog Documentation to try and find a solution to what I need but have had no avail. Thanks for reading my post and thanks in advance for any direction you can give on this.
UPDATE MAR 16 2012;
First I'd like to give thanks to Masci for his initial support in this matter. Below is the connect statement that I have along with the error I receive.
//Declared data type
QFileDialog openFile;
QComboBox comboBoxTemplateSelector;
connect(openFile, SIGNAL(currentChanged(const &QString)), this, SLOT(checkTemplateSelected()));
openFile.layout()->addWidget(comboBoxTemplateSelector);
I also noticed that it didn't like the way I added the QComboBox to the modified dialog's layout(which is the second error). I really hope that I'm just doing something dumb here and its an easy task to overcome.
In response to tmpearce's comment heres my header code;
#include <QWidget>
namespace Ui {
class ReaderTable;
}
class ReaderTable : public QWidget
{
Q_OBJECT
public:
explicit ReaderTable(QWidget *parent = 0);
~ReaderTable();
public slots:
void checkTemplateSelected();
void importFile();
void saveFile();
private:
Ui::ReaderTable *ui;
};
Thanks for reading and thanks in advance for any contributions to this challenge!
Instance a QFileDialog (do not call getOpenFileName static method), access its layout and add a disabled QComboBox to it.
// mydialog_ and cb_ could be private fields inside MyClass
mydialog_ = new QFileDialog;
cb_ = new QComboBox;
cb_->setEnabled(false);
connect(mydialog, SIGNAL(currentChanged(const QString&)), this, SLOT(checkFilter(const QString&)));
mydialog_->layout()->addWidget(cb_);
if (mydialog_->exec() == QDialog::Accepted) {
QString selectedFile = mydialog_->selectedFiles()[0];
QString cbSelection = cb_->currentText();
}
the slot would be something like:
void MyClass::checkFilter(const QString& filter)
{
cb_->setEnabled(filter == "what_you_want");
}
returning from the dialog exec(), you could retrieve selected file and cb_ current selection.
Notice you could add something more complex than a simple QComboBox at the bottom of the dialog, taking care of gui cosmetics.
Actually I don't like very much this approach (but that was what you asked for :-). I would make a simple dialog like this:
and enable the combo only if the selected file meets your criteria. The "browse" button could call getOpenFileMethod static method in QFileDialog.
You can handle item selection by this signal:
void QFileDialog::fileSelected ( const QString & file )
Then it occurs, call setFilter with type you want.
Sorry, if i don't understand your task.
I have code in C++ for saving and retrieving data from an XML file. I have some forms built in QML that I would like to hook up in such a way that when data is entered in my QML, processing is handled in C++, and searches are done for products via QML form, processed in C++ and list of product items are handed back to QML for display.
class ProcessRequests : public QObject
{
Q_OBJECT
Q_PROPERTY(string username READ username WRITE username)
Q_PROPERTY(string useremail READ useremail WRITE useremail)
Q_PROPERTY(string usercomplaint READ usercomplaint WRITE usercomplaint)
public:
ProcessRequests()
{}
~ProcessRequests(){}
Q_INVOKABLE void SubmitComplaint(){
//TODO: Add Xml code to save the property values to file
}
};
I think your problem is to inter-communicate between QML and C++ code, you can do it with code like this:
//Product.cpp
QmlApplicationViewer viewer;
QDeclarativeEngine *engine = viewer.engine();
QDeclarativeContext *context = engine->rootContext();
context->setContextProperty("Product", this);
//Your QML File
Product.YourFunction(args);
The topic is rather large, you'd better have a look at the docs, they're quite clear and there should be some tutorial as well. This is a good starting point: QML bindings in C++.
I'm writing a code for a voting machine that will allow user to read in custom XML ballots and then vote on them, however in the current build I have in QtCreator, I don't see an easy way to edit buttons in my GUI directly. In the code snippet below, I assign an element to pull out the names and type of ballot being read in, but I need to append a label on the GUI as well as change buttons to the names of candidates read in. Any ideas on how to do this?
while(!n.isNull()){
QDomNode x = n.firstChildElement();
QDomElement e = n.toElement();
QMessageBox::information(0,
tr( "Loading Element" ),
tr( "Our element is %1" ).arg(e.tagName()) );
QDomElement p = x.firstChildElement();//p finds Races
QMessageBox::information(0,tr("Foo"),tr("p = %1").arg(p.text()));//finds Race and Ballot types
n = n.nextSibling();
}
}
All the widgets you created using the Designer UI are available from your code. How to access them depends on how you linked your UI with the rest of your classes (see http://doc.qt.io/archives/4.6/designer-using-a-ui-file.html) but if you used the multiple inheritance approach your widgets and layouts will be accessible directly from your class using the name under which they appear in Designer. Qt Creator's completion will even work with them.
Having that in mind you can easily use the usual methods to change a widget's name, add a label to a layout, etc.
If this is still unclear, please add the code you use to embed your GUI, as it is needed in order to give you a sensible code example.