In my application i need to save some information of qtabelwidget .Can any one explain me with an example how to save the contents of Qtabel widget in an array.
and can i restrict the cell text maximum length like an lineedit.?
Thanks.......
In my application i need to save some information of qtabelwidget .Can any one explain me with an example how to save the contents of Qtabel widget in an array.
and can i restrict the cell text maximum length like an lineedit.?
Thanks.......
To store contents in an array you can convert each row to a QString and save it in an array of QStrings.
Qt Code:
#include <QtGui> int main(int argc, char *argv[]) { QTableWidget w; QTableWidgetItem *item; QString str; QString *strArray; int rowCount, colCount; w.insertRow(0); w.insertRow(0); w.insertColumn(0); w.insertColumn(0); w.setItem(0, 0, item); w.setItem(0, 1, item); w.setItem(1, 0, item); w.setItem(1, 1, item); rowCount = w.rowCount(); colCount = w.columnCount(); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { str += w.item(i,j)->text(); str += '\t'; } strArray[i] = str; str.clear(); } w.insertRow(0); w.setItem(0, 0, item); w.setColumnWidth(0, 300); w.resize(600, 400); w.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
Yes. Use a QTableWidgetItem subclass that discards/truncates text longer than what ever limit you wish to impose or provide the QTableWidget with a QStyledItemDelegate subclass that provides a length-limited QLineEdit when asked for the editor.
thanks ashkan and chris.
Here is another problem.
when i am trying to set the max length of a text i dint find any function to do it.
can i set the text length for a particular column.
please provide me an example or a piece of code.
And in my application i want to give an option to the user through a button.when the user clicked on that button a row should be added to my table.
my code is.
onbuttonclicked()
{
ui->mytablewiget->insertrow(ui->mytablewiget->rowcount());
}
but no row is added while running.
is there any other way>
Thanks.
No, it is the same question you started with.
You already have the answer. Use a delegate or have the model item silently discard the extra characters. The are examples in the docs and this forum.can i set the text length for a particular column.
please provide me an example or a piece of code.
This will not even compile (unless you have subclassed QTableWidget to add insertrow()). If you are going to post code, copy and paste it from a compiling program and use [code] [/code] tags.Qt Code:
onbuttonclicked() { ui->mytablewiget->insertrow(ui->mytablewiget->rowcount()); }To copy to clipboard, switch view to plain text mode
You have the correct function but unfortunately "but no row is added while running" is an inadequate description of the problem, why you think it is broken, what you have done to diagnose it etc.
i have gone through the documentation but still dont know hoe to use QStyledItemDelegate to limit the text length.can u please explain with an example.?
and i am trying to read the data from a row.and set the text of columns of perticular row to lineedits.
i did like this.
Qtcode:
void trial:: on_tableWidget_doubleClicked(const QModelIndex &index)
{
item = new QTableWidgetItem(0);
item = ui->tableWidget->itemFromIndex(index);
}
while running it shows the error
/usr/include/QtGui/qtablewidget.h:333: error: 'QTableWidgetItem* QTableWidget::itemFromIndex(const QModelIndex&) const' is protected.
how t solve it or is there any other way to read the complete row?
Hi chris.
I try to understand the usage of item delegate but not get the required points.
so Can u give me the a piece of code to limit the text length of qtablewidget .
u need to set the property on the cell widget.
hope it helps
Bala
It has been a whole month. Have you tried anything at all for yourself? Have you showed what you have tried and asked for help identifying where it fails? Have you asked for help interpreting the documentation you read or examples you have studied?
Here... a complete example that you can copy and paste without understanding:
Qt Code:
#include <QtGui> class LimitedLengthDelegate : public QStyledItemDelegate { Q_OBJECT public: QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); Q_UNUSED(index); editor->setMaxLength(7); return editor; } }; int main(int argc, char **argv) { t.setItemDelegate(new LimitedLengthDelegate(&t)); t.show(); return app.exec(); } #include "main.moc"To copy to clipboard, switch view to plain text mode
The entire working part of the solution is ten lines of code and they follow the pattern described in the docs I gave you directly and the things those pages reference.
thanks chris.
Yes it was a hole month but i worked for one day only to get item delegate.i have gone through stardelegate example.I understood create editor function n all but dint get any clue for my problem.
Any way i can get it with ur code....
Thanku...
Bookmarks