Input Data in my Combo Box - c++

I feel really silly asking this question but how do you add any data to a Combo Box? Like I want to display A,B and C as my dropbox strings but I have tried out many things, still my Combo Box does not display anything.
I have tried setting the 'Data' property with A;B;C..still it doesn't show this.
I tried this in my Oninitdialog() function:
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_SENSOR_LIST);
CString string;
for(int i=0;i<10; i++)
{
string.Format("String %d", ++i);
pComboBox->AddString(string);
}
..
still no change. It may be I might not be doing something really silly but not able to figure it out at the moment. Kindly help.
Cheers.

Make sure your combobox has a height that can hold all entries. The height of a combo box doesn't affect the combo box itself, but the dropdown list.
You can change it in the dialog editor:
Click on the down arrow of the combo box, now there should be a rectangle that allows you to change the height of the combobox dropdown list. Make it large enough to hold all entries.

Related

Winforms controlling controls

I couldn't think of a coherent search term for my problem so please forgive me if this has been asked before.
I have 24 combo boxes sitting on a panel control, displayed in 4 rows of 6.
After the user defines the value for each combo and hits the "go" button, I add all combo boxes to list so I can use the values in another section of my program.
The problem is that the order the comboboxes are added to the list is messed up, compared to their visual layout on the panel, and I would like to define the order. Currently, it adds the 9th combobox to the list first then the 20th, 2nd, 16th etc etc.
I tried TabIndex but that didnt work.
Before I manually rename and relabel all of the boxes, any other suggestions will be gratefully received.
The controls of your form exist in Controls collection of the container controls, for example when you add a Panel and a Button to a Form and two ComboBox to the Panel, then:
Form.Controls contains button1 and panel1
Panel.Controls contains comboBox1 and comboBox2
Controls are added to the Controls collection, with the same order that you add them to designer. Open designer.cs, look at the end of InitializeComponent to see the order.
You can also see/change the order using Document Outline window.
That said, now it should be obvious that panel1.Controls.OfType<ComboBox>() returns combo boxes with the same order that you see in Document Outline (or based on their z-index, and it doesn't have anything to do with their x/y placements).
You may want to order them based on TabIndex, or any other property that you like:
panel1.Controls.OfType<ComboBox>().OrderBy(x=>x.TabIndex)
Note
In general, if you are going to use those values for a search, instead of relying on the order of values, a much better idea is creating a SearchModel class which has a few properties, the set those properties to the selected value of the corresponding combo box (manually or using databinding) then pass the search model to the other classes.

Submenu in a QComboBox

I have a few inter-dependent combo boxes and, as new options come, new entries need to be added. I managed to reduce the number of entries by grouping certain family-like members, but now I need a new combobox, and it's getting cumbersome, plus it starts to look ugly. So I thought of adding a submenu. Currently, my way of populating the combo box(es) is through a struct with enum and std::string, something like this:
QComboBox *combo {new QComboBox(vbox)};
for(int i=0; i<struct.LIST_ALL; ++i)
combo->insertItem(i, struct.names[i].c_str());
If I need to add a submenu at a certain position, how do I proceed? The documentation for QComboBox doesn't seem to have anything related to a submenu. Am I going the wrong way?
This answers my question.

QTableView re-focus the view to specific column

I have a QTableView with 80+ columns displayed in it. The subclass i have created for QTableView allows has functionality for the standard table stuff i wanted e.g order by columns move columns and rows hide column and rows etc.
However the problem i have is the view's focus. lets say you have all the data in the table, you scroll all the way to the right so you are looking at columns 70-80 (assuming 10 columns fit on the screen at once) if i click the header of row 80 (to order by column 80) the table reorders (as expected) however it also jumps the view to the last focused cell (which as far as i can tell is the cell that was last clicked)
What i want to do is not necessarily refocus on the column that was clicked because this might still change the view what im looking for is to just keep the view's focus exactly as it is instead of having it jump back to where the last clicked cell was?
Is there some flag im missing for focus policy or will i need to get the current view and set the view back to that view after mouse clicks that reorder the table and if so how could this be done.
Im aware i have provided no code for this question but it doesnt seem there is any needed since its not a bug im looking to fix more of a feature im unaware of, if you want to see any just comment
EDIT:
Im using a QSortFilterPorxyModel, this seems to get set once (when i call this->setSortingEnabled(true); after the initial call to this on contruction my table model never calls sort again. I have a slot linked to the header clicked signal, and i set the scroll bars to scrollTo() to the index clicked, but i think the sort is happening after this so it does nothing, any idea of what signal are emitted after sorting so i can catch them and then set the view back maybe?
thanks
Before you do your sort, store the current values for where the scroll bars are with
int vPos = yourQTableView->verticalScrollBar()->sliderPosition();
int hPos = yourQTableView->horizontalScrollBar()->sliderPosition();
then after the sort, set it back
yourQTableView->verticalScrollBar()->setSliderPosition(vPos);
yourQTableView->horizontalScrollBar()->setSliderPosition(hPos);
The signals you are looking for are signals of QSortFilterProxyModel inherited from QAbstractItemModel:
layoutAboutToBeChanged()
layoutChanged()
I couldn't reproduce your symptoms by creating a QTableWidget, filling it up with random stuff, and then sorting a particular column. The selected cell stays selected, but does not become visible if it has scrolled off screen.
So the question is, what is causing the behavior you're seeing. It sounds as though scrollTo() is being called by some other function. Since that's a virtual function, I would override it with a pass-through function and see when it's getting called.

AutoSuggestion in Combobox

I created a Combobox with CreateWindowEx. Everything goes well. But I would like to make a final feature: AutoSuggestion. The Combobox is used to do search text in a document, hence at some point, it have items that are the strings a user searched. The AutoSuggestion should be: drop down the list of items, find those items that begin with the string that a user typed in the edit control, but do not select one of them, do not display all other items, and finally do not change select item when keydown or keyup occurs, only highlight the item and select only when a user press Enter. Do you have any idea how to accomplish this task?
It sounds like you want Autocomplete functionality.

How do I set the QComboBox width to fit the largest item?

I have a QComboBox that I fill with QString using:
comboBox->addItem(someString);
When I start my GUI application the width of the QComboBox is always 70, even if the smallest item is much larger. How can I dynamically set the width of a QComboBox, for instance, to the largest QString within the comboBox?
Edit:
After some further testing I found the following solution:
// get the minimum width that fits the largest item.
int width = ui->sieveSizeComboBox->minimumSizeHint().width();
// set the ComboBoxe to that width.
ui->sieveSizeComboBox->setMinimumWidth(width);
Qt (4.6) online documentation has this to say about QComboBox:
enum SizeAdjustPolicy { AdjustToContents, AdjustToContentsOnFirstShow, AdjustToMinimumContentsLength, AdjustToMinimumContentsLengthWithIcon }
I would suggest
ensuring the SizeAdjustPolicy is actually being used
setting the enum to AdjustToContents. As you mention a .ui file I suggest doing that in Designer. Normally there shouldn't be anything fancy in your constructor at all concerning things you do in Designer.
According to the docs the default SizeAdjustPolicy is AdjustToContentsOnFirstShow so perhaps you are showing it and then populating it?
Either populate it first before showing it or try setting the policy to QComboBox::AdjustToContents.
Edit:
BTW I'm assuming that you have the QComboBox in a suitable layout, eg. QHBoxLayout, so that the size hint/policy is actually being used.
I was searching for a solution to only change the size of the dropdown menu of the combobox to fit the largest text without changing the size of the combobox itself.
Your suggestion (#Linoliumz) did help me find the solution. Here it is :
Suppose you have a combobox called cb:
C++:
width = cb->minimumSizeHint().width()
cb->view().setMinimumWidth(width)
PyQT :
width = cb.minimumSizeHint().width()
cb.view().setMinimumWidth(width)