PDA

View Full Version : should i use listview ?



rimie23
1st May 2012, 20:07
Hi,
i want to see the name of all my object (with their position) [when i add object in my scene ogre i add it in the listobject)
i use listview ?
if that's the case , is there exemple how i do that ? or it's better to use QTreeWidget because i want when i clic of an object in the list i can do another thing

ChrisW67
2nd May 2012, 05:45
If you want a simple list then use a QListWidget or QListView.
If you want a multi-column table then use a QTableWidget or QTableView.
If you want a tree structure then use a QTreeWidget or QTreeView.

Each xWidget includes an internal data model and the xView classes need an external model. Each xWidget is-a xView and all can respond to users clicking, double clicking etc. All follow the Model/View Programming approach.

The documentation describes how to use theses classes and even link to examples like the Config Dialog Example

rimie23
2nd May 2012, 13:11
if i use qlistview

is there an exemple how i use it for another class

because i define this list view in class


listView = new QListView(dockWidgetContents_8);
listView->setObjectName(QString::fromUtf8("listView"));
listView->setGeometry(QRect(0, 0, 281, 290));

but how i connect it with my function
add_object()

Le_B
2nd May 2012, 13:17
you don't add object to the view you add them to the model that is connected to your view.
you should read this, it seems complicated but in practice it is not.

http://qt-project.org/doc/qt-4.8/model-view-programming.html

rimie23
2nd May 2012, 13:35
you don't add object to the view you add them to the model that is connected to your view.
Sorry i did not understand
i know that when i add in my scene (ogre) an object with add object (that work) i must see the name and position of this object in the listview
(i did not found exemple like that)
http://qt-project.org/doc/qt-4.8/mod...ogramming.html
it is very complicated

Le_B
2nd May 2012, 13:46
a view is the graphical representation of data.
the data are stored in a model.
so you need a model (a simple model would QStringListModel)
you link the model to the view : listView->setModel(model);
you put the info in the model (not the view) and the view will automatically update the display.

or if you don't want to bother and don't need to display a lot of info you can use http://doc-snapshot.qt-project.org/4.8/qlistwidget.html
it contains both model and view and is simplier, but it s slower

rimie23
2nd May 2012, 15:44
i try to do that
in my class QMainWindow


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
.......................
listView = new QListView(dockWidgetContents_8);
listView->setObjectName(QString::fromUtf8("listView"));
listView->setGeometry(QRect(0, 0, 281, 290));

QStandardItemModel *model=new QStandardItemModel();
listView->setModel(model);


in my class ogre widget (where i create my objects


................................................. add an object
QStandardItem *item=new QStandardItem(QString ("Cube %0").arg(count));

model->setItem(count,item);
QListView *listview;
listview->update();

but it dos not work

Le_B
2nd May 2012, 15:56
mmmm... you should use appendRow(item) instead of setItem
the listview->update(); is not necessary the model should send the new info to the view into the appendRow() function
the model pointer into the add object funtion where did you get it ?

always use "this" as the parent pointer when you use new or you will leak

rimie23
2nd May 2012, 19:39
mmmm... you should use appendRow(item) instead of setItem
the listview->update(); is not necessary the model should send the new info to the view into the appendRow() function
the model pointer into the add object funtion where did you get it ?

always use "this" as the parent pointer when you use new or you will leak

yes i use appendRow but nothing it happend

the model pointer into the add object funtion where did you get it ?
what you mean ? i just make that

QStandardItemModel *model=new QStandardItemModel();
listView->setModel(model);



always use "this" as the parent pointer when you use new or you will leak
where i use it ?
(sorry i know that my questions is a lot but i try to understand to do somthing good)

Le_B
3rd May 2012, 12:55
1) the listview is created into the mainwindow class and you call listView->setModel(model); into the ogre widget class
so i want to know where you got that pointer "listview" 'just to check that it comes from the mainwindow class and is the same)

2) all QObject can have a parent QObject pointer pass as a constructor parameter. when the parent is destroyed it will automaticaly destroy his children.
so when you do new QStandardItemModel(); you should do new QStandardItemModel(this); for exemple
there is very few times when you don't want to do that

