QComboBox - Select no entry - qt

I have a QComboBox on my ui and set the model like this:
QStringListModel* model = new QStringListModel;
QStringList stringlist;
stringlist << "Test1" << "Test2" << "Test3";
model->setStringList(stringlist);
ui->comboBox->setModel(model);
Now I want to change the current index to be none (so that I get a blank combo box).
I already tried setting the current index to -1 with ui->comboBox->setCurrentIndex(-1);, but that results to an index aout of range exeption in qlist:
ASSERT failure in QList<T>::operator[]: "index out of range", file F:/Qt/5.4/mingw491_32/include/QtCore/qlist.h, line 486

Regular (not editable) QComboBox don't allow a state where "no item" is selected. The selection has to be valid all the time.
You will have to add an empty string item in first position, and you may want to check this topic to make this dummy item not selectable: https://stackoverflow.com/a/7633081/3336423
Edit: Actually, it looks like it's perfectly possible to set the selection to -1 for any combobox (editable or not). So there is no need to add an empty item as proposed above.

Related

Empty string returned while trying to retrieve the text contents of an element of QListView

I am trying to automate a PyQt based application that uses a QListView in icon mode using Squish
Inorder to select a specific item in the view, i need to first identify the text of the item. I am using the below code to do the same
targetList = waitForObject("{name='someListView' type='QListView'}")
object.children(targetList)[11].model().data(object.children(targetList)[11]).toString()
Here object.children(targetList)[11] is of type QModelIndex
But the above code always returns an empty string.
Is there any other way to retrieve the text data
I would rather use QListView API only. So, in case of valid targetList object, i.e. it's found by waitForObject function, I would write:
targetList = waitForObject("{name='someListView' type='QListView'}")
model = targetList.model()
col = targetList.modelColumn
idx = model.index(11, col)
itemString = idx.data().toString()
Here is an example:
def main():
# Use itemviews example included in Squish:
startApplication("itemviews")
# Configure QListView to use IconMode as
# mentioned by original poster:
obj = waitForObject("{occurrence='2' type='QListView' unnamed='1' visible='1'}")
obj.setViewMode(QListView.IconMode)
# Get desired item via object.children();
# this yields a "wrapped" QModelIndex which
# features a property "text" which contains
# the desired text:
it = object.children(obj)[4]
test.log("item text: %s" % it.text)

Getting char value from ComboBox in JavaFX

