PDA

View Full Version : Programattically Clicking an Item in QTreeView



johnny_sparx
8th May 2006, 19:49
I have the following class defined (with alot of the extra stuff removed for clarity), and I have a question about its behavior:


class DatabaseTreeWidget : public QTreeView
{
Q_OBJECT;
public:
DatabaseTreeWidget(QString Filename, QWidget *parent = 0);
~DatabaseTreeWidget();
void Update_Tree_From_Flags();

signals:
void Message_Changed(QString *Message);
void DB_File_Path_Changed(QString *File_Path, const QModelIndex *index);
void DB_Key_Changed(QString *Key);
void DB_Flags_Changed(int Flags);

private slots:
void Database_Item_Clicked(const QModelIndex &index);
bool Database_Is_Empty(QString File_Name);
void Click_Current_Database_Selection();
};

I can use this class with no problems except for one. I need to get the currently selected item and click it from the program (triggering another event). I tried some testing code as follows:


void DatabaseTreeWidget::Click_Current_Database_Selecti on()
{
this->selectAll();
std::cout<<"Selected list size: "<<this->selectedIndexes().size()<<"\n";
//ScanDatabaseItem *Item = (ScanDatabaseItem *) selectedIndexes().at(0).internalPointer();
}

When I test the code, I would expect that I would get a count of all items in the tree. I get zero. I initally used currentIndex() but could not obtain the currently selected item in the widget. :confused:

How can I achieve this? Thanks.

JS

wysota
8th May 2006, 21:33
emit clicked(currentIndex());

johnny_sparx
8th May 2006, 22:47
This is a problem: segmentation fault.... makes me think there is no item selected although the widget shows otherwise.

I tried something like this, but it appears like there is nothing to "click". The currently selected item in the widget shows that something is selected, but a count of selected items returns zero items selected.

The QTreeView clearly shows an item selected, but I cannot even get that "currentItem" and read values from the underlying model.

What am I doing wrong here?:eek:

wysota
9th May 2006, 00:32
This is a problem: segmentation fault....
You probably have something wrong with a slot which is connected to the signal. Emit can't cause a segfault here.


makes me think there is no item selected although the widget shows otherwise.
There is a difference between "selected" and "current".


I tried something like this, but it appears like there is nothing to "click". The currently selected item in the widget shows that something is selected, but a count of selected items returns zero items selected.
Try using the selection model (QAbstractItemView::selectionModel()) and use QItemSelectionModel::selectedIndexes() to get a list of selected indexes. You'll probably get the same result as using selectedIndexes() from QAbstractItemView, but it'll just make you certain there are no items selected :)


The QTreeView clearly shows an item selected, but I cannot even get that "currentItem" and read values from the underlying model.
Does it? A selected item is marked with a different coloured background (at least in most styles) and current item is marked with a dotted border (at least in most styles). Are you sure you have a selection? Try selecting more than one item to be sure of that.


What am I doing wrong here?:eek:
Where exactly? :)

johnny_sparx
11th May 2006, 17:26
I am quite sure that I have an item selected... background is dark, there is no dotted line around the item that I can see.

I am not quite sure how to use the selection model. All I want is to have the model index of the item that is highlighted by a background change in the actual widget.