rimie23
3rd May 2012, 13:24
so i want to know where you got that pointer "listview" 'just to check that it comes from the mainwindow class and is the same
i did not got any pointer:( (i just write the same code in my repling, i read that in qt assistant exemple ) shall i define pointer ? how i do that

so when you do new QStandardItemModel(); you should do new QStandardItemModel(this); for exemple
ahhhh, ok i will do that

Le_B
3rd May 2012, 13:27
can you put the entire function please ?

rimie23
3rd May 2012, 13:46
ok
that's the code of add_object


void OgreWidget::add_object(const Ogre::Vector3 &pos)
{

Ogre::String name1 = "entity " + Ogre::StringConverter::toString(Ogre::Root::getSin gletonPtr()->getTimer()->getMicroseconds());
mEntity = ogreSceneManager->createEntity(name1, Ogre::SceneManager:: PT_CUBE);

Ogre::MaterialPtr material1111 = Ogre::MaterialManager::getSingleton().create("green", "General");


material1111->getTechnique( 0 )->getPass( 0 )->setAmbient(1, 1, 0);
mEntity->setMaterial(material1111);
Ogre::String name = "Node " + Ogre::StringConverter::toString(Ogre::Root::getSin gletonPtr()->getTimer()->getMicroseconds()); // Provides a unique timestamp - the time since the application was started in microseconds.
mNode = ogreSceneManager->getRootSceneNode()->
createChildSceneNode(name, pos);
mNode->attachObject(mEntity);
count++;

QStandardItem *item=new QStandardItem(QString ("Cube %0").arg(count));

//model->setItem(count,item);
model->appendRow(item);
QListView *listview;
listview->update();

ogreRenderWindow->update();
this->update();

}


i just do that

Le_B
3rd May 2012, 13:50
so "model" must be a member variable of OgreWidget
and it must be set somewhere right ?

rimie23
3rd May 2012, 14:06
beh
i define model in mainwinows


lass MainWindow : public QMainWindow
{
Q_OBJECT

public:

listView = new QListView(dockWidgetContents_8);
listView->setObjectName(QString::fromUtf8("listView"));
listView->setGeometry(QRect(0, 0, 281, 290));

QStandardItemModel *model=new QStandardItemModel();
listView->setModel(model);

i think i bad understand how i use the "model" no? (i modifate qt assistant exemple )

Le_B
3rd May 2012, 14:11
so you must have a crash when doing model->appendRow(item); in OgreWidget right ?
if it s true it s just that you need that ogreWidget ask for the model pointer to Mainwindow, else how do you want OgreWidget to know what model is ?

rimie23
3rd May 2012, 20:48
you need that ogreWidget ask for the model pointer to Mainwindow, else how do you want OgreWidget to know what model is ?
how i do that (when i see the exemple in qt assistant it make just like that maybe because he is in the same class?)
so how i said to ogre widget to applicate the model in mainwindows , but at first is my definition of the model in mainwinows true ?

Le_B
3rd May 2012, 22:11
just create a set_model function(const QStandardItemModel *model) function in ogrewiget that will save the pointer value to a member variable
then call it after you create the model in mainwindow (i suppose you create the ogrewidget in the same function as the model)

rimie23
3rd May 2012, 22:18
i suppose you create the ogrewidget in the same function as the model)
create model like that ?


model->appendRow(item);

rimie23
9th May 2012, 13:51
Ok , i define my model like that
in ogrewidget.cpp


QStandardItemModel* OgreWidget::add_object(/*Ogre::Real*/double offsetX ,/*Ogre::Real*/double offsetY)
{
.................................................
model= new QStandardItemModel();
int i=1;
for(itr = result.begin() ;itr != result.end();++itr)
{
if(itr->movable)
{

MarkerNode->_setDerivedPosition(mouseRay.getPoint(itr->distance));
MarkerNode->setScale(0.1f, 0.1f, 0.1f);
QStandardItem *item=new QStandardItem(QString ("Marker %0").arg(i));
model->appendRow(item);
i++;

}
}
return model;
}

and in my_interface.cpp


list_O = new QToolButton(page_13);
list_O->setGeometry(QRect(30, 210, 101, 31));
list_O->setText(QApplication::translate("MainWindow", "Object Liste", 0, QApplication::UnicodeUTF8));
listView =new QListView(list_O);

but it still do not make to me the list

rimie23
9th May 2012, 22:40
i want just to know what i forget to do