How to make a QCombobox only display content (not editable, not selectable) - qt

I have a problem with QComboBox like this: I have a QComboBox for configuring color, I use QColorDialog in QComboBox. In display mode, I just want to display value of QComboBox for user, user cannot edit value or select other value from QComboBox. I tried 2 solutions like this:
use set property editable = false: user still chooses other value by selecting combobox
use set property enable = false: user cannot edit or select, but color in combobox is grey, not value that I configured, ex: red.
I googled but don't grab any answers. Somebody helps me?

You could disallow changes by creating a slot for currentIndexChanged:
and changing it back.

Related

How to change maxVisibleItems in QFileDialog filter combobox

I have QFileDialog with 11 items in the filter. By default QComboBox shows 10 and for the rest you need to scroll - i.e. you have to scroll for 1 item. I would like to change it. QComboBox has maxVisibleItems property, but how can I access QFileDialog's filter combobox? I can see it in QDialogPrivate::qFileDialogUi->fileTypeCombo, but I don't have access to it.
Thanks!
The following solution only works for non native file dialogs (i.e. you must set the QFileDialog::DontUseNativeDialog flag).
In that case, QObject::findChild can be used to find the combobox in the widget. The following example assumes the dialog has only one combobox. If thats not the case, you must find the correct one via QObject::findChilden, which returns a list of all children.
Example code could look like this:
auto dialog = new QFileDialog(parent);
dialog->setOptions(QFileDialog::DontUseNativeDialog);
auto cBox = dialog->findChild<QComboBox*>();
if(cBox)
cBox->setMaxVisibleItems(11);
else
qCritical() << "Unable to find any combobox child";
//setup and show
Important: If you can't find the child, it's possible you have to first show the dialog before you can modify the box. In that case, place the code after the show call, and it might work.

How to set display text for ComboBox (checkable) in Qt

I follow these to create checkable combobox
ComboBox of CheckBoxes?
http://programmingexamples.net/wiki/Qt/ModelView/ComboBoxOfCheckBoxes
However when I do a this->Model->clear() then add items, the combobox text (the text combobox displays before user clicking anything) goes blank. The items will still show and are checkable when click on the combobox. I suspect the clear() remove the header and causes this, however I try setHorizontalHeaderLabels etc but I still can't set the combobox text. What am I missing?
Try setting the ComboBox's selected index after you add items. Like:
ui->combobox->setCurrentIndex(0);
Because it could be that after you clear the ComboBox, its display index may have gone to -1 and stayed there even if you add items.

A suggest to solve QLineEdit property

I'm working with Qt5...and I'm tryng to set a default value in QLineEdit in case of I wrote nothing in the linetext area.
Is possible to do that?
You can do this for example by connecting
void QLineEdit::editingFinished () [signal]
and check in the slot actual text from QLineEdit and if necessary set yours.
If you want user to have empty field when they start editing
First use placeHolderText property of QLineEdit to set the default value to display.
Then connect editingFinished (or possibly some other, check them out) signal to your own slot, where you check if text is empty (and then use the placeHolderText value) or if user entered something.
If you want to leave default text for user to edit
Instead of using placeHolderText, simply set contents of the QLineEdit to desired default value, when you create it. Then in the slot for editingFinished, if user made field empty, restore the text to default.

click feature in Qt

I just want to clarify, weather the feature is present or not in Qt.
The scenario is like this,
I have a list view with items, I want to place the icon to the listview when the item is selected.
The selection I mean is, first time when I click item should be selected, next time if I click the same item then it should display some icon. Please note
It is not the double click. again if do select some other item same feature should continue
So is there any feature which handles this feature by default, any property or flag which I need to set to listview to behave like this or manual implementation
Is required for this.
No problem (: Now I understand what you mean... So if you click on an item it should be selected (for example highlighted in blue) and then when you click on this item again, an icon should be displayed.
I can't think of a regualar way to do this, there is no such flag or something.
The easiest way I can think of would be to store the index in a QList when you select it. And when you deselect it, you delete the index from the list. SO, when you click on an item you can check if it is in that list and if so you can display your icon.
Another way would be to create your own type of QModelIndex. Everytime, this index is selected, you set a bool like is_already_selected on true. When clicking on this item again you check this bool and then decide whether an icon should be displayed or not.
For further information, see: QListView, QAbstractItemView::currentIndex, QModelIndex

Is it possible to catch a comboBoxes value before change with a change event

I am displaying a combo box in something of a WYSIWYG preview. I want the user to be able to click on the combo box and see the options inside, but I don't want them to be able to change the value. I tried using preventDefault() on the change event but it doesn't work. I don't want to disable it because I do want the user to be able to "look inside" the dropdown.
So I'm trying to block the change, but can't. My next resort is to change the selected index back to what it was before the change, Is there any way to do this within the scope of a ListEvent.CHANGE event listener?
Current Workaround is to basically re-assign the controls selected item the same way I am defining the selected item when I originally build it (a default selection). So a user sees their change then it immediately changes back to the default selection.
Are you sure that a combobox is what you want? could you do the same thing with a list component that is not selectable?
update:
If you must use a combobox and you dont want the lag from listening for the event and resetting the control, I see two possible options. You could subclass the control and make your own. When you do, hijack any methods that set the value besides the initial selection.
Or, you could try something like this: http://wmcai.blog.163.com/blog/static/4802420088945053961/. The site seems like it is in another language but the code is still there. It will allow you to make your options disabled, so the user cannot choose one of the other options.
HTH

Resources