Delete QListWidget item by text content - c++

I need to delete a qlistwidget item by text content
I tried:
QString mstring = "Programmer II";
QList<QListWidgetItem *> items = ui->listJobs->findItems(mstring, Qt::MatchExactly);
if (items.size() > 0)
ui->listJobs->takeItem( ui->listJobs->currentRow() );
...and various permutations, but I'm missing something. The code above compiles, but does not delete the item from the qlistwidget.

The code doesn't indicate what the value of currentRow is, but findItems doesn't set it, so it's unlikely to correlate with the value you're trying to remove. I don't see any way to use the results of findItems and get the row(s) you want to remove. I think you have to loop through the contents, compare the text of each item, and then remove the ones that match. You'll probably want to do the loop in reverse order; otherwise, once you've removed an item, the loop counter will no longer match the list item's row numbers.

Related

Inserting elements in a QSet<QString> and retrieving , removing the last item of the QSet

I have the following dataset:
QSet<QString> set;
I tried to insert 3 elements to the QSet.
set.insert("A");
set.insert("B");
set.insert("C");
After adding the above three elements,its showing the size of my QSet as 3, but total 16 elements are visible,among which 13 elements are being shown as Empty.
And every time,I try to debug this, the element "A" is on the top of the list once and next time, "C" appears on the top of the list.
Due to this, I am not able to retrieve the last element of the QSet. I want to retrieve the element,which is inserted at the last to theQSet.
I Am using the following lines of code,for retrieving and removing the elements from QSet.
QString str1 = QString( *( set.end() - 1 ) ); //to get the last element
set.remove( *( set.end() - 1 ) ); //to remove the last element
I think,it has to do something with properly initializing my QSet, but I still could not figure out the problem exactly
I would be glad if someone can explain me better
Thanks in advance!
QSet does not store it's elements in any particular order. As you have noticed, the no particular order is not even stable between runs of your program. In a very real sense, there is not a "last element", which is why there is no front or back member
The order of insertion is not recorded anywhere. If you want to maintain the insertion order, you will need a SequenceContainer, such as std::vector or QVector.
QSet is based on Hash table and so unordered. Use QMap, if you need ordering by key.

Tkinter: Adding a entry at index 3 without there being one at index 1 or 2

The problem im getting is if type something in my entry box it first fills index 1 and 2 of the listbox before finally typing into the 3rd index.
def country_get(event):
listbox.delete(3)
listbox.insert(3, country_label.cget('text') + event.widget.get() + '\n')
title_text=StringVar()
entry_country=Entry(master, bg="wheat3", fg="dark slate gray", textvariable=title_text)
entry_country.bind('<KeyRelease>', country_get)
entry_country.grid(row=4, column=1)
I want to be able to type at any index of the listbox whether it be the 3rd or 5th, without having anything at the previous index's.
You can't do what you want. The listbox isn't designed to have empty rows. If you want empty rows, you will need to insert empty strings.
Depending on how many entries I'm using or need, i just insert empty strings like so:
listbox.insert(0, "")
listbox.insert(1, "")
listbox.insert(2, "")
listbox.insert(3, "")
listbox.insert(4, "")
It appears that the listbox is empty when it fact it is just filled with empty strings. There are most likely other ways to get around this, but for what I need my program to do, this is what worked for me.

Unity Slider mapped to list<float>?

I currently have a list<float> that are arranged in an increasing order. My sliders min.value is the lowest value of my list (i.e. first element of my list) and sliders max.value is equal to the last item of my list.
What I want to do is that whenever my slider is at a specific value or +-0.1 to one of the elements on my list, I Want to be able to get the index out that specific element to perform a task.
Any suggestions?
Someone asked for code, I don't even like this code since it only gives me the right value once I press "play" from zero, but then it removes my data, so I can't go back with the slider
this code is inside update void, and isPlaying is true when I press a "play" button
if(isPlaying){
timeSlider.value += Time.deltaTime;
timer = timeSlider.value;
print(timeSlider.value);
if((newTimes[0]>=(timer-0.1f)) && (newTimes[0]<=(timer+0.1f))){
print("time: " + newTimes[0]+ " at run time: " + timer);
NewTimes.RemoveAt(0);
}
}
You can set the slider to use whole numbers in the inspector and use the value as your index?
My first thought would be:
Set Slider Steps to Floats in your List
-> Perform task on List[currentSliderStep] as needed

QTableWidget having multiples lines in cell

I'm using a QTableWidget for a simple table. Now each cell should contain two numbers, but I don't want them to be left to right, but for readability on top of each other. I'm using Qt 5.8.
Hence, I tried something like this:
QTableWidget* table=new QTableWidget;
auto item = new QTableWidgetItem(QString("%1\n%2").arg(1).arg(2));
table->setItem(0, 0, item);
Interestingly, the outcome was that the newline \n was completely ignored. I obtain a cell containing 12. Replacing \n by <br> didn't helped.
Any idea, what I'm doing wrong/missing?
This should work, just try to resize the height of your row to give the item enough space.

How to access the list of items in a combobox

What is the "object type" of the .List property of a combobox in vba? I am having quite a struggle in accessing the items when I treat it like a an Array of strings.
Let's say I want to go through the list and check if any of the items match a certain string, how would I go about that?
Levraininjaneer, I think I might have some help for you...
I've made a windows form with a combobox, a button, and a listbox... The combobox has some items in it, like Item 1 to Item 3, "ABC", "DEF", "GHI"...
Now, you say you want to access the items in your list? Well, try this out...
string[] array = new string[comboBox1.Items.Count];
int itemCount = comboBox1.Items.Count;
for (int i = 0; i < itemCount; i++)
{
array[i] = (string)comboBox1.Items[i];
string item = array[i].ToString();
this.listBox1.Items.Add(item);
}
MessageBox.Show(array[1]);
MessageBox.Show(array[4]);
And it will do this:
And the message boxes at the bottom of the code will display "Item 2" & "DEF"
If you want to "save" an instance of an item in the list box, you can also do it like this:
string arrayItem = array[3].ToString();
MessageBox.Show(arrayItem);
This will display a message box saying "ABC" as the index (number in [square] brackets, it starts at 0 generally... So if you put array [1] . it's not the 1st item, it's actually the 2nd item... If you wanted to get the last item, and if there's 6 items, it would be:
array[5];
And also, if you wanted to check if an object contains a certain string, you can always use the .Contains method of a string
.Contains("Item")
Hope this helps :)
Win10Pro(x64)
Visual Studio 2015 Community
C#
WindowsForm project