Control the position of the icon in a QLineEdit - c++

I've subclassed QStyle to control the aspect of my app. After customizing the aspect of the QLineEdits, I need to add an icon inside the QLineEdit and it has to be clickable by the user. I've seen that I can use the method QLineEdit::addAction to add an action and an icon in the QLineEdit. The problem arises with the position in which it draws the icon because as I have the customized QLineEdit, the icon draws too near the edge of QLineEdit. I need to separate more icon QLineEdit edge. Does anyone know how to control the position of the icon in the QLineEdit?

Related

Is there a way to attach or anchor two QWidgets together?

I'm getting started with Qt and decided to build a full-screen text editor. I want to have a button (button with arrow in screenshot) attached to a QDockWidget which opens and closes it so the button is always visible to the right side of the screen and stay anchored to it when dock is visible or resized.
My current app is a simple fullscreen textEdit set to centeralwidget in Mainwindow.
I haven't found a way to do this yet with layouts or existing addAnchor() functions so any help or direction is appreciated.
You can achieve what you want by using a container for your text edit and the button. A QWidget instance can be used as an "invisible"*** container for other widgets.
So in Qt Designer you add a widget as a central widget of the main-window, inside this widget you add the text edit and the button, then you set a vertical layout for this container widget.
Don't forget to restrict the docking widget to only dock to the right side, you can do that with: dock->setAllowedAreas(Qt::DockWidgetArea::RightDockWidgetArea); //assuming dock is the pointer to your QDockWidget.
In case you want the dockWidget to be able to dock to any side and the button to follow, you can do that too, but it get a little bit more complicated. Basically you need to connect a slot to dockLocationChanged of your dockWidget and based on where it's docked you need to set-up a new layout for the container widget to be vertical or horizontal and the order of the textEdit and the button based on the side the dock happened.
LE:*** you will most likely need to set the margins you want, since both the widget and it's layout can have them and the actual content might have higher spacing than you want.

How to add vertical splitter for Viewer and QScrollArea?

I have a Viewer contained inside a QFrame like this:
The white area is Viewer and grey area is QFrame.
What is want to have is another QscrollArea with the Viewer layed out vertically with a vertical splitter like this:
But even after selecting both QScrollArea and Viewer and then clicking the layout vertically in splitter button. The splitter does not appear? Any idea as to how to achieve this?
This is the answer you're looking for:
https://stackoverflow.com/a/28313475/1839954
On a not layed out area, add the children widgets, select them with CTRL key pressed and then lay them out using a vertical splitter (there's a button on the layout toolbar above the form preview)
Assuming that by "viewer" you mean a QListView for example, i had no problem to define this using Qt Designer. You should select the QFrame and the QScrollArea from the object inspector and then click on the vertical splitter layout inside Qt Designer.
Here is the result :
Here is my object inspector :
Hope this helps.

Setting and manipulating Icons in QT

I want to add icons in QMainWindow and when i will click that window it should perform some action like popup some window. So what should i use for the icon menu?
You could use the QToolButton class to accomplish this task.
It is possible to set it to only contain an image/icon without text.
l buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction(). It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts.
A tool button's icon is set as QIcon. This makes it possible to specify different pixmaps for the disabled and active state. The disabled pixmap is used when the button's functionality is not available. The active pixmap is displayed when the button is auto-raised because the mouse pointer is hovering over it.
The button's look and dimension is adjustable with setToolButtonStyle() and setIconSize(). When used inside a QToolBar in a QMainWindow, the button automatically adjusts to QMainWindow's settings (see QMainWindow::setToolButtonStyle() and QMainWindow::setIconSize()). Instead of an icon, a tool button can also display an arrow symbol, specified with arrowType.
So, you would use these methods:
QAction * QToolBar::addAction(const QIcon & icon, const QString & text)
Creates a new action with the given icon and text. This action is added to the end of the toolbar.
and
toolButtonStyle : Qt::ToolButtonStyle
This property holds whether the tool button displays an icon only, text only, or text beside/below the icon.
The default is Qt::ToolButtonIconOnly.
To have the style of toolbuttons follow the system settings (as available in GNOME and KDE desktop environments), set this property to Qt::ToolButtonFollowStyle.
QToolButton automatically connects this slot to the relevant signal in the QMainWindow in which is resides.
As you can see, the default is icon only.

Adding icon to QDialog

How can I add an icon to QDialog, but not the icon on a frame, I need to place an icon inside this dialog.
You can use QLabel and set the pixmap properties to display the image. It's not an icon though

Adding scroll bar to widget containing a layout in QT C++

I am new to QT and I am creating a widget that has a gridlayout. The gridlayout contains a matrix of QLineEdit widgets. The window resizes to fit the layout but when layout is large it goes off screen. When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.
I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.
I tried the following with no luck:
Window->resize(QSize(500,500));
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(Window);
where window is the widget containing the layout. Also, the window closes when after executing "scrollArea->setWidget(Window);" and I dont why.
If someone can help me out I would really appreciate it.
Thank You!
For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?
For the menu bar, why don't you take it out of the widget that is scrollable. You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part). Is that what you are looking for?
I fixed my problem by creating a QMainWindow with the menu bar. Then created a widget which includes the layout, set the Scroll Area to the widget. Finally set the central widget of the main widow to the scroll area.