I've been trying to add a simple combobox with four items to a stupid table widget cell and I still can't get it placed in the correct row and column. The best I could get was to make it show up only if I tell the combobox its parent is the table widget, the problem is it gets located at position x 0 y 0 of the table widget. I use setCellWidget method and it still doesn't populate the correct cell no matter what row and column I specify there. What should I do then?
I've found this example in PyQt but whenever I try to implement a ruby-esque version of it on Ruby it just doesn't work.
This code does what you were looking for: generate a n×m table, and insert a combobox at a given cell, here at row 4 / column 2.
require 'Qt4'
qt_app = Qt::Application.new(ARGV)
win = Qt::Dialog.new
win.show
table_widget = Qt::TableWidget.new(win)
layout = Qt::GridLayout.new(win) # not required
layout.addWidget(table_widget) # not required
rows = table_widget.rowCount = 7
columns = table_widget.columnCount = 4
(0..rows-1).each do |row|
(0..columns-1).each do |column|
text = "cell #{row}/#{column}"
table_widget.setItem(row,column, Qt::TableWidgetItem.new(text))
end
end
combo_box = Qt::ComboBox.new(table_widget)
combo_box.insertItems(0, ["one", "two", "three", "four", "five"])
table_widget.setCellWidget(4, 2, combo_box)
table_widget.show
qt_app.exec
Related
I'm working on a lua script where I need a list of buttons to display. I have the following method to add a button to the list buttons:
function addButton(name, text, func, activeColor, passiveColor, xmin, xmax, ymin, ymax, type)
buttons[name] = {}
buttons[name]["text"] = text
buttons[name]["func"] = func
buttons[name]["active"] = false
buttons[name]["activeColor"] = getColorOrDefault(activeColor, ACTIVECOLORDEFAULT)
buttons[name]["passiveColor"] = getColorOrDefault(passiveColor, PASSIVECOLORDEFAULT)
buttons[name]["xmin"] = xmin
buttons[name]["xmax"] = xmax
buttons[name]["ymin"] = ymin
buttons[name]["ymax"] = ymax
buttons[name]["type"] = type
print("added: "..table.getn(buttons))
end
This function is called twice to add 2 buttons, but the output is:
added: 0
added: 0
what could be a reason my elements are not added to the table?
They're being added--You're just using the wrong method to detect them.
As you can read about in this question, using table.getn only returns the amount of array slots (numeric indexes, such as buttons[0] or buttons[23]. Note that this only counts as an array slot if there are no null indexes in between buttons[1] and buttons[23]!
Using this debugging code, we'll see that your items are indeed being added to the buttons table:
for name, button in pairs(buttons) do
print(name, button)
end
This is also how you'd get a list of your buttons :-)
This question already has an answer here:
Trying to take a value from a dictionary of a ComboBox and instert the key in a textEdit
(1 answer)
Closed 2 years ago.
I have a drop down which the displayed text is populated from a csv:
fish_events_terms = gpd.read_file("domains/FISH/events/thesaurus_terms.csv")
self.comboActivityType.addItems(list(map(lambda x: x.upper(), fish_events_terms['TERM'])))
I want to display the above but record the uid of that value in this case CLA_GR_UID
So the user sees some text from the TERM column and the value of CLA_GR_UID is passed.
I'm not sure if I understand the question correctly, but if you want to store extra data in an item in addition to the displayed text, you could add the items on-by-one by using QComboBox.addItem(text, user_data), i.e.
from PyQt5 import QtWidgets, QtCore
import pandas as pd
class Widget(QtWidgets.QWidget):
def __init__(self, parent = None):
super().__init__(parent)
self.combo = QtWidgets.QComboBox(self)
# some data
self.df = pd.DataFrame({'TERM': ['apple', 'banana', 'cherry', 'date', 'grape'],
'UID': [1, 2, 3, 4, 5]})
# for each row in dataframe, add item with value in 'TERM' column as text and value in 'UID' column as data
for row in self.df.itertuples():
self.combo.addItem(row.TERM, row.UID)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.combo)
self.combo.currentIndexChanged.connect(self.combo_index_changed)
def combo_index_changed(self, index):
# retrieve user data of an item in combo box via QComboBox.itemData()
print(f'index {index}, text {self.combo.itemText(index)}, uid {self.combo.itemData(index)}')
if __name__ == "__main__":
app = QtWidgets.QApplication([])
w = Widget()
w.show()
app.exec()
Suppose I have the following setting on a Google Sheet:
Column A containing a list of different items (Apple/Melon/Grapes), column B containing Data Validation drop-down menus with either option 1 or 2.
What I want is that if I select option 1 for any of the items, the value of the corresponding cell in column A is going to pasted in D2. If I select option 1 for another item, the value will be pasted in D3, and so forth, thus building a secondary list without leaving any blank cells in between. If I select option 2, the item should be ignored.
Ideally, the order of the items in column D would follow my actions chronologically, i.e. if the item in A3 is the first item I select option 1 for, then it shall be on the top of the column D list at all times, even if later on I select option 1 for A1 as well (which shall then sit on the second position of the D list).
Is it possible to be achieved?
You can do this via Apps Script with a simple onEdit trigger, using its event object.
Select Tools > Script editor to open the Apps Script editor.
Copy this function and save the project (check inline comments):
function onEdit(e) {
// Get the range that was edited (column, row, sheet, value):
var range = e.range;
var sheet = range.getSheet();
var col = range.getColumn();
var row = range.getRow();
var value = range.getValue();
var sheetName = "Sheet1"; // Name of your sheet (the tab) (please change if necessary)
// Check that edited column is B, edited value is 1, and edited sheet is "Sheet1":
if (sheet.getName() === sheetName && col === 2 && value === 1) {
// Get length of non-empty cells in column D:
var columnD = sheet.getRange("D:D").getValues().map(function(item) {
return item[0];
}).filter(function(item) {
return item !== "";
});
var firstEmptyRow = columnD.length + 1; // First empty row in column D
var itemToCopy = sheet.getRange(row, 1).getValue(); // Get value from column A to copy
sheet.getRange(firstEmptyRow, 4).setValue(itemToCopy); // Write value to column D
}
}
Now, every time you edit column B and the edited value is 1, the corresponding option from column A will get copied to column D.
Reference:
onEdit
Event objects: Edit
delete range D2:D and paste in D2 cell:
=FILTER(A:A; B:B=1)
I have 2 columns x and Y. I need to filter grid based on values of 2 columns.
For example: Suppose my filter condition is true, then it should check the value of both the columns and if any of the column value is true it should not filter the row. If none of the value is true than the row should be filtered.
I can think of one quick solution for this. The below code will set normal filters on 2 columns, then you just change the columns LogicalOperator.
private void Filter() {
Infragistics.Win.UltraWinGrid.FilterCondition fc = new Infragistics.Win.UltraWinGrid.FilterCondition();
fc.CompareValue = "someValue1";
fc.ComparisionOperator = Infragistics.Win.UltraWinGrid.FilterComparisionOperator.Equals;
ultraGrid1.DisplayLayout.Bands[0].ColumnFilters["col_x"].FilterConditions.Add(fc);
Infragistics.Win.UltraWinGrid.FilterCondition fc2 = new Infragistics.Win.UltraWinGrid.FilterCondition();
fc2.CompareValue = "someValue2";
fc2.ComparisionOperator = Infragistics.Win.UltraWinGrid.FilterComparisionOperator.Equals;
ultraGrid1.DisplayLayout.Bands[0].ColumnFilters["col_y"].FilterConditions.Add(fc2);
// set the logical operator of the columns on the band
ultraGrid1.DisplayLayout.Bands[0].ColumnFilters.LogicalOperator = Infragistics.Win.UltraWinGrid.FilterLogicalOperator.Or;
}
Is this what you were looking for?
i have 50 checkboxes for 50 american states. The user can choose all 50 or only 1( so basically any number he wants). Based on his choice, I want to insert or update the table in sql server 2008. e.g-
Color = blue and chk1=check, chk2= check and chk3 = check (chk = checkbox). now the user wants to ad 10 more states to this or remove these 3 and add 5 more. so u basically get the idea. the table in database looks like this - ID Color State_id there is a table called states, so stateid shall come from there. so how do i do a loop insert or update in vb.net?
I would use a data source and a checkboxlist. You already have your states in a datatable, populate a checkboxlist with a databound selection from a SqlDataSource (or datatable of your own choosing). Then when you click your button just iterate through the following loop:
Dim dt as New myTypedDataTable ' constructed from datasource
Dim color as String = "Blue" ' Filled however you set color
For Each item As ListItem In Me.CheckBoxList1.Items
If item.Selected Then
Dim row as myTypedDataTableRow = dt.NewmyTypedDataTableRow
row.Color = color
row.State_id = item.Value
dt.Rows.Add(row)
End If
Next
Once you've got the datatable filled with the rows in question you could either use the SqlDataSource to perform an insert operation or perform the insert operation atomically. There are several ways to accomplish it, but this is probably the simplest way to iterate through the items given the data structure you described.
In this context, I sometime go the easy way and delete all the user items store in the database and then only do insert.
This can be a problem if you have for example a insert_date. In this case, you'll need to list of user selected options. Loop in the new list, if the item is not found in the old list then it's an insert. Loop in the old list, if the item is not found in the new list then it's a delete.
I would use bitwise operation and a long variable in .net (mixed with an enum for the flag)
one field in the db and way easier to play with what the user select
small sample
Enum state As Long '64 enum maxium since long = 64 bits
ALABAMA = 1
ALASKA = 2
NEVADA = 4
ARIZONA = 8
ARKANSAS = 16
CALIFORNIA = 32
COLORADO = 64
CONNECTICUT = 128
DELAWARE = 256
'etc etc etc
End Enum
Module Module1
Sub Main()
Dim userselect As state = 0
Console.WriteLine("your checked box state")
Console.WriteLine("in this case im using the order of the enum for selecting")
Dim checkbox = New Boolean() {True, False, False, True, False, False, True, False, False}
For i = 0 To checkbox.Length - 1
userselect = CType(userselect + If(checkbox(i), (2 ^ (i + 1)), 0), state)
Next
For Each s As state In [Enum].GetValues(GetType(state))
If (userselect And s) > 0 Then
Console.WriteLine("selected " & s.ToString)
End If
Next
Console.WriteLine("Value of userselect is " & userselect.ToString)
Console.ReadKey()
End Sub
End Module
OUTPUT:
selected NEVADA
selected ARIZONA
selected COLORADO
Value of userselect is 76