How to init tableWidget? - qt

I've just started with Qt. I saw some articles, but don't understand one thing.
Here it is.
At this link filesTable comes from nowhere. I don't understand where it was inited?
Another example.
void MainWindow::on_pushButton_clicked()
{
ui->tableWidget->setColumnCount('');
ui->tableWidget->setRowCount('');
}
What is
tableWidget
? I mean, how I can create it?
Sorry for such question. I have to.

filesTable could be a member variable declared in the header file. If you're new to C++, you should probably start by learning C++ first, and then start using Qt.
tableWidget is a variable that points to a QTableWidget type object, which was created in the Qt Designer. It was declared somewhere in the ui header file, for example ui_mainwindow.h. This is a file that is usually auto generated by Qt Creator.

Related

Changing widget's name in both design view and code view in Qt

I'm just starting Qt5 using QtCreator, and I have a question regarding changing the name of widgets in design view.
So, for example, I have a QComboBox and I I use findChild() to find it (this is in mainwindow.cpp):
QComboBox *comboBoxType;
void MainWindow::on_Start_clicked()
{
comboBoxType = ui->stackedWidget->findChild<QComboBox *>("typeComboBox", Qt::FindChildrenRecursively);
comboBoxType->addItem("Hash");
comboBoxType->addItem("Hash2");
}
But when I changed the name of the the widget from typeComboBox to typeBox in design view, for instance, the typeComboBox name in mainwindow.cpp wasn't changed, and led to an obvious crash:
02:32:37: The program has unexpectedly finished.
02:32:37: The process was ended forcefully.
02:32:37: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe crashed.
Is there any ways of changing/updating both name, in .ui and mainwindow.cpp simultaneously, or is there a better way other than manually typing in the name in mainwindow.cpp, so that I don't have to go find the culprit in code after I change something in design view?
In QtCreator you can use the Advanced Find menu (Ctrl+Shift+F) to do a find and replace operation across all files in the project.
Enable the options "Case sensitive" and "Whole words only" to limit the match to the exact name.

QML map binding v2

I'm complete noob in Qt, so my question may sound too stupid, but I really need help. I know C++ a little and that's it.
So, my task is to write a C++ program which reads INI-alike (format is not very important) file
height=20
width=15
To make it clear, I have no idea what properties will be defined in this file, names or types are unknown to me at compile time.
After that program loads QML file (I can do this) and injects loaded file data (have no idea how to do this) as JavaScript object, for instance named "Settings", so that QML property bindings will use it like
Rectangle {
width: Settings.width
height: Settings.height
}
So the question is: How can I inject read data as JavaScript object into QML so that QML property binding will use it?
One way to do it would be to write a QObject wrapper around QSettings and expose an instance of it to QML, another would be to use the Settings QML Type. I am sure there are others.

QML map binding

I'm complete noob in Qt, so my question may sound too stupid, but I really need help. I know C++ a little and that's it.
So, my task is to write a C++ program which reads INI-alike file (I can do this, but not sure of most correct/Qt way)
height=20
width=15
or
height=int:20
width=int:15
if properties should be strongly typed. File format is not very important. To make it clear, I have no idea what properties will be defined in this file, names or types are unknown to me at compile time.
after that program loads QML file (I can do this) and injects loaded file data (have no idea how to do this) as JavaScript object, for instance named "Settings", so that QML property bindings will use it like
Rectangle {
width: Settings.width
}
So the questions are:
What is most correct/qt-style way to read INI-alike file?
How can I inject read data as JavaScript object into QML so that QML property binding will use it?
First: The most Qt-style way is using QSettings class:
QSettings *settings = new QSettings("G:/options1.ini",QSettings::IniFormat);
qDebug()<< "height" <<settings->value("height").toInt();
qDebug()<< "width" <<settings->value("width").toInt();
My file:
height=20
width=15
Output:
height 20
width 15
See description of this class. It is really helpful thing.
http://qt-project.org/doc/qt-4.8/qsettings.html

How to set QCamera on label

