How to take user input In Qt with image background Widget view? - c++

I want the user to enter a password to starts the Qt application. To take the input from the user I am using the QInputDialog widget.
The problem is I doesn't know how to add this widget on the above of some image showing window widget, this is my code
bool ok;
QString text = QInputDialog::getText(this,
tr("QInputDialog::getText()"),tr("Enter the password:"),
QLineEdit::password,"Qt-application", &ok);
if (ok && !text.isEmpty())
textLabel->setText(text);
The above-used method is looking very casual, can anybody share the sample code to get the password from the user so it looks something good with the proper size and alignment adjustment.

If you aren't satisfied with the way QInputDialog looks, why don't you use the form designer in Qt Creator to make your own fancy looking password input dialog?
In Qt Creator, select File -> New File or Project. Then choose the Qt template under Files and Classes and then select Qt Designer Form Class. From there, go through the wizard and it'll create an empty dialog for you (*.ui). Open the .UI file, then you can drag 'n drop your desired widgets, set properties, configure layout, etc...

Related

how to add a custom widget to Qt 4.8.6

I'm trying to figure out how to add a custom widget to Qt Designer. So far I've been unsuccessful. Very new to Qt. I'm running Qt 4.8.6. I know there is a newer version but due to project constraints I have to stick with this one. Essentially I'm need to add a hex spin box, i.e. a spinbox that counts in hex rather than in decimals. I even found code that implements it. It does explain how to integrate it into Qt Designer, however it is explained for version 3. Can someone offer assistance?
Any help is greatly appreciated
Read about creating designer widgets and using them, but briefly:
Subclass QDesignerCustomWidgetInterface to create your custom designer plugin for your custom widget
Build and install your designer plugin
Tell designer about your plugin location, or just make sure it's installed to $QTDIR/plugins/designer
There's also an example to follow.
Here is a documentation. At short, you just add any widget (e.g. QSpinBox to draw it nice in the Designer) to your form and convert it to the needed one.
To add a custom widget to Qt designer simply follow the steps:
In Qt designer create a new widget File->New->Widget.
Add your UI etc for this widget and save it->->MyWidget.ui
Create a class "MyWidgetHandler" to handle this widget in a MyWidgetHandler.h & cpp
In your MainWindow/Dialog wherever you want to display this widget add a "Widget" from the "Containers" section of the designer.
Now right-click the "Widget" container you just added and click "Promote to..."
This should open a dialog. You can select here the base class to inherit properties in your case QComboBox or just select the QWidget class.
Next enter the full class name i.e with namespaces eg: "blah::MyWidgetHandler"
In the Header file section simply add the path to the header file for this class. This should be a resolvable path. Eg: "UI/MyWidgetHandler.h" or "C:/UI/MyWidgetHandler.h"
Now click "Promote" and save your MainWindow.ui
The Property Editor should now show your custom class type eg: blah::MyWidgetHandler. and the properties from the base class you selected.
Don't forget to setup the UI in your handler class and include header in the Mainwindow handler.
Cheers !!!

Qt Designer (creator) widget box like widget

Is there a good example or complete code for Qt Designer (creator) widget box like widget with QWidget or QFrame within? So it should work like QToolBox or QTabWidget but be expandable: possible to view few pages (widgets) at a time, not single one like QToolBox or QTabWidget or QStackedWidget provide.
Or need to write myself using QTreeWidget for example? Need same look and feel with same root decoration on category button (or panel) in in Qt Creator or with line like following:
+ ——- Category 1 ————
widgets
widgets
+ ——- Category 2 ————
widgets
widgets
etc
If it exists with plugin to Qt Creator - even better.
Created sample widget and plugin for Qt Designer, put it on GitHub if somebody else need this and want to improve: https://github.com/akontsevich/WidgetBox. It is ready for using however some points good to have improved:
Style category button (or panel) closer to Qt Creator (Designer) look
or like to LibreOffice Writer Properties Tool Box on the right side
Fix isPageExpanded propery change in Qt Designer
Hope for community participation or suggestions on these improvements.
Sample screenshots of current state:

Qt c++ : Dynamic GUI from multiple .ui files

