PDA

View Full Version : qt4 QStringList with model/view



incapacitant
15th March 2006, 08:15
QStringList Destinataires::getDestList()
{
QStringList rows;
int rowCount = model->rowCount();
int i;
QString s;

QItemSelectionModel *sel = qDestin->selectionModel();
for( i = 0; i < rowCount; i++ )
if ( sel->isRowSelected( i, qDestin->rootIndex() ) )
s = model->data( model->index( i, 0 ) ); <- compile error
rows.push_back( s );
return rows;
}



96 C:\Qt\test\sms\destin.cpp no match for 'operator=' in 's = *(((Destinataires*)this)->Destinataires::model->QAbstractItemModel::_vptr$QObject + 68u)(((const QModelIndex&)((const QModelIndex*)(&*(((Destinataires*)this)->Destinataires::model->QAbstractItemModel::_vptr$QObject + 48u)(i, 0, ((const QModelIndex&)((const QModelIndex*)(&QModelIndex()))))))), 0)'

What should I do to retrieve the QString s from the model ?

jpn
15th March 2006, 09:06
QStringList Destinataires::getDestList()
{
QStringList rows;
QModelIndexList selected = selectionModel()->selectedIndexes();
foreach (QModelIndex idx, selected)
{
// you only want strings in 1st column? the list of selection
// indexes contain indexes for possible rest of colums also..
if (idx.row() == 0)
{
rows << idx.data().toString();
}
}
return rows;
}

incapacitant
15th March 2006, 09:13
with


QModelIndexList selected = selectionModel()->selectedIndexes();


