PDA

View Full Version : QList<QTableWidgetItem *> question



abrou
29th August 2008, 20:04
Hello,

What I want is to find the position of a command in a list in a QTableWidget relative to other commands by that name. I don't know if it is the first item in the QTableWidget with the same name though.

For instance, in the list (the numbers are just for explanation, they won't be in the QTableWidget):

1 transport
2 mix
3 transport
4 mix
5 mix

I would want to know how many "mix"s are before the fourth step, "mix". This mix would be in position 2 in this example, or there is 1 "mix" before it in the whole list. Does that make sense?

What I planned on doing was using QList<QTableWidgetItem *> QTableWidget::findItems ( const QString & text, Qt::MatchFlags flags ) const to make a list of all of the commands by that name, and I have a pointer to the QTableWidgetItem that I am interested in (through a signal).

I then try to put commandList.indexOf(item); where item is the pointer to the QTableWidgetItem, and it returns -1. In the example above, I would expect it the result 1. Is there a way I can make this method work, or should I find another approach (this information is also stored in an .XML document). I believe findItems() is working as expected in the example below.

I am not sure if this makes sense, so here is my code so far:


void compare::connector(QTableWidgetItem * item)
{


QTableWidget *table = item->tableWidget();
int col = table->column(item);
int row = table->row(item);

QString command = item->text();
QList<QTableWidgetItem *> commandList;
if(table->objectName()=="commandList1") commandList = commandList2->findItems(command,Qt::MatchExactly);
else if(table->objectName() =="commandList2") commandList = commandList1->findItems(command,Qt::MatchExactly);

qDebug()<<commandList.indexOf(item);
}

I know Qt is really convinient for stuff like this, and I would really like to learn how to do this properly. I suspect I am getting my pointers messed up, but I am not sure. Thanks for your help again!

edit: Nope, I was wrong, it wasn't pointers. It was just my algorithm, I was acessing the wrong list sorry to bother you