I have been developing an GUI application in Qt. In which i need to load the GUI from different form files (.ui).
Please be patient as I am going to write a lot of things.
The idea is to keep my application completely flexible that the user can change the interface by simply editing .ui files and there will be no need to modify the source code for new element added to .ui files.
I am also trying to divide the software in multiple layers. (I'll try to clarify below.)
For example: (Please see the attached image. I have merged all the images in single file because my account is not authorised to add more than two images.)
I have the main window as show in image (Main)
The first.ui file contains primary widget which is tab widget and looks like as shown in image (First)
The second.ui file contains second widget which I need to add into the tab widget.
the second form looks like as shown in image (Second)
At the end the complete interface will look something like as shown in image (Final) Image Link Here
In the code I have done the following:
/*
Created a Main Window with menubar.
The menubar has an action to select and open a first.ui file.
The File opened is loaded by QUiLoader loader.
First.ui file containes a QTabWidget with two tabs and a label.
In label, i have given a path to another file second.ui
loader loads the opened file and returns the QWidget
The returned QWidget has been set as a CenntralWidget in MainWindow.
Then I have read the first.ui file to find the text given in label (which is the path to another file second.ui).
Once I have obtained that path, I have loaded the second.ui file using loader.
This file contais a widget having form layout and few buttons.
Now I am trying to add this obtained widget to the Tab loaded from previous file.
CASE 1:
I successfully managed to do it by using:
selectedTab->layout()->addWidget(otherWidgetObainedFromSecondFile);
but in this case, the problem is that, I cannot access the element of form and buttons.
That is, The buttons and form fields doesnt look like an active element.
They just look like a snapshot fixed in the tab.
CASE 2:
So, in contrary I decided not to directly add the widget to tab.
But i created another MainWindow, set the second widget as centralWidget to this new main window.
And then added this mainwindow to tab.
but it also gave me the same result.
*/
Please suggest me a solution.
I am open to different suggestions but I want to keep the idea same that is the GUI has to remain in layered form (loads from multiple ui files)
I will really appreciate your help.
You can use QFormBuilder for this.
Assuming your Tab Widget is named TabPane
QFormBuilder loader;
QFile file(":second.ui");
file.open(QFile::ReadOnly);
QWidget *pane=loader.load(&file,this);
file.close();
qDeleteAll(ui->TabPane->findChildren<QWidget *>(QString(),Qt::FindDirectChildrenOnly)); //remove any existing child widgets from TabPane
ui->TabPane->layout()->addWidget(pane);
If I understand you correctly you have to use addTab method of QTabWidget

Custom menu actions in Qt Designer

I'm currently trying to use Qt Designer to build a GUI and I would like to customize slots of my menu actions.
E-g: I'd like the user to press a menu action and it'd show a widget if it is hidden or hide it if it is already visible.
Basically, what I want to do is execute some code of mine and not the default actions such as show() or hide().
So I'm wondering if I should create a subclass of QMenuBar, add custom slots to it, then create a plugin to use it inside Qt Designer or if I should create a subclass for QMenu or QAction ? Or maybe it isn't the right way to do that ?
I'm working under Visual Studio and I'm only using Qt Designer, not Qt creator.
I'm new to GUI and Qt programming and I'm a bit lost here.
Thanks in advance :)
You have basically 2 options:
Implement the custom logic in the Mainwindow sublcass.
For this, you simply add the slots required for your handling in the class, and make them available in Qt Designer. You can do this:
either in the Signal/Slot editor and click "Modify" and then click on the + Symbol. By this you make new slots available in QtDesigner;
or when your slot is called on_(senderName)_(signalName), Qt autowiring will automatically connect the signals, and you don't have to do this in code or desinger.
Create a QMenuBar subclass and implement the custom logic there.
Your case tell Qt Designer to select your specific subclass as replacement for the default QMenuBar by right-clicking on it, and select "Promote to...". In the new dialog, you can specify your custom class that will be used as replacement in actual code, but in design time a QMenuBar is used. With this mehtod, you don't have to write a separate plugin to make your class available in Qt Designer.
Note that with the second option, your custom logic will only be called when the actions are triggered through the menu bar, not by shortcuts or tool buttons
Create a slot in your class:
onMenuActionTriggered()
Use the connect() to react on action's signal:
connect(ui.myAction, SIGNAL(triggered()), this, SLOT(onMenuActionTriggered()));
In your slot you can do whatever you want.
Another solution (not my favourite one, but possible) is to use the auto-connect functionality, which means, by declaring a slot 'on_myAction_triggered()' (where myAction is the name of your QAction) you don't need to use the connect() since it is automatically connected by Qt
The menu bar is automatically added to any new form derived from QMainWindow (the default when creating a gui application, but you can create new main windows using file->new file or project... and selecting Qt->Qt Designer Form Class ).
To add options to it you simply click in the area labeled "Type here" and write your option text. When you do so an action will appear in a list in the lower part of Qt Designer. Right click on that action and select "go to slot". It will pop up a dialog with "triggered()" already selected for you. Simply click "Ok" and Qt Creator will take care of all the details and transport you to the body of the slot function.

retranslateUi() clears QLabel text

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.
As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code