I've decided no to use OpenCV. I will use the QCamera class. Everything is working perfect to this moment. I can capture and save images wherever I want, but the problem is how I can set the camera to a label or graphics view?
I mean, to see what is happening at the moment. When I make infinite loop everything crashes. Write any information you know, cause there are no examples how to do that, or I just can't see. If you can please write some source code.
Use QCameraVievFinder or QVideoWidget widgets ( docs - here) for that purpose, here is example for you:
#include <QCameraViewfinder>
// .......
QCamera *camera=new QCamera(this);
QCameraViewfinder *viewfinder = new QCameraViewfinder(this);
viewfinder->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
camera->setViewfinder(viewfinder);
setCentralWidget(viewfinder);
//viewfinder->show();
camera->start(); // to start the viewfinder
Note: you need to add to your *,pro file this config to use them: QT += multimedia multimediawidgets
If you want a bit more low level widget (to process every frame the way you like (face detection etc), subclass QAbstractVideoSurface, docs - here
or try to connect to QVideoProbe class (docs - here), though i could not do it myself, this class is a bit experimental i guess, didn't worked

qt QTranslator reuse

I noticed that the Qt documentation is not very verbose on some of the aspects of the translations. I was fooling around with it trying to figure out their behaviour using trial & error. The ultimate goal is to get the translation changed on runtime but I am very confused as to what extent the QTranslator object can be re-used.
Consider this (where 'a' is the main instance of the application):
QTranslator translator;
translator.load("mytranslation_cz");
a.installTranslation(&translator);
(...)
a.removeTranslation(&translator)
Now the translator was removed from the application but what happened to the translator object?
In my tests when above code was followed by this again
translator.load("mytranslation_fr");
a.installTranslation(&translator);
it did not do anything in main() and it crashed the application when called from one of the widgets (using pointer to main app).
Therefore I am suspecting that I would need to create one QTranslator object per translation I want to load in the application and that I cannot reuse the QTranslator object. Am I right in this assumption?
And as a side question. Assuming the QTranslator object is untouched by the removeTranslation(), is it possible to simply install it later again like this?
QTranslator translator;
QTranslator translator1;
translator.load("mytranslation_cz");
translator1.load("mytranslation_fr");
a.installTranslation(&translator);
(...)
a.removeTranslation(&translator);
a.installTranslation(&translator1);
(...)
a.removeTranslation(&translator1);
a.installTranslation(&trasnlator); //Will this work?
Thanks for any clarification as I am somewhat confused as to what happens to the QTranslation objects when installing and removing translations from the application and especially if the QTranslation object can be reused for multiple translations somehow on runtime?
QTranslator::load basically in simple sense can be considered as a function that opens a given .qm file, reads in all the translated values and loads it in for a specific language.
Now in general operation you would not want to reuse this for many languages as by "reusing" (even if its allowed) your adding the overhead of parsing this given .qm file for every language every time you switch your UI language, which is just basically an overhead you don't need.
Your assumption of creating a QTranslator for each language is correct. As for your side question, Yes you can also re-use it. Thats the benefit of having individual QTranslator objects per translation. Just call qApp->removeTranslator() with the current translation and then qApp->installTranslator() with the new one. This way you are reusing the loaded translations as and when you please.
The way we structure this is by sub-classing QApplication and adding 2 functions
void Application::CreateTranslators() {
// translators_ is a QMap<QString, QTranslator*>
if (!translators_.isEmpty())
return;
QStringList languages;
languages << "en" << "ar" << "zh";
foreach(QString language, languages) {
QTranslator* translator = new QTranslator(instance());
translator->load(language);
translators_.insert(language, translator);
}
}
Now this function is called at the very start of the application.
2nd function is as following
void Application::SwitchLanguage(QString language) {
// current_translator_ is a QTranslator*
if (current_translator_)
removeTranslator(current_translator_);
current_translator_ = translators_.value(language, nullptr);
if (current_translator_)
installTranslator(current_translator_);
}
That's it. Using the second function you can switch your language at run-time as you please.
Couple things you'll also need to be aware of is changing QTranslator at run-time will update all translations from your .ui file strings marked as translatable automatically, however those set from code will not be. To get that you will have to override QWidget::changeEvent() and then check if the event is of type QEvent::LanguageChange and then set the required strings for that QWidget accordingly (Don't forget the tr() while doing so)

Resources