PDA

View Full Version : saving and retrieving the contents of qtabelwidget



vani.pv
15th September 2012, 12:18
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.......

Ashkan_s
15th September 2012, 16:42
To store contents in an array you can convert each row to a QString and save it in an array of QStrings.


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QTableWidget w;
QTableWidgetItem *item;
QString str;
QString *strArray;
int rowCount, colCount;

w.insertRow(0);
w.insertRow(0);
w.insertColumn(0);
w.insertColumn(0);


item = new QTableWidgetItem("Qt");
w.setItem(0, 0, item);

item = new QTableWidgetItem("4.7");
w.setItem(0, 1, item);

item = new QTableWidgetItem("Qt");
w.setItem(1, 0, item);

item = new QTableWidgetItem("4.8");
w.setItem(1, 1, item);

rowCount = w.rowCount();
colCount = w.columnCount();

strArray = new QString[rowCount];

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);

item = new QTableWidgetItem(strArray[0] + strArray[1]); //To view the result
w.setItem(0, 0, item);

w.setColumnWidth(0, 300);

w.resize(600, 400);
w.show();

return app.exec();
}

ChrisW67
16th September 2012, 03:22
and can i restrict the cell text maximum length like an lineedit.?
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.

vani.pv
17th September 2012, 09:53
thanks ashkan and chris.

vani.pv
17th September 2012, 13:37
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.

ChrisW67
18th September 2012, 01:23
Here is another problem.
when i am trying to set the max length of a text i dint find any function to do it.

No, it is the same question you started with.


can i set the text length for a particular column.
please provide me an example or a piece of code.

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.




onbuttonclicked()
{
ui->mytablewiget->insertrow(ui->mytablewiget->rowcount());
}


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
tags.

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.

vani.pv
18th September 2012, 08:24
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?

vani.pv
17th October 2012, 09:55
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 .

BalaQT
17th October 2012, 16:26
u need to set the property on the cell widget.

hope it helps
Bala

ChrisW67
17th October 2012, 23:16
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 .

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:



#include <QtGui>

class LimitedLengthDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
LimitedLengthDelegate(QObject *parent = 0): QStyledItemDelegate(parent) { }

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);

QLineEdit *editor = new QLineEdit(parent);
editor->setMaxLength(7);
return editor;
}
};


int main(int argc, char **argv)
{
QApplication app(argc, argv);

QTableWidget t(3, 3);
t.setItemDelegate(new LimitedLengthDelegate(&t));
t.show();

return app.exec();
}
#include "main.moc"


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.

vani.pv
18th October 2012, 10:20
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...