Parsing ini-File in Qt [closed] - qt

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using a custom made INI parser, for which the parameters are the form name, parent widget. The syntax looks like this:
int width = w->findchild("form_name","section_to_be_accessed_name").toInt();
I maybe a little bit wrong with the syntax, because I don't have the code with me right now. I want to add a few labels which are mentioned in page 2 of the INI file. The properties of those images are mentioned in the ini file itself. Even the absolute path,etc.
I tried multiple combinations, but it doesn't work. Can anyone give me a syntax for the same? What should be the return value here? A label, I have already created labels in Qt designer. Let's say label1. But there are many labels. Kindly let me know.

QSettings is a great class that works well for handling INI files. I would check it out. It has been optimized well, and is very robust. It also uses QVariant in a very intelligent way. It also handles "Groups" well.
http://qt-project.org/doc/qt-4.8/qsettings.html
// in your main, or somewhere very early in your program
qApp->setApplicationName("Star Runner");
qApp->setOrganizationName("MySoft");
QSettings::setDefaultFormat(QSettings::IniFormat);
Later when you want to access or set a setting
QSettings s; // in windows this would be
// C:/Users/<username>/AppData/Roaming/MySoft/Star Runner.ini
Or you could specify your ini file this way to point at a specific ini file
QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )
QSettings s("path/to/my/inifile.ini", QSettings::IniFormat);
And now an example of using the settings variable:
// set the setting
s.setValue("group_name/setting_name", new_value);
// retrieve the setting
// Note: Change "toType" to whatever type that this setting is supposed to be
current_value = s.value("group_name/setting_name", default_value).toType();
If you want to handle nested elements and gui layouts and design, I would look at XML or JSON. Both are supported by Qt.
XML is the native way that Qt Creator stores the UI files that are created by Qt Designer.
http://qt-project.org/doc/qt-5.0/qtxml/qtxml-module.html
http://qt-project.org/doc/qt-5.0/qtcore/json.html
Hope that helps.

Related

Retrieve different text from combo box in Qt Designer (pyqt)

this could sound strange and it is more a curiosity then a question.
I have a simple combobox with 2 elements in Qt Designer.
The 2 combobox elements are vertical and horizontal but for the script I'm writing I need to get only v or h.
Usually I easily do it with a loop like:
name = self.combbox.currentText()
if name == 'vertical':
name = 'v'
else:
name = 'h'
and that's ok.
I was just thinking if there is a way in Qt Designer to assign a kind of tag to the elements so the user sees the complete text but with the code it can be retrieved the tag.
Thanks to all
I don't believe you can do this with Qt Designer alone (see How can I add item data to QComboBox from Qt Designer/.ui file).
With some extra Python, though, you can add use setItemData() to add whatever extra data you want (How can I get the selected VALUE out of a QCombobox?) and retrieve it with itemData() and currentIndex().

AvalonEdit :: How do I preserve current state in the UndoStack?

I am currently working on implementing AvalonEdit in an HTML WYSIWYG/"Source Code" side-by-side editor. When the user switches from Design Mode (a RichTextBox) to Source Mode (AvalonEdit TextEditor) the XAML from the RTB is converted to HTML and written to the TextEditor.Document.Text property.
This apparently wipes out the undo stack.
How can I push the state of the Document to the UndoStack so the user can "go back"? I tried wrapping the write operation in a RunUpdate() IDisposable, but that didn't work:
using (var _ = TextEditor.Document.RunUpdate())
{
TextEditor.Document.Text = html;
}
Any assistance would be greatly appreciated.
Since this is a couple years late, I'm not sure if it answers the question directly. However, the current release of AvalonEdit functions such that setting TextEditor.Text will clear the undo stack, but modifying TextEditor.Document.Text will not. (This runs counter to the behavior observed by the asker, so perhaps it has changed in the couple years since). Looking at the source code, TextEditor.Document.Text appears to execute code equivalent to
this.Replace(0, this.TextLength, value);
so perhaps a similar call would work even on older versions of the library.

How to translate with Qt Linguist with label arguments (%1)

I got a line for a Qlabel like this:
QString(tr("Are you sure you want to delete the scene called %1 ?")).arg(variable);
Some people told me you can't translate that. They told me to append different strings with the parameters and the text...
But what about a phrase using various parameters? How does the translator know which order if it is appended in that order?.
Has no sense for me. There must be a way!.
Should should drop the QString(...) part, since tr() already returns a QString. Otherwise I don't see a problem with the translation of the following code:
tr("Are you sure you want to delete the scene called %1 ?").arg(variable);
In the Use QString::arg() for Dynamic Text part of the Qt documentation you can find more information.

Qt: read video dimension without creating a video player