I get compile error :
89 C:\Qt\test\sms\destin.cpp `((Destinataires*)this)->Destinataires::selectionModel' cannot be used as a function

Sorry I am completly a beginner in QT4 and model/views.

jpn
15th March 2006, 09:19
Right, I missed the qDestin ptr.. ;) How about this:

QModelIndexList selected = qDestin->selectionModel()->selectedIndexes();

incapacitant
15th March 2006, 09:34
Yes what you say works perfectly. QT4 is hard to understand for me. :o

incapacitant
15th March 2006, 10:05
As I want all rows your code has become :


QStringList Destinataires::getDestList()
{
QStringList rows;
QModelIndexList selected = qDestin->selectionModel()->selectedIndexes();
foreach (QModelIndex idx, selected)
rows << idx.data().toString();

return rows;
}



But there you declare "QModelIndexList". What does it mean ?
Related Non-Memberstypedef QModelIndexList
Synonym for QList (http://www.qtcentre.org/forum/qlist.html)<QModelIndex (http://www.qtcentre.org/forum/qmodelindex.html)>.
Completly lost. :(

jpn
15th March 2006, 10:13
As docs say, it's just a synonym for a QList containing QModelIndexes. :)

All view's have a selection model which keeps track of selected items.
There might be multiple selected "items", so the selection cannot just be a single "item".
So the view's selection model needs to return a list of selected indexes.


QModelIndexList QItemSelectionModel::selectedIndexes (http://doc.trolltech.com/4.1/qitemselectionmodel.html#selectedIndexes) () const

Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.

incapacitant
15th March 2006, 10:30
There is a pre-defined type for all QList ?
Here we have QList<QStrings>.
Would it be still the same QModelIndexList ?
with QList<Int>, or another type ?

jpn
15th March 2006, 10:46
There is a pre-defined type for all QList ?
Here we have QList<QStrings>.
Would it be still the same QModelIndexList ?
with QList<Int>, or another type ?

Actually, QStringList and QList<QString> are different. QStringList inherits QList<QString> and adds some functionality..

Because QList is a template class, you can have a list of basically anything, as long as it is an assignable data type (http://doc.trolltech.com/4.1/containers.html#assignable-data-types).

There are a few type definitions: QVariantList, QObjectList, QModelIndexList..
You can take them as a more convenient way of writing the type or something.. :)

There is an essential reason for these type definitions, and I think it has something to do with Qt's meta-object system, only registered meta types can be used in signal and slot connections..
But that's a whole different world to discuss ;)

incapacitant
15th March 2006, 11:00
Well now the next question is how do I retrieve the ith item number of a QStringList ?

(i am lost with this)

jpn
15th March 2006, 11:20
Take a look at the documentation and see what methods QList (http://doc.trolltech.com/4.1/qlist.html) offers..

You can use for example method at(int i) or operator [].

QString row = rows.at(i);

QString row = rows[i];

incapacitant
15th March 2006, 14:32
In this code I try to copy selected items from dialog menu into dialog destination.



for ( int i = 3; i < model->rowCount(); i++ )
{
s = model->rows.at(i);
row = Destinataires_dlg.model->rowCount();
Destinataires_dlg.model->insertRow( row );
Destinataires_dlg.model->setData(model->index(row, 0), s );
}



but i cannot find the right member to extract each row from model (in menu).

Can you help me ?

jpn
15th March 2006, 15:11
Sorry, but I don't quite get it what you are trying to do, but why don't you just use the same model for the destination view?
Or is there any specific reason why couldn't you use the same model..?

incapacitant
15th March 2006, 16:39
First I am sorry because I am very poor at QT.

I have a menu dialog with users listed that calls a destin dialog, both within different classes.
Destin dialog allows the user to select items, as this thread explained to me and go back to my menu dialog (while closing the destin window) and brings back selected items that I list on the menu dialog.

Then before calling again Destin I want to identify the entries of the menu that are on the destin model to highlight them as already selected.
So I go thru the menu dialog for entries to highlight.


Destinataires Destinataires_dlg( this );
if ( model->rowCount() > 3 )
{
for ( int i = 3; i < model->rowCount(); i++ )
{
// find entry to highlight from the menu model
s = model->rows.at(i);
// then I should find the same entry in destin model to mark it as selected
// but I don't know how to do that in QT4, worked very well in QT3
// here I have to do a loop to search s in destinataires_dlg.model
// and highlight it
row = Destinataires_dlg.model->rowCount();
Destinataires_dlg.model->insertRow( row );
Destinataires_dlg.model->setData(model->index(row, 0), s );
}
}


nb in qt3 the code was :


if ( qRecap->count() > 3 )
{
for ( int i = 3; i < qRecap->count(); i++ )
{
QListBoxItem *itemQRecap = qRecap->item( i );
for ( int j = 0; j < Destinataires_dlg.qDestinCount(); j++ )
{
QListBoxItem *itemQDestin = Destinataires_dlg.qDestinItem( j );
// si item de destinataires est dans le recapitulatif, on le selectionne
if ( itemQRecap->text() == itemQDestin->text() )
Destinataires_dlg.qDestinSelect( itemQDestin );
}
}
}

jpn
15th March 2006, 20:13
Looking for something like this?

incapacitant
16th March 2006, 07:37
This is precisely what I was trying to do. Thank you very much.
Trouble is I don't understand how you make it work :

I don't understand how the selector form contains still the selection when you go back to the selector form. My constructor reinitializes the content and does not remember what was selected before.

That is why I was trying to iterate through the rows of the main form : to reselect manually the selector entries. Why your constructor does not re-init the selection, but displays what was selected before is a mistery for me.

jpn
16th March 2006, 08:06
For instance I don't understand how the selector form contains the selection when you go back to the selector form. My constructor reinitializes the content and does not remember what was selected before.
Because in the example the selector form is created only once. The select-button executes the same form again and again. So the selector form can remember it's state between "launches".

incapacitant
16th March 2006, 08:49
How do you ensure it is launched only once ? Because the selector call is in the mainform constructor ? I am lost. :(

jpn
16th March 2006, 09:24
Yes, the selector dialog is created only once, in the mainwindow constructor.
In the same constructor, also a connection (guess which :)) is made which causes the select button click to launch the selector dialog as modal (http://doc.trolltech.com/4.1/qdialog.html#modal-dialogs).

incapacitant
16th March 2006, 10:23
Ok, then I should be able to adapt your solution to my code.
Thanks a lot.

incapacitant
16th March 2006, 19:13
If I do as you propose (ie declare the selector in the mainform), then the code that's supposed to retrieve the rows selected in the selector class fails.


// Destinataires Destinataires_dlg( this ); <- in main form in your new syntax
model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());

// add number of char available
int NbCharRestants = NbCharSMSMax - qMessage->toPlainText().length();
s.setNum(NbCharRestants,10);
s = TxtCharSMSMax + " " + s;
appendRow( s );
// add sms credit
qMajCredit();
// get the selected destinations
QStringList ret = Destinataires_dlg.getDestList(); <- compile error
if ( ret.size() )
{
appendRow ( TxtDestinataires );
for( int i = 0; i < ret.size(); i++) appendRow ( ret[i] );
}
else
{
s = TxtDestinataires + " * " + TxtIndefini;
appendRow( s );
}
}


147 C:\Qt\test\sms\menu.cpp `getDestList' has not been declared

jpn
16th March 2006, 19:41
// Destinataires Destinataires_dlg( this ); <- in main form in your new syntax
...
QStringList ret = Destinataires_dlg.getDestList(); <- compile error


The compiler doesn't know any "Destinataires_dlg" variable, because it's commented out.
Instead of this, you have to refer to the dialog which you have created somewhere else and stored as a member variable.

incapacitant
16th March 2006, 20:03
I have declared the selector window in the main form code and added as class variable:


Destinataires_dlg = new Destinataires(this);



public:
ApplicationWindow();
~ApplicationWindow();
Destinataires *Destinataires_dlg;


But I still get the same compile error !
So I cannot get the selection from the selector screen in the main code.

jpn
16th March 2006, 20:39
So it's a pointer, you need to use "->"-operator.. a basic c++ principle.. ;)


QStringList ret = Destinataires_dlg->getDestList();