How to remove span from QTableWidget in PyQt4 , Python 2.7? - python

I have a QTableWidget with some of it's cells merged together using the following command:
QTableView.setSpan (self, int row, int column, int rowSpan, int columnSpan)
This gives me something like this:
Now, I am looking for a way to restore some of the previously merged cells back into original states (original rows and columns). When I looked into the documentation, there is this command called:
QTableView.clearSpans (self)
But as far as I understand, this reset all the spans that were previously set.
Is there any way to reset the span setting of just a specific cell without affecting the other merged cells? ( For example, just resetting the span setting on row 4, column 0 without affecting the other merged cells)

Is there anyway to reset the span setting of just a specific cell
without affecting the other merged cells? ( For example, just
resetting the span setting on row 4, column 0 without affecting the
other merged cells)
How to
Just re-use the function setSpan to set the span of the target cell back to 1x1.
In your example, use setSpan(4,0,1,1) to reset the span setting on row4, col0.
Example
Take your image as an example. Use setSpan(2,1,1,1) to "remove" the span setting on the target cell.

Related

How to insert multiple values into specific treeview columns?

I have a database returning the total of several column and I am trying to display it in a treeview. If I do
for i in backend2.calc_total()[0]:
treeviewtotal.insert("", END, values=i)
I get
which is not what i want as i want everything to start from "food" column onwards. I cant make date a iid as i already have an iid that I am referencing to my database.
If I do
list2 = ['Date', 'Food', 'Transport', 'Insurance', 'Installments', 'Others']
for i in range(len(backend2.calc_total()[0][0])):
treeviewtotal.insert("", 0, list2[i+1], values=backend2.calc_total()[0][0][i])
I get this
instead, all the totals get stacked into 1 column (which is scrollable).
Any way to achieve my aim of allocating the respective totals to the respective column in a same row? Thanks!
With reference to the first attempt, the following solves the problem:
for i in backend2.calc_total()[0]:
treeviewtotal.insert("", END, values=([], *i))
values= takes in a list. Therefore we add an empty space by using [], but since i itself is already a list, we need to "flatten out" the list by doing *i.
Please correct me if I used any parts of the code wrongly. Still trying to learn =)

Excel autofilter range: keep the range unchanged / avoid that rows are added automatically

I have an issue to limit the Excel autofilter to the set range and display a sticky bottom row below that range...
This works well (I am using Python xlsxwriter):
worksheet.autofilter('A1:D111')
It results in a filter list range in Excel (Office 365) of $A$1:$D$111.
However, if I write a cell below the autofilter range with:
worksheet.write(111, 3, 'Total filtered selection', format_string) #adds string to Excel row 112
Then this row is also included in the filterrange (the filter now ends at $D112 for some reason... see picture).
Due to this the bottom row is not sticking to the bottom of the selection on changing the filter, which is what I wanted in order to show a total for the selection (using =SUBTOTAL(101, E1:E111) which only includes filtered rows as intended).
What am I doing wrong? Thanks!
By using the same range for a chart series the autofilter range remains unchanged:
As dwirony suggested, it seems to be standard behavior for Excel to add new data (rows) to the autofilter. This way the filter range is extended with my bottom row showing subtotals and the row is hidden when the filter is reapplied.
However, if you apply a chart series to the same range of cells as the autofilter range then the autofilter remains unchanged! I.e. my autofilter range was 'A1:D111' and changed to 'A1:D112' on adding content to row 112. However, if I create a chart series for range 'A1:D111' and add content to row 112 then the autofilter range will remain unchanged.
PS note: I also tried to keep the range fixed by defining a named range (without using it) but this does not help / the filter still adds new rows outside the named range automatically. In xlsx writer:
workbook.define_name('Filterrange', '={}!$A$1:$D$111'

How to process dataframe without creating NaN's everywhere?

I have a set of columns that contain dates (imported from Excel file), and I need to process them as follows:
If a cell in one of those columns is blank, set another column to 1, else that column is 0. This allows me to sum all the 1's and show that those items are missing.
This is how I am doing that at present:
df_combined['CDR_Form_notfound'] = np.where(df_combined['CDR-Form'].mask(df_combined['CDR-Form'].str.len()==0).isnull(),1,0)
A problem I am having is that I have to format those columns so that A) dates are trimmed to show only the day/month/year and B) some of the columns have a value of "see notes" in them, instead of being a date or blank. That "see notes" is essential to properly accounting for missing items, it has to be there to keep the cell from flagging as empty and the item counting as missing (adding to the 'blank cells' count). The actual problem is that if I run this code before the .isnull code above, evry blank becomes a NaN or a nan or a NaT, and then NOTHING flags as null/missing.
This is the code I am using to trim the date strings and change the "see notes" to a string...because otherwise it just ends up blank in the output.
for c in df_combined[dateColumns]:
df_combined[c] = df_combined[c].astype(str) # uncomment this if columns change from dtype=str
df_combined[c] = np.where(df_combined[c].str.contains("20"), df_combined[c].str[:10], df_combined[c])
df_combined[c] = np.where(df_combined[c].str.contains("see notes"), df_combined[c].str, df_combined[c])
I think my problem might have something to do with the dtypes of the columns. When I run print(df.dtypes), every column shows as 'object', except for one I specifically set to int using this:
df_combined['Num'] = df_combined['Num'].apply(lambda x: int(x) if x == x else "")
Are you trying to count NaNs?
If yes, you can just do:
len(df.loc[:, df.isnull().any()])
I see that you mention "blank" because it is comming from excel, so what you could do is to transform these blanks into nan before running the command above using:
df['CDR-Form'].replace('', np.NaN,inplace=True)

Fill the gap in every row of ObjectListView how?

Here's the problem, if you don't use check-state column in ObjectListView, there's always this gap in every row:
Especially when you want to use a small column to show row numbers:
This is so NOT looking good, how do you make each row expand to the start?
I've just been adding an extra first empty column that has no no valueGetter and a width of 0 so it doesn't show, that way the following column starts on the left edge.
Add the following as the first column.
ObjectListView.ColumnDefn(title="", valueGetter="", maximumWidth=0)

How to make QTableWidget's columns assume the maximum space?

The columns of my QTableWidget do not fill in the space of the table, so that an empty space is left on the right hand-side. How to make the columns of my QTableWidget assume the maximum space so as to fill in this space?
The headers of the table have methods for controlling this:
header = table.horizontalHeader()
header.setStretchLastSection(True)
or:
header.setResizeMode(QHeaderView.Stretch)
I don't know of any method to set this property to the QTableWidget's content. However, I could use the following to get the columns to resize:
def resizeEvent(self, event):
self.setColumnWidth(0, event.size().width())
This resizes the first column only. To resize all columns, one should get all children item and apply column width / number of items.

Categories