PDA

View Full Version : How to display data from tablewidget using fiter mechanism



lekhrajdeshmukh
7th December 2011, 10:48
Dear All,
I have impelemented combo box on my tablewidget for filtering row data.Now when I get unique data on my combobox after that i have selected one of the data but nothing comes to the screen because of no implemenattion has been done on respective slot.I am not getting any idea to how to implement this on my slot.Request you all to please look into the below code and help me out of this problem.

bool TableData::filterSlot()
{
int columnCount = this->tablewidget->columnCount();
int rowCount = this->tablewidget->rowCount();
QStringList filterList;
QString temp_string;
qDebug()<<"Count inside filter slot is";
qDebug()<<rowCount<<":"<<columnCount;
for(int c = 0; c<columnCount;c++)
{
for(int r = 0; r<rowCount;r++)
{
temp_string = this->tablewidget->item(r,c)->text();

if(!filterList.contains(temp_string))
filterList << temp_string;
}
filterList << "None";
combo = new QComboBox(tablewidget);
combo->addItems(filterList);

combo->setCurrentIndex(filterList.count()-1);
this->tablewidget->setCellWidget(0,c,combo);
filterList.clear();
connect(combo,SIGNAL(activated(const QString &)),this,SLOT(testAnother(const QString &)));
}
return true;
}

Please help me on implementation of testAnother()slot by which i will show the matching data from table.
Thanks in advance.

lekhrajdeshmukh
7th December 2011, 14:17
Is anybody have any idea about this???

d_stranz
7th December 2011, 20:17
Please use "
" tags when you post source code. Click the "#" button to add these tags.


[CODE]bool TableData::filterSlot()
{
int columnCount = this->tablewidget->columnCount();
int rowCount = this->tablewidget->rowCount();
QStringList filterList;
QString temp_string;
qDebug()<<"Count inside filter slot is";
qDebug()<<rowCount<<":"<<columnCount;
for(int c = 0; c<columnCount;c++)
{
for(int r = 0; r<rowCount;r++)
{
temp_string = this->tablewidget->item(r,c)->text();

if(!filterList.contains(temp_string))
filterList << temp_string;
}
filterList << "None";
combo = new QComboBox(tablewidget);
combo->addItems(filterList);

combo->setCurrentIndex(filterList.count()-1);
this->tablewidget->setCellWidget(0,c,combo);
filterList.clear();
connect(combo,SIGNAL(activated(const QString &)),this,SLOT(testAnother(const QString &)));
}
return true;
}


Since every combobox is connected to the same slot, the only way you can determine which cell is responsible for the signal is to loop over the cells until you find a combobox that matches the one that sent the signal. Something like this:


void testAnother( const QString & str )
{
QWidget * senderWidget = sender();
int columnCount = this->tablewidget->columnCount();
int rowCount = this->tablewidget->rowCount();
int c = 0;
int r = 0;
bool bFound = false;
for( c = 0; c < columnCount; c++)
{
for( r = 0; r < rowCount; r++)
{
QWidget * cellWidget = tableWidget->cellWidget( r, c );
if ( cellWidget == senderWidget )
{
bFound = true;
break;
}
}
}

if ( bFound )
{
// do something with r, c
}
}

lekhrajdeshmukh
8th December 2011, 06:47
Hi d_stranz,
Thanks for your reply but when i tried to compile the code with testAnother slot it's giving error on below line.
1)QWidget *senderWidget = sender();(invalid conversion from 'QObject*' to 'QWidget*')
2)QWidget *cellWidget = tablewidget->setCellWidget(r,c);
no matching function for call to 'CustomTableWidget::setCellWidget(int&, int&)'
candidates are: void QTableWidget::setCellWidget(int, int, QWidget*)
i think on second line i need to do change this as follwing.
QWidget *cellWidget = tablewidget->setCellWidget(r,c,combo);

But if i include combo on this line it gives below error.
error: void value not ignored as it ought to be

And also i m very thankful to you if you give some idea about implementation inside if(bfound) part.
Thanks in advance.

lekhrajdeshmukh
9th December 2011, 05:41
Can anybody help me on above issue??

ChrisW67
9th December 2011, 07:07
1) QObject::sender() returns a QObject* not a QWidget*. The QObject may be a QWidget... you should qobject_cast<>() to QWidget* and check for NULL if you want to treat it as a QWidget, or change your variable to QObject*.

2) QTableWidget::setCellWidget() takes three arguments and returns none. You start by giving it two arguments, a clear error. When you give it three your code is expecting a return value where none is provided... hence the error message.

lekhrajdeshmukh
12th December 2011, 06:45
Dear Chris,
Thanks for your replay and find below updated code.
void TableData::testAnother(const QString &text)
{
QWidget *senderWidget = qobject_cast<QWidget * >(sender());

int columnCount = this->tablewidget->columnCount();
int rowCount = this->tablewidget->rowCount();
int c =0;
int r =0;
bool bFound = false;
for(c = 0; c<columnCount; c++)
{
for(r = 0; r<rowCount; r++)
{
QWidget *cellWidget = tablewidget->setCellWidget(r,c,combo);
if(cellWidget == senderWidget)
{
bFound = true;
break;
}
}

if(bFound)
{
tablewidget->hideRow(r);
}
}

}
Now during compilation i am getting below error.
error: void value not ignored as it ought to be on this line " QWidget *cellWidget = tablewidget->setCellWidget(r,c,combo)"

d_stranz
13th December 2011, 04:53
QWidget *cellWidget = tablewidget->setCellWidget(r,c,combo);

You misread my code. I did not write "setCellWidget( r, c )", I wrote "cellWidget( r, c )". My code retrieves the cell widget at r, c. Your code is trying to set one. Can you understand why trying to set the widget makes no sense for what you are trying to do?

You should also not expect people here to write all of your code for you. When I post a code suggestion, I usually haven't compiled or tested it. That's why it says, "you need to do *something like this*". I expect you to understand the code I have suggested, not just copy and paste it into your program without even thinking about it.

AND USE "CODE" TAGS WHEN YOU POST SOURCE CODE. Geez. Paste your code into your message, select it, then click the "#" button on the editor.


And also i m very thankful to you if you give some idea about implementation inside if(bfound) part.

Why did you want to know which combobox sent the signal?

I have no idea what you want to do in your application. Once you find the combobox, you then know the row and column, so probably you want to ask the model to give you a model index for that item.

ChrisW67
13th December 2011, 05:34
error: void value not ignored as it ought to be on this line " QWidget *cellWidget = tablewidget->setCellWidget(r,c,combo)"
I already told you the cause of this. However, as d_stranz has indicated, you probably don't want setCellWidget() anyway.