Results 1 to 5 of 5

Thread: QTableWidget::item return own QTableWidgetItem

  1. #1
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default QTableWidget::item return own QTableWidgetItem

    Hey guys,
    i created an own QTableWidgetItem:
    Header:
    Qt Code:
    1. struct Connections{
    2. QTableWidgetItem *connectedItem;
    3. int connection;
    4. };
    5.  
    6. class MyQTableWidgetItem : public QTableWidgetItem
    7. {
    8. public:
    9. MyQTableWidgetItem();
    10. void setConnection(MyQTableWidgetItem &connectedItem, int connection);
    11. private:
    12. QVector <Connections*> ConnectedItems;
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 
    Source:
    Qt Code:
    1. #include "myqtablewidgetitem.h"
    2.  
    3. MyQTableWidgetItem::MyQTableWidgetItem()
    4. {
    5. }
    6.  
    7. void MyQTableWidgetItem::setConnection(MyQTableWidgetItem &connectedItem, int connection)
    8. {
    9. bool itemFound = false;
    10. for(int i = 0;i < this->ConnectedItems.length();i++){
    11. if(this->ConnectedItems[i]->connectedItem->text() == connectedItem.text()){
    12. this->ConnectedItems[i]->connection = connection;
    13. itemFound = true;
    14. i = this->ConnectedItems.length();
    15. }
    16. }
    17. if(!itemFound){
    18. Connections *newConnection = new Connections;
    19. newConnection->connectedItem = &connectedItem;
    20. newConnection->connection = connection;
    21. this->ConnectedItems << newConnection;
    22. }
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    Now I place this items into a table:
    Header:
    Qt Code:
    1. class AnalyseTable : public QTableWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. AnalyseTable(QWidget * parent = 0);
    6. private:
    7. bool lastRowUsed();
    8. signals:
    9. void createNewButton(QTableWidgetItem &item);
    10. public slots:
    11. void InsertNewRow();
    12.  
    13. };
    14. #endif // ANALYSETABLE_H
    To copy to clipboard, switch view to plain text mode 
    Source:
    Qt Code:
    1. #include "analysetable.h"
    2. #include <QDebug>
    3. AnalyseTable::AnalyseTable(QWidget *parent) : QTableWidget(parent)
    4. {
    5. this->setColumnCount(6);
    6.  
    7.  
    8. }
    9.  
    10. bool AnalyseTable::lastRowUsed()
    11. {
    12. int rows = this->rowCount();
    13. if(!rows){
    14. return true;
    15. }
    16. if(this->item(rows - 1,0)->text() == "")
    17. return false;
    18. return true;
    19.  
    20. }
    21.  
    22. void AnalyseTable::InsertNewRow()
    23. {
    24. if(this->lastRowUsed()){
    25. int rows = this->rowCount();
    26. this->insertRow(rows);
    27. int column = this->columnCount();
    28. for(int i = 0 ; i< column; i++){
    29. this->setItem(rows,i,new MyQTableWidgetItem());
    30. }
    31. emit createNewButton(*(this->item(rows,0)));
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    My problem now:
    emit createNewButton(*(this->item(rows,0)));
    this->item() doesnt retrun a MyQTableWidgetItem it return a QTableWidgetItem. How can i fix that? Someone got an idea?
    Greets

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget::item return own QTableWidgetItem

    If you know that you have only MyQTableWidgetItems, you can just use static_cast.
    Otherwise use dynamic_cast and check if the result is != 0.

    Btw, taking the address of a function argument looks very fishy.
    Better pass the "connectedItem" already as a pointer, that is what you have at the caller anyway, right?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ReasyEasyPeasy (17th October 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget::item return own QTableWidgetItem

    Thanks its working and I changed the argument

  5. #4
    Join Date
    Jan 2011
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget::item return own QTableWidgetItem

    You'd better use QTableWidget::setItemPrototype() with your item class. If not, your application will automatically create QTableWidgetItem instead of MyQTableWidgetItem.

  6. #5
    Join Date
    Jul 2015
    Posts
    21
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget::item return own QTableWidgetItem

    Hey,
    ahh wow thanks helped me with another jproblem!"!!!

Similar Threads

  1. Properly using QTableWidget and QTableWidgetItem
    By c0dehunter in forum Newbie
    Replies: 4
    Last Post: 27th October 2012, 15:38
  2. Replies: 1
    Last Post: 10th January 2012, 21:59
  3. QTableWidgetItem for a QTableWidget
    By Archa4 in forum Newbie
    Replies: 1
    Last Post: 28th April 2011, 11:11
  4. QTableWidget or QTableWidgetItem CSS
    By ajayajgdeva in forum Newbie
    Replies: 0
    Last Post: 5th February 2010, 13:47
  5. QTableWidget QTableWidgetItem
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2006, 15:03

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.