QFileDialog in a non-QT app - qt

I have an app that uses a 3rd party GUI framework, but I want to open files with using QFileDialog. I'm thinking of instantiating a subclass of QWidget that is invisible and serves the purpose of serving up the dialog.
Is there a better way to do this?

I don't see any need for an invisible widget, since the file dialog doesn't require a a parent widget in order to be shown up.
Since the dialog needs to have a Qt event loop running, you will need to either show the dialog modally using exec(), or using one of the static functions like getOpenFileName.
To use any of the widget classes, including the file dialog, you need to have an instance of QApplication, although that instance doesn't have to have its exec() method called.

Related

Is there an elegant way to manage properly a large collection of keyboard shortcuts in qt?

I'm learning qt by working on given examples. I've started to play a bit with keyboard shortcuts. To assign them I've used QtDesigner which is very handy, for example using "return" key to click a research QPushButton.
Here my main class is a Widget called TextFinder, which has a pointer on a Ui::TextFinder class, which is automatically built from QtCreator, which is the standard procedure to encapsulate user interface attributs and methods.
By assigning a shortcut using QtDesigner, the following lines are generated in the Ui::TextFinder class:
#ifndef QT_NO_SHORTCUT
findButton->setShortcut(QApplication::translate("TextFinder", "Return", nullptr));
#endif // QT_NO_SHORTCUT
where findButton is an alias for my QPushButton. So far so good.
Suppose now, in a large program, I want to implement many shortcuts to trigger many kinds of signal and I use QtDesigner to do so, it will generate these lines of code, possibly in different header files corresponding to different widgets. It will become quickly difficult to manage them and have a global vision of the "shortcuts state" of the program.
What would be a good method to manage all the shortcuts in the program at one place? Is it possible to make some config file to perform this task?
Typically in this case you go one level up from the shortcuts and look into Actions.
A QAction is a somewhat global (e.g. on the level of a QMainWindow) vehicle for something that the user can trigger (or toggle) through various ways. To name the most prominent ones,
Menu entries
(Toolbar) buttons
Shortcuts
You can manage your actions through Qt Designer (if you have a QMainWindow), or at a central place in your code. Note that the action encompasses not only the shortcut, but also title, icon etc.
You can arrange a toolbar in Qt Designer by dragging actions onto it, but you can also manually assign an action to any button in code.

Pass arguments to constructor of widget within QStackedWidget

I have a graphical application written in C++ using Qt for an embedded device, which uses a QStackedWidget holding a number of UI widgets. The UI is all designed in Qt Creator's designer tool. When the user navigates through the device's application the display to be shown at that menu option is selected from the QStackedWidget and this all works great.
I'm now wanting to pass in a pointer to some configuration which is read from file when the application starts, but I can't seem to find a way to pass this pointer as an argument into the constructor of a widget on the QStackedWidget. Can anyone help?
My current approach is to call a function I've written within the class of a widget on the QStackedWidget, which works but doesn't feel the best way to do it.
To my knowledge if you want to use custom constructors - with other kinds of arguments than just the QWidget * parent - you have to create the ui programmatically:
create your custom StackedWidget with a special constructor,
prepare the global interface using the designer,
then add the StackedWidget in the constructor of the class after the setupUi method.
The other way is to use an initialization method after the construction of the item, like you did.

How to use subclassed class in qt

I've subclassed qt's pushbutton and now I would like to use it in my project (instead of qpushbutton) - the problem I have is that even if I add #include "mybutton.h" in ui_class it gets overwritten and I don't know what else can I do it.
Is there a way to have this button within designer on a panel just like the ordinary qpushbutton is?
Never modify the ui_Class file yourself. Any change you make there will be overwritten when the .ui gets compiled. Instead use the promote to functionality within QtDesigner.
If some forms must be designed, but certain custom widgets are
unavailble to the designer, we can substitute similar widgets to
represent the missing widgets. For example, we might represent
instances of a custom push button class, MyPushButton, with instances
of QPushButton and promote these to MyPushButton so that uic generates
suitable code for this missing class.

Qt: How to initialize dialog widgets?

I would like to know what the established procedure is for initializing the controls within a Qt custom dialog box. In the code I am writing, the dialog would present a QListView containing directories from an object passed (by reference) to the dialog class during construction. When the dialog is displayed, I obviously want the list to display the directories currently configured in the object.
Where should this be done though? Perhaps in the overridden showEvent() method?
Background: I used to do a lot of MFC programming back in the day, and would have done this sort of stuff in the OnCreate method, or some such, once the window object had been created.
Thankfully Qt doesn't require you to do any hooking to find the moment to create things (unless you want to). If you look over the Qt examples for dialogs, most do all the constructing in the constructor:
http://doc.qt.io/archives/qt-4.7/examples-dialogs.html
The tab dialog example--for instance--doesn't do "on-demand" initializing of tabs. Although you could wire something up via the currentChanged signal:
http://doc.qt.io/archives/qt-4.7/qtabwidget.html#currentChanged
Wizard-style dialogs have initializePage and cleanupPage methods:
http://doc.qt.io/archives/qt-4.7/qwizardpage.html#initializePage
http://doc.qt.io/archives/qt-4.7/qwizardpage.html#cleanupPage
But by and large, you can just use the constructor. I guess the main exception would be if find yourself allocating the dialog at a much earlier time from when you actually display it (via exec), and you don't want to bear the performance burden for some part of that until it's actually shown. Such cases should be rare and probably the easiest thing to do is just add your own function that you call (like finalizeCreationBeforeExec).

Qt App Ui Multi-Language Support:Change Images Accordingly

I use UI Designer to set up the form layout. And I want to change the button background image to another one when language setting is changed in the system setting of the phone. How can I do this? I know how to support multi-language of the text , but I dont know how to support mult-language of the image. Thanks
When the language is changed, QCoreApplication::installTranslator() will be called. From the documentation:
Installing or removing a QTranslator,
or changing an installed QTranslator
generates a LanguageChange event for
the QCoreApplication instance. A
QApplication instance will propagate
the event to all toplevel windows,
where a reimplementation of
changeEvent can re-translate the user
interface by passing user-visible
strings via the tr() function to the
respective property setters.
User-interface classes generated by Qt
Designer provide a retranslateUi()
function that can be called.
So you should reimplement QWidget::changeEvent() in your toplevel window and change the image there if the type() of the event is LanguageChange.

Resources