How to add unselectable instruction text to the QComboBox - qt

I have a QComboBox with the items.
I want to put some instruction as top item in my combo box such as "Select number..". It shouldn't be selected too.
is there anyway to do it in QComboBox widget?
Thanks for reading.

You can initially put an instruction item as a regular item and remove it from the combo box when it is clicked at the first time. This signal signal
should do the job
void highlighted(int index);

You can make the item unselectable via the list model. Something like:
comboBox->model()->itemData(0)->setEnabled(false)
But you might need a cast in there, not sure: I use PySide so I don't :-)

Related

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...)

Does Qt have combo boxes like Word?

In Microsoft Word, if you want to use Bullets you have a combo box (If we can call it a combo box) to select the shape of the bullet (by clicking the little triangle) or you can just apply the default one by clicking the combo box's button.
Actually the combo box in Word has two parts. For an example let's consider a situation where I want to have a combo box in Qt that has these items as menu items:
"Restart", "Shutdown" and "Log off". User needs to choose one of them but he also can apply previously selected item by clicking its button exactly like Windows shutdown menu in start. You can click Shut down or select another option.
How can we achieve this in Qt?
If you are pursuing after a menu that looks like in the second picture, you can use QToolButton to achieve your goal. Use a QToolButton with popupMode set to MenuButtonPopup. It will render a control something similar to following.
Then you can style the look & feel further more using Qt Style Sheets. Read this example on how to style a QToolButton.
Create a QMenu dynamically, so you can attach it to the QToolButton at run-time in such a way that all items will be included in the menu except for the default item. Default action has to be assigned to the QToolButton itself.
You can use void QToolButton::setMenu (QMenu * menu) to assign a QMenu to your QToolButton at run-time.
If you are trying to design a control that is in your 1st screenshot, you will have to create a custom Qt control I guess. There is no default control available, which can yield that look & feel out of the box.

How to set icon of the line edit in a QCombobox widget without inserting the item into the list?

I want to set the icon on the left side of the QCombobox widget. I know I can insert a item first and then set the icon of the inserted item and then select this newly inserted item. However, I would like to do that without inserting a new item into the drop down list for special reasons. Windows ComboBox control allows us to change the icon of the edit box by using an index of -1. I don't know how to achieve that with QCombobox.
Thanks for any comments!
Never tried it myself, but here is an idea.
QComboBox is based on Qt's model/view framework, so the items are contained into a QStandardItemModel which can be accessed with QComboBox::model().
The steps would be:
Instantiate a QStandardItem
Use setIcon() and setText() on the QStandardItem (or use the proper ctor)
When you want to add the item to the Combo list, append it thru the model.
Example:
QStandardItem* item = new QStandardItem(theIcon, theText);
[...]
QStandardItemModel* comboModel = qobject_cast<QStandardItemModel*>(theCombo->model());
comboModel->appendRow(item);

Changing multiple selection mode for QListView

In QListView, i'd like to disable mouse drag multiple selection - that is, mous down on a row, drag the mouse down and select the rows below it while dragging.
I'd still like row selection using CTRL-mouse click.
Is that possible?
It seems you've set the list view's selection mode to QAbstractItemView::MultiSelection. Try setting it to QAbstractItemView::ExtendedSelection with:
listView->setSelectionMode( QAbstractItemView::ExtendedSelection );
and see if that helps.
I think the easiest way to do it would be to create a derived class from the QListView and then override its mouseMoveEvent function. This function in the Qt Code for the QListView looks for a dragging state and creates a rectangle. I think something like this may work, but I didn't test it:
void DerivedListView::mouseMoveEvent(QMouseEvent *e) {
if (state() != DragSelectingState)
QListView::mouseMoveEvent(e);
}

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

Resources