How to set maxLength of QTableWidget item - qt

Is there a way to set the max length of a QTableWidget item (table cell)? I have a table that is editable and I would like the max length to be 3 when a user is editing a cell.

Create a subclass of QStyledItemDelegate and grab the QLineEdit that is created for the cells. You can use QLineEdit::setMaxLength(int) to set the maximum length that you want.
See the documentation of QStyledItemDelegate

tableWidget->setColumnWidth(column_number, length)
but 3 is small value for length, try with 30.

Related

QGridLayout insert qwidget

I would insert QWidget in QGridLayout. Currently, when I put a QWidget in my grid, the number of columns and rows fits it. For example, when I insert a QWidget in row 15 and column 15 then my grid will be 15x15 whereas I would like it to be 20x20.
Is it possible to give a number of rows and columns?
Thanks.

How to get the specific cell with (column x and row y) in TableView? javafx

given a tableview object
I have got the tableposition for the cell I wanted to get.
I also got the row and column number of the cell I wanted to get.
Let's say it is row 3, column 2, how do I get the cell in row 3 column 2?
I want the cell obj itself, not the value of the cell.
If you are using JavaFX 8u40 or above you can get it by using the AccessibleAttribute.
TableCell cell = (TableCell) tableView.queryAccessibleAttribute(AccessibleAttribute.CELL_AT_ROW_COLUMN,rowIndex,columnIndex);
Remember you may get null value if the cell is not visible in the tableView viewport.

Tkinter Grid Columnspan ignored

Consider the following python script
#!/usr/bin/env python
from Tkinter import Tk, Label
width = SOME_VALUE_HERE
root = Tk()
label1 = Label(root, text='1 columns wide')
label2 = Label(root, text='%i columns wide' % width)
label1.grid()
label2.grid(row=0,column=1,columnspan=width)
root.mainloop()
When I run this, no matter what value is set for 'SOME_VALUE_HERE', both labels take up half the window, regardless of whether or not Grid.columnconfigure is called, or the sticky parameter is used in grid().
Unless I've overlooked something, I would have thought that setting the columnspan would force the second label to be 'SOME_VALUE_HERE' times as wide as the first.
Have I misunderstood how grid works? How would I go about achieving this behavior?
By default, empty grid column are zero width, so you described the following table. Grid geometry manager will by default try to optimize the screen real estate used by your application. It will integrate all the constraint and produce the fittest layout.
+---------------+---------------++++
| 0 | 1 |||| <-- 2,3,4 empty, 0 width
+---------------+---------------++++
| 1 column wide | 4 column wide |
+---------------+---------------++++
To provide strict proportional column width, you have to use the uniform option of columnconfigure. uniform takes an arbitrary value to designate the group of the column that share these proportions, and the weight argument is used to properly handle widget resizing.
label1.grid(row=0, column=0)
label2.grid(row=0,column=1, columnspan=width)
for i in range(width+1):
root.grid_columnconfigure(i, weight=1, uniform="foo")
Note that with only these two labels, you could achieve the same layout by adjusting the width of column 1. Differences will occur still while you populate column 2,3,4...
label2.grid(row=0,column=1) #no columnspan
root.grid_columnconfigure(0, weight=1, uniform="foo")
root.grid_columnconfigure(1, weight=width, uniform="foo")
When you put something in column 1 with a columnspan of two (or more) that means it will be in column 1 and column 2 (etc). However, if there is nothing controlling the width of a column, that column will have a width of zero. You need to force column 2 to have a widtheither by putting something in there, giving it a minsize, or forcing uniform columns.
When I look at your code, I can't guess how wide you think column 2 should be, and neither can the computer.
I had a similar problem only to discover that the elements are limited by the widest widget. We can safely say that Tkinter is configured to make your app uniform in that it should be a regular repeating square/triangular structure. Solution to override default options.
With the Tkinter's automatic optimization in mind, play with the width and height of largest widget (grid box) and relate the other boxes to it proportionally.
Using the above method use columnspan to adjust the width.
Configure the widths by use of columnconfigure()

How to word wrap text in the rows and columns of a QTableWidget?

I tried:
QTableWidget *j = new QTableWidget (10000, 5, centralWidget);
j->setColumnWidth (0, 500);
j->setColumnWidth (1, 30);
j->setColumnWidth (2, 30);
j->setColumnWidth (3, 320);
j->setColumnWidth (4, 310);
j->setWordWrap (true);
Also tried resizeColumnsToContents and resizeRowsToContents, but failed.
If the text is longer than the set width, I want the sentence to break down.
Currenty, the lengthy part of the sentence just doesn't get shown.
setWordWrap defines the behaviour of the text, without altering column size. If you need to keep column width fixed, call resizeRowsToContents after the insertion of the item to the cell (I assume you're adding text to the table via QTableWidgetItem).
Please notice that if any of the words contained in the item are wider than column size, text will be elided from that point on (by default you will see ellipses: ...). If you want to change such behaviour you need to reimplement item's painting function or stretch your columns.
This will adjust the word wrapping automatically every time a column resizes:
connect(
tableWidget->horizontalHeader(),
SIGNAL(sectionResized(int, int, int)),
tableWidget,
SLOT(resizeRowsToContents()));
As mentioned in the question comment, setting the row size explicitly to some value seems to work:
tableWidget->resizeRowsToContents();
tableWidget->verticalHeader()->setDefaultSectionSize(50);
I note that for my code, I did not have to explicitly call setWordWrap in order to have cell contents be word wrapped.

Getting QGraphicsTextItem length?

Is there anyway to calculate the text's length when TextWidth = -1?.
I have a rectangle that has a QGraphicsTextItem in it, and I want to change the rectangle's width when characters exceed the rectangle.
I found this post by stopping on the same problem.
i'm using text->boundingRect().width()to get the width.
Perhaps it helps anybody
textWidth = -1 means, that
"[...] the text will not be broken into
multiple lines unless it is enforced
through an explicit line break or a
new paragraph."
(QTextDocument::textWidth())
So, if you want to get the length of your QGraphicsTextItem you can't use textWidth, but instead you need the actual length of the String within this QGraphicsTextItem. Have a look at QGraphicsTextItem::toPlainText(), which returns a QString. Call size() on that string.
int length = my_graphics_text_item.toPlainText().size()
Now you have the number of characters in this string and can implement a resize function to make your rectangle grow, when there are too many characters. It's a kind of workaround, but I hope it helps solving your problem.
You could also create a QFontMetrics([font of your QGraphicsTextItem]) instance and call its width(QString) function to obtain the width of the passed string in pixels, were it drawn in the specified fontfamily/-size/-weight.
Just obtaining the character count is only reasonable when using a monospaced font. In all other cases it's not a good idea.

Resources