Results 1 to 9 of 9

Thread: How to display data from tablewidget using fiter mechanism

  1. #1
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default How to display data from tablewidget using fiter mechanism

    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.

  2. #2
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display data from tablewidget using fiter mechanism

    Is anybody have any idea about this???

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to display data from tablewidget using fiter mechanism

    Please use "[CODE ]" tags when you post source code. Click the "#" button to add these tags.

    Qt Code:
    1. bool TableData::filterSlot()
    2. {
    3. int columnCount = this->tablewidget->columnCount();
    4. int rowCount = this->tablewidget->rowCount();
    5. QStringList filterList;
    6. QString temp_string;
    7. qDebug()<<"Count inside filter slot is";
    8. qDebug()<<rowCount<<":"<<columnCount;
    9. for(int c = 0; c<columnCount;c++)
    10. {
    11. for(int r = 0; r<rowCount;r++)
    12. {
    13. temp_string = this->tablewidget->item(r,c)->text();
    14.  
    15. if(!filterList.contains(temp_string))
    16. filterList << temp_string;
    17. }
    18. filterList << "None";
    19. combo = new QComboBox(tablewidget);
    20. combo->addItems(filterList);
    21.  
    22. combo->setCurrentIndex(filterList.count()-1);
    23. this->tablewidget->setCellWidget(0,c,combo);
    24. filterList.clear();
    25. connect(combo,SIGNAL(activated(const QString &)),this,SLOT(testAnother(const QString &)));
    26. }
    27. return true;
    28. }
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. void testAnother( const QString & str )
    2. {
    3. QWidget * senderWidget = sender();
    4. int columnCount = this->tablewidget->columnCount();
    5. int rowCount = this->tablewidget->rowCount();
    6. int c = 0;
    7. int r = 0;
    8. bool bFound = false;
    9. for( c = 0; c < columnCount; c++)
    10. {
    11. for( r = 0; r < rowCount; r++)
    12. {
    13. QWidget * cellWidget = tableWidget->cellWidget( r, c );
    14. if ( cellWidget == senderWidget )
    15. {
    16. bFound = true;
    17. break;
    18. }
    19. }
    20. }
    21.  
    22. if ( bFound )
    23. {
    24. // do something with r, c
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display data from tablewidget using fiter mechanism

    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.

  5. #5
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display data from tablewidget using fiter mechanism

    Can anybody help me on above issue??

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to display data from tablewidget using fiter mechanism

    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.
    Last edited by ChrisW67; 9th December 2011 at 06:15.

  7. #7
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display data from tablewidget using fiter mechanism

    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)"

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to display data from tablewidget using fiter mechanism

    Qt Code:
    1. QWidget *cellWidget = tablewidget->setCellWidget(r,c,combo);
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by d_stranz; 13th December 2011 at 04:07.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to display data from tablewidget using fiter mechanism

    Quote Originally Posted by lekhrajdeshmukh View Post
    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Display DIB data
    By nightroad in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2010, 21:42
  2. Display value in tableWidget row/column
    By sosanjay in forum Qt Programming
    Replies: 4
    Last Post: 3rd October 2009, 08:04
  3. QPixmap Display RGB data
    By phoenixtju in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2009, 10:31
  4. IO Control Mechanism (Hardware Data Access)
    By rud_1023 in forum General Programming
    Replies: 2
    Last Post: 23rd October 2006, 12:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.