How copy data from one model view to another
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
Code:
void dcsinter::on_pushButton_clicked()
{
const QModelIndex index
= ui
->dirtableView
->selectionModel
()->currentIndex
();
QString selectedtext
= index.
data(Qt
::DisplayRole).
toString();
list << selectedtext ;
model2->setStringList(list);
ui->listtableView_2->setModel(model2);
ui->listtableView_2->setColumnWidth(0,600);
}
thank you
Re: How copy data from one model view to another
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
Re: How copy data from one model view to another
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.
Re: How copy data from one model view to another
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
Re: How copy data from one model view to another
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
Code:
QStringListModel* model
= qobject_cast<QStringListModel
*>
(ui
->listtableView_2
->model
());
old << newData;
model->setStringList(old);
Re: How copy data from one model view to another
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
Code:
void dcsinter::on_pushButton_clicked()
{
const QModelIndex index
= ui
->dirtableView
->selectionModel
()->currentIndex
();
QString selectedtext
= index.
data(Qt
::DisplayRole).
toString() QFile listfie
("list.txt");
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
Re: How copy data from one model view to another
I doubt if that is the right solution but you must know... Use also QIODevice::Append at QFile::open().
Re: How copy data from one model view to another
you're right it didn't work that i thought, i need a help here do u have any idea that this code
Code:
QStringListModel* model
= qobject_cast<QStringListModel
*>
(ui
->listtableView_2
->model
());
crash the program
i think i struck in this table issue
Code:
void dcsinter::on_pushButton_clicked()
{
const QModelIndex index
= ui
->dirtableView
->selectionModel
()->currentIndex
();
QString selectedtext
= index.
data(Qt
::DisplayRole).
toString();
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
Re: How copy data from one model view to another
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.
Re: How copy data from one model view to another
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.
Re: How copy data from one model view to another
Re: How copy data from one model view to another
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
Code:
void dcsinter::listing()
{
ui->listtableView_2->setModel(model);
}
then on click push button
Code:
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
());
list << selectedtext ;
model->setStringList(list);
}
:o
Re: How copy data from one model view to another
Hi, one more improvement to avoid the cast: Make the model a private member of your class an access it directly.
Re: How copy data from one model view to another
can we remove an added item from the table
Re: How copy data from one model view to another
Yes, Use QStringListModel::removeRows(). If you have a string and want to know which index (row number) is has, use QAbstractItemModel::match().