PDA

View Full Version : How copy data from one model view to another



breakthecode
3rd December 2011, 18:00
i am trying to select a data member from a model/view class, here a table view and add that particular data to an another table view. i can copy the data to another table view but only one selected data can add when i click the add button by using my code. can i add more data to table view by modifying the code or is there any other solution



void dcsinter::on_pushButton_clicked()
{

const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
QString selectedtext = index.data(Qt::DisplayRole).toString();

QStringListModel *model2 = new QStringListModel();
QStringList list;
list << selectedtext ;
model2->setStringList(list);
ui->listtableView_2->setModel(model2);
ui->listtableView_2->setColumnWidth(0,600);



}


thank you

breakthecode
4th December 2011, 05:12
sorry i missed something to say in the above thread, select a data from 1st table and add it to 2nd table it will appear in the 2nd table but when add another data from the first table then in the 2nd table replace the first data with 2nd

Lykurg
4th December 2011, 07:44
Beside that you have a memory leak in your function, you replace the model and do not append the new list. So reuse the old model and append the data by getting the old string list, append the new one and set the new composed list back to the model.

breakthecode
4th December 2011, 08:11
i want two table views , one is for tabling files in a dir, second one is for real time adding of selected files. i think i need some modification in here

QStringListModel *model2 = new QStringListModel();
QStringList list;
list << selectedtext ;
model2->setStringList(list);

thank you

Lykurg
4th December 2011, 09:34
Well I don't know the architecture of your code, but instead of creating the model each time the function is called do something like that

QStringList newData; // or where ever your data is
QStringListModel* model = qobject_cast<QStringListModel*>(ui->listtableView_2->model());
QStringList old = model->stringList();
old << newData;
model->setStringList(old);

breakthecode
5th December 2011, 12:10
finally i got an another idea, load the string to a file then read the list model from there but when i writing the strings to the file again hits the same problem, only first the first string is saved others are overwrite on the first one. i want to write the strings one after the another, is there any way to solve that


void dcsinter::on_pushButton_clicked()
{

const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
QString selectedtext = index.data(Qt::DisplayRole).toString()
QFile listfie("list.txt");
listfie.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&listfie);
out << selectedtext << '\n' ;
}

imagine if the vale selectedtext string on first click is hdbhdn.avi second click hgsdjsdff.avi
then file shows like the below

hgsdjsdff.avi

i want to like this

wdbhdn.avi
hgsdjsdff.avi

i want a continues writing on the file

thank you

Lykurg
5th December 2011, 12:43
I doubt if that is the right solution but you must know... Use also QIODevice::Append at QFile::open().

breakthecode
5th December 2011, 14:51
you're right it didn't work that i thought, i need a help here do u have any idea that this code
QStringListModel* model = qobject_cast<QStringListModel*>(ui->listtableView_2->model());crash the program

i think i struck in this table issue


void dcsinter::on_pushButton_clicked()
{

const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
QString selectedtext = index.data(Qt::DisplayRole).toString();

QStringListModel *model = new QStringListModel();
QStringList list;
list << selectedtext ;
model->setStringList(list);
ui->listtableView_2->setModel(model);
ui->listtableView_2->setColumnWidth(0,600);
}


i have a question, is QTableView build for adding column with data

Lykurg
5th December 2011, 16:59
Have you checked if ui->listtableView_2->model() is returning a null pointer (= the view hasn't any model yet)? You first have to set a model to the view (best in the constructor) and then you can reuse the model in your on_pushButton_clicked function.

breakthecode
5th December 2011, 18:20
thanks 1000
this is what i am looking for 2 days this single piece idea
you said it in the first day but i can't understand it, i now know it's a basic in c++ pro. but i can't understand it, sorry for waisting your time
again thanks , now it's working very well.

Lykurg
5th December 2011, 21:12
You are welcome.

breakthecode
6th December 2011, 04:33
here i publish the full result of the forum discussion, it's useful for anyone encountering the same problem
aim is to copy a data from a qtableview and add that data to another qtableview
first create a constructor

void dcsinter::listing()
{
QStringListModel *model = new QStringListModel;
ui->listtableView_2->setModel(model);


}
then on click push button

void dcsinter::on_pushButton_clicked()
{

const QModelIndex index = ui->dirtableView->selectionModel()->currentIndex();
QString selectedtext = index.data(Qt::DisplayRole).toString();

QStringListModel* model = qobject_cast<QStringListModel*>(ui->listtableView_2->model());
QStringList list = model->stringList();
list << selectedtext ;
model->setStringList(list);

}
:o

Lykurg
6th December 2011, 06:48
Hi, one more improvement to avoid the cast: Make the model a private member of your class an access it directly.

breakthecode
8th December 2011, 06:24
can we remove an added item from the table

Lykurg
8th December 2011, 06:39
Yes, Use QStringListModel::removeRows(). If you have a string and want to know which index (row number) is has, use QAbstractItemModel::match().