I need to read the dimensions of a given video file (its width and height), without constructing a video player, like Phonon, e.g. My question is which class I should use to get access to this data. I have already tried using QPixmap and QMovie, but niether of them supports *.mov.
Thank you!
Pavlo, you can try this:
QMediaContent media("myMovie.mov");
QSize resolution = media.canonicalResource().resolution();
The code uses QMediaResource class from Qt Mobility project. I haven't tried it yet and I suppose you need at least a correct backend (plugin that is capable of reading MOV format). I'm giving this answer only from API overview.
Hope this helps.
I finally solved my problem and I thought I'd share my solution with everybody else.
In the class constructor I initialize the following two variables:
media = new Phonon::MediaObject(this);
videoWidget = new Phonon::VideoWidget;
I connect a signal of media to a slot in my class:
connect(media,SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this,SLOT(videoState(Phonon::State,Phonon::State)));
I let the user choose a video file:
QString filename = QFileDialog::getOpenFileName(this,tr("Choose video file"),QDir().homePath(),tr("Video files (*.mov *.mpg *.avi)"));
And apply this file to the media object:
media->setCurrentSource(filename);
Phonon::createPath(media,videoWidget);
Because media object is already connected to a slot, every change in media can be monitored with its help.
void VideoModuleDialog::videoState(Phonon::State newState, Phonon::State oldState)
{
if(newState == Phonon::PlayingState || newState == Phonon::StoppedState)
{
width->setText(QString().number(videoWidget->sizeHint().width()));
height->setText(QString().number(videoWidget->sizeHint().height()));
}
if(newState == Phonon::ErrorState)
{
QMessageBox::critical(this,tr("Video file error!"),
tr("Video file error: ") + media->errorString(),QMessageBox::Ok);
}
}
I must admit, however, that this code seems to me to be quite slow. Phonon library is used in my program only in one place, and this is here, in a dialog window where user can choose a video clip to embed, and i want the video dimensions to be read from file. It takes some time until this dialog window opens, so I guess, this solution is a bit too harsh for my problem. However, I was not able to find another solution. If there are different opinions as to the subject of this post, I'd be glad to hear them.

What determines sorting of files in a QFileDialog?

Users open files in our app through a QFileDialog. The order of the filenames is bizarre. What is determining the sorting order, and how can we make it sort by filenames, or otherwise impose our own sorting, perhaps giving it a pointer to our own comparison function?
The documentation and online forums haven't been helpful. Unless it's well hidden, there doesn't seem to be any sorting method, property, etc.
This is a primarily Linux app, but also runs on Macs. (I know nothing about Mac.)
Here is the juicy part of the source code:
QtFileDialog chooser(parent, caption, directory, filter);
/// QtFileDialog is our class derived from QFileDialog
chooser.setModal(true);
chooser.setAcceptMode(acceptMode);
chooser.setFileMode(fileMode);
QStringList hist = chooser.history();
chooser.setHistory(hist);
/* point "x" */
if(chooser.exec()) {
QStringList files = chooser.selectedFiles();
...blah blah blah...
From one of the answers, I tried an evil experiment, adding this ill-informed guesswork code at "point x":
QSortFilterProxyModel *sorter = new QSortFilterProxyModel();
sorter->sort(1); // ???
chooser.setProxyModel(sorter);
But this crashed spectacularly at a point about 33 subroutine calls deep from this level of code. I admit, even after reading the Qt4 documentation and sample code, I have no idea of the proper usage of QSortFilterProxyModel.
Are you using QFileDialog by calling exec()? If you are, you should have a button to switch the view to Detail View. This will give you some column headers that you can click on to sort the files. It should remember that mode the next time the dialog opens but you can force it by calling setViewMode(QFileDialog::Detail) before calling exec().
An alternative is to call the static function QFileDialog::getOpenFileName() which will open a file dialog that is native to the OS on which you are running. Your users may like the familiarity of this option better.
Update 1:
About sort order in screen cap from OP:
This screen capture is actually showing a sorted list. I don't know if the listing behaviour is originating from the Qt dialog or the underlying file system but I know Windows XP and later do it this way.
When sorting filenames with embedded numbers, any runs of consecutive digits are treated as a single number. With the more classic plain string sorting, files would be sorted like this:
A_A_10e0
A_A_9a05
Going character by character, the first 1 sorts before the 9.
.. But with numerical interpretation (as in Windows 7 at least), they are sorted as:
A_A_9a05
A_A_10e0
The 9 sorts before the 10.
So, the sorting you are seeing is alphabetical with numerical interpretation and not just straight character by character. Some deep digging may be required to see if that is Qt behaviour or OS behaviour and whether or not it can be configured.
Update 2:
The QSortFilterProxyModel will sort the strings alphabetically by default so there is not much work to using it to get the behavior you are looking for. Use the following code where you have "point x" in your example.. (you almost had it :)
QSortFilterProxyModel *sorter = new QSortFilterProxyModel();
sorter->setDynamicSortFilter(true); // This ensures the proxy will resort when the model changes
chooser.setProxyModel(sorter);
I think what you need to do is create a QSortFilterProxyModel which you then set in your QFileDialog with QFileDialog::setProxyModel(QAbstractProxyModel * proxyModel)
Here are some relevant links to the Qt 4.6 docs about it.
http://doc.trolltech.com/4.6/qfiledialog.html#setProxyModel
http://doc.trolltech.com/4.6/qsortfilterproxymodel.html#details
I don't think it depends upon the implementation of Qt libraries... But upon the Native OS implementation..
For example in Windows,
if you use QFileDialog, it will display the Files and Directories by Name sorted.. It is the same when used in other applications. In the sense that, if you try to open a file through MS- Word, it indeed displays the Files and directories as Name sorted by default..
And am not sure about other environments since am not used to them...
But in Windows, you can change the sorted order by right-click in the area of Files and Directories display and can select the options you like.. For e.g like Name,size,type, modified... And also which is similar, when you use an MS-Word application...
So, I believe it does depend on the Native OS implementation and not on QFileDialog's...

Resources