How to modify QCombobox current item using PaintEvent() in Qt - qt

I have a Combobox which has 3 items i.e. Item 1, Item 2 and item 3. I added these 3 items in .ui file and also added an image each for all using a resource file. Now when I am running the application, it shows the first item in combobox along with a image towards left. When i click on dropdown, I can see all the items along with images.
What I basically want to do is just display the image in the combobox and when user clicks on dropdown, it should show the image and text too.
Scenario: Items and images inside Combobox are added as follows:
Image 1 Item 1
Image 2 Item 2
Image 3 Item 3
When I run the app, it should display in combobox as
Image1
since first item gets displayed by default. I think it can be done when painting the current selection, QCombobox uses a re-implantation : void MyCombo::paintEvent(QPaintEvent *e) which is something I am not aware of.
Can anyone help me in this?? :)
How can I achieve it???

You hould inherit a QComboBox and reimplement void QComboBox::showPopup () [virtual]
P.S. If you reimplement this function to show a custom pop-up, make sure you call hidePopup() to reset the internal state.
Second idea I have is to implement a model for combobox but i don't now exatly is there any roule for popup elements.

Related

How to open editor outside of qlistview?

The QAbstractItemView::openPersistentEditor method calls the createEditor method reimplemented in a class inherited from the QStyledItemDelegate class. It opens a widget inside the current item. But my task is to show the editor widget in some custom way. Say, under the current item, but with it's right border outside of the scroll area of the view. And it's height being far more, than just one item's height. Is there any way to do it?
It opens a widget inside the current item because the current item its parent.
One of the solutions is the following: you provide a simple editor widget which is shown inside the current item, which really does not provide editing, only data exchange.
But this editor widget creates custom widget as a dialog or as a widget whose parent is the topmost ItemView. Or you can delegate the creation of the custom editor widget to the ItemView. The result of the editing is sent to the in-item editor widget which does the job of providing the ItemView with newly edited data.

Buttons in vertical layout in QDialog with return value

I want to create a QDialog with Qt that looks somewhat like this:
The desired properties here are:
I can add a text to the dialog explaining the question behind it
I can add several buttons that are in a vertical layout
I can retrieve the value of the clicked button, i.e. I know if the user cancelled or clicked 1, 2 or 3 - ideally I can emit a signal with the corresponding value as parameter.
The dialog has a certain minimum height and width.
I have used a QMessageBox before for this purpose, but I can't get it to use a vertical layout. I have experimented with a QDialogButtonBox and a QDialog, but I can't really figure out how to get my desired return value easily.
A piece of code creating this dialog with code how to retrieve the clicked value would be great!
Did you try a QDialog widget? you can add QDialog without buttons to your project. In UI designer, add Vertical layout, label, and buttons.
In your class define your signals and emit it when the user clicked on buttons.

QCompleter popup position

I'm having trouble trying to move the QCompleter popup view position.
I tried the QCompeter:complete and it's pops the completer view in the position as I wanted.
But if I start typing it close it, and open the completer in the 'default' position.
I also tried the QCompleter:setPopup() function.
I create a QListView and I tried to moved to different position.
And still the QCompleter popup view remains in the same position.
In my project I'm using a QFrame that wrap QLineEdit.
And I want that the completer view will get the QFrame position.
I succeed to set the completer view width via setFixedWidth() function.
but not to move the position.
Any suggestions ?
Thanks.
I suggest setting the CompletionMode to InlineCompletion, so there will be no popup. Then make your QListView indepedant of the QLineEdit; just react to signals that indicate when a view types some text, leaves the QLineEdit, etc (hint: subclass QListView) and sets the text in QLineEdit when a user selects a value from the list.
I think it will be difficult to override the placement since QCompleter takes ownership of your QListView. (Personally I think it does not make much sense to place the completion list somewhere else than next to the input field, but alas...)

Customizing the checkboxes of the items of a QTreeView

I'm having the following situation: I need to create a custom tree control, whose checkboxes are also customized. I have easily made most of the customizations for the tree control by using style sheets; I have succeeded adding checkboxes to the QTreeView's items, but I'm having big problems with customizing them - I need to display a custom image for the checked state, and another for the unchecked state.
The place I concluded this can be done is in my subclass of QStyledItemDelegate, in the paint event (i.e. CheckBoxItemDelegate::paint). What I need is to display the text, the icon, and the checkbox for the item. But the problems are:
- I can't get the style of the item (which I set using the stylesheet) - otherwise my text can be drawn with the incorrect color;
- I don't know the rects of each subitem (the checkbox, the icon, the text);
- I don't know how to get the icon of an item (given its QModelIndex) to draw it.
P.S. I had subclassed the QTreeView (obviously), and, as I am working with QFileSystemModel, I have subclassed it too in order to add the checkbox functionality to it.
Can anybody help me, please?
Is QStyledItemDelegate::paint the proper place for changing the visuals of the checkbox of the tree items? If yes, can you please give me a small example or something, how I can do that?
This is how I rendered checkable items inside a QTreeView with two images (eye opened/eye closed, to represent their visibility state) instead of a checkbox:
ui.myTreeView->setStyleSheet(
"QTreeView::indicator:unchecked {image: url(:/icons/eye_grey.png);}"
"QTreeView::indicator:checked {image: url(:/icons/eye.png);}"
);
Items should be set as checkable, of course. Hope this helps.

QML ListView Delegates z-order are always on top of everything else

I have a QML ListView with a custom widget (I call it PayloadOdometerRecord) as a delegate. This PayloadOdometerRecord contains a couple of other custom widgets (TextBox) that when clicked will change state and display a keyboard for user input.
The ListView works correctly until the user clicks one of these TextBox widgets to display the keyboard. The keyboard gets clipped by all of the delegates below the delegate that is displaying the keyboard.
When using this TextBox widget in a non-ListView, it works correctly...the keyboard is fully visible.
Does anyone have any idea why this is happening? My goal is to have a full-screen keyboard that shows up and is not covered by any other components.
Thanks!
Can you show some code? What is the parent of your keyboard component?
Z works only between siblings. The keyboard should be a child of the root element and have a higher z value than the other children of root to make it work.
If there are other components in front of the keyboard, you can try to set the keyboards z-property to a large value (e.g. 1000) → Qt Doc

Resources