I need to get the selected value in a combobox as of a data type char. I know how to get the selected item it's the conversion which I've got stuck on. Any suggestions?
This is the combobox and it content:
idCharCombo = new ComboBox<>();
idCharCombo.getItems().addAll("A","B","G","H","L","M","P","Z");
Now i will be using this data in a method which passes an int and a char (bellow is the use of the method where the second element is still an object rather than a char):
if (checkStaffMemberById(Integer.parseInt(idNoTxtFld.getText()), idCharCombo.getValue()) == true){
AlertBox.display("ID Validation", "ERROR! ID Already Exists.");
hope i arranged adequately
Since your combo box appears to only hold single-character strings, and you want to treat them as chars, the most obvious thing to do is to use a ComboBox<Character> instead of a ComboBox<String>. I.e. replace your declaration, which presumably looks like
ComboBox<String> idCharCombo ;
with
ComboBox<Character> idCharCombo ;
and then you can do
idCharCombo.getItems().addAll('A','B','G','H','L','M','P','Z');
Then
idCharCombo.getValue()
will return a Character which will be autounboxed to a char as needed, so your method call
checkStaffMemberById(Integer.parseInt(idNoTxtFld.getText()), idCharCombo.getValue())
should work as-is.

Modify all selected cells in a QTableWidget?

It is a standard QTableWidget
All cells are QTableWidgetItem.
All cells are editable/selectable
Question: How can I modify all the cells I have selected?
Possible way is to use the dialog open. So the idea is like this :
Select the items
Make a button or something to open input dialog.
Apply the value of input dialog to all selected items.
I was facing a similar problem a couple years ago and I solved it like this:
I have inherited my own view and I have reimplemented methods commitData() and mouseReleaseEvent().
commitData takes all selected indices from the selection model and calls QAbstractItemModel::setData() for all of them. Data are taken from the editor like this:
QByteArray n = editor->metaObject()->userProperty().name();
if (n.isEmpty())
n = delegate->itemEditorFactory()->valuePropertyName(model()->data(index, Qt::EditRole).userType());
if (!n.isEmpty())
{
QVariant data = editor->property(n);
for (const QModelIndex & idx : selectedIndices)
{
model()->setData(idx, data);
}
}
mouseReleaseEvent() performs three steps:
Get a current selection from the selection model.
Call original event handler (QTableWidget::mouseReleaseEvent())
Restore selection: QItemSelectionModel::select()

I am having trouble adding elements to a referenced vector of a map

EDIT: Forgot to mention that my programming language is C++, and my IDE is Qt Creator.
I don't understand why I am having trouble adding elements to a referenced vector of a map (i.e. sometimes it works and sometimes it fails).
For example:
class C_html4_tags {
public:
C_html4_tags();
//.......
typedef std::vector<S_html_attr_value> TYPE_attr_values;
//.....
};
//...
C_html4_tags::C_html4_tags() {
//........
map<S_browser, TYPE_attr_values> attr_supported_attr_values_map;
S_browser dummy_browser; //a fake browser key for referencing a fully Html 4.01-compliant supported attr values list
dummy_browser.name = "Dummy";
//.....
TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
//......
**attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];**
//...
**attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();**
//......
**attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];**
//...
**attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();**
}
I do the bolded lines multiple times with a bunch of different attributes, with very little difference between them, without a problem until reaching this one attribute (a_tabindex_attr), in which the IDE doesn't report anything wrong when running it normally (except "The program has unexpectedly finished." However, when debugging it, it now reports:
Signal received
The inferior stopped because it received a signal from the Operating
System.
Signal name : SIGSEGV Signal meaning : Segmentation fault
And the backtrace points to the attribute I mentioned above, on the line of code which does this:
attr_supported_attr_values.clear();
Now, after adding some debugging cout lines to the code, I learned that what's happening is, for some reason, even after doing:
attr_supported_attr_values.push_back(attr_value);
the vector object which is the returned mapped value of:
attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
is not actually being changed when I push_back an S_html_attr_value object to the referenced vector. Now, I don't why that is, since I do pretty much the exact same thing in a bunch of other attributes' code before this one, without any problem, but for some reason now, on this one, it ain't adding the object to attr_supported_attr_values (which is a reference to the returned mapped value of 'dummy_browser'). And I know this for a fact, because I outputted the size of the internal mapped value object of the map (using an iterator for the map), and it was 0 after the call to push_back(). However, what is even more odd is, I ALSO outputted attr_supported_attr_values.size() after the call to push_back(), and that was 1! Now how could that be since they're both supposed to be the same object??
Note:
The full code of what I'm talking about is below (or at least the attribute code, minus the debugging statements...):
//a_tabindex_attr:
attr_desc = "Specifies the position of an <a> element in the\n"
"tabbing order for the current document.\n"
"The tabbing order defines the order in which\n"
"elements will receive focus when navigated by\n"
"the user via the keyboard. The tabbing order\n"
"may include elements nested within other elements.\n"
"Elements that may receive focus based on tabindex\n"
"adhere to the following rules:\n\n"
"1. Those elements that support this attribute and\n"
"assign a positive value to it are navigated first.\n"
"Navigation proceeds from the element with the\n"
"lowest tabindex value to the element with the\n"
"highest value. Values need not be sequential\n"
"nor must they begin with any particular value.\n"
"Elements that have identical tabindex values\n"
"should be navigated in the order they appear\n"
"in the character stream.\n"
"2. Those elements that do not support this\n"
"attribute or support it and assign it a value\n"
"of \"0\" are navigated next. These elements are\n"
"navigated in the order they appear in the\n"
"character stream.\n"
"3. Elements that are disabled do not participate\n"
"in the tabbing order.";
attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
attr_value_desc = "A number you specify for the tab index/order.\n"
"It must be a value between 0 and 32767.";
attr_value_content = "<i>number</i>";
attr_value = C_html4_attributes_obj.getAttrValue(attr_value_desc, attr_value_content,
attr_value_supported_browsers,
attr_value_required, attr_value_deprecated,
attr_value_notes, attr_value_tips);
attr_value.is_relative = true;
attr_supported_attr_values.push_back(attr_value);
try {
N_init_class_members::initAttr(C_html4_attributes::attr_tabindex, a_tabindex_attr, attr_desc,
attr_supported_browsers, attr_supported_attr_values_map, attr_required,
attr_deprecated, attr_notes, attr_tips);
}
catch (const char* exc) {
string exc_str = "Error! Call to N_init_class_members::initAttr() from\n"
"constructor of C_html4_tags class. That function\n"
"reported the following exception:\n\n";
exc_str += exc;
throw (exc_str.c_str()); //re-throw exception
}
a_tabindex_attr.is_standard_attr = true;
a_tabindex_attr.is_optional_attr = true;
//cleanup from attribute operations so we can begin working on the next attribute:
C_html4_attributes_obj.clear(); //wipes the slate clean for all the supported attributes' properties except content
attr_desc.clear();
attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();
attr_value_desc.clear();
attr_value_content.clear();

(Py)Qt - QTreeView, Model, insert row

I am little bit confuse.
I am working with QTreeView as model I assigned QSortFilterProxyModel.
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qsortfilterproxymodel.html
//treeview
self.ProxyView = QtGui.QTreeView(self.centralwidget)
//model
self.ProxyModel = QtGui.QSortFilterProxyModel(self)
self.ProxyModel.setSourceModel(QtGui.QStandardItemModel(0, 3, self))
//assign model to tree
self.ProxyView.setModel(self.ProxyModel)
On button click I want to add row to this tree.
Here is my slot
def pushButton_addRow(self):
self.ProxyModel.insertRow(0)
self.ProxyModel.setData(self.ProxyModel.index(0,0), "hi")
It doesnt work, it works only for first row, then I added empty rows. But wenn I fill second cell it works.
self.ProxyModel.setData(self.ProxyModel.index(0,1), "hi")
Any Idea?
Maybe your new QModelIndex has wrong parent()?

Resources