PDA

View Full Version : QTableWidget::item return own QTableWidgetItem



ReasyEasyPeasy
17th October 2015, 16:05
Hey guys,
i created an own QTableWidgetItem:
Header:

struct Connections{
QTableWidgetItem *connectedItem;
int connection;
};

class MyQTableWidgetItem : public QTableWidgetItem
{
public:
MyQTableWidgetItem();
void setConnection(MyQTableWidgetItem &connectedItem, int connection);
private:
QVector <Connections*> ConnectedItems;

signals:

public slots:

};
Source:

#include "myqtablewidgetitem.h"

MyQTableWidgetItem::MyQTableWidgetItem()
{
}

void MyQTableWidgetItem::setConnection(MyQTableWidgetIt em &connectedItem, int connection)
{
bool itemFound = false;
for(int i = 0;i < this->ConnectedItems.length();i++){
if(this->ConnectedItems[i]->connectedItem->text() == connectedItem.text()){
this->ConnectedItems[i]->connection = connection;
itemFound = true;
i = this->ConnectedItems.length();
}
}
if(!itemFound){
Connections *newConnection = new Connections;
newConnection->connectedItem = &connectedItem;
newConnection->connection = connection;
this->ConnectedItems << newConnection;
}

}

Now I place this items into a table:
Header:

class AnalyseTable : public QTableWidget
{
Q_OBJECT
public:
AnalyseTable(QWidget * parent = 0);
private:
bool lastRowUsed();
signals:
void createNewButton(QTableWidgetItem &item);
public slots:
void InsertNewRow();

};
#endif // ANALYSETABLE_H

Source:

#include "analysetable.h"
#include <QDebug>
AnalyseTable::AnalyseTable(QWidget *parent) : QTableWidget(parent)
{
this->setColumnCount(6);


}

bool AnalyseTable::lastRowUsed()
{
int rows = this->rowCount();
if(!rows){
return true;
}
if(this->item(rows - 1,0)->text() == "")
return false;
return true;

}

void AnalyseTable::InsertNewRow()
{
if(this->lastRowUsed()){
int rows = this->rowCount();
this->insertRow(rows);
int column = this->columnCount();
for(int i = 0 ; i< column; i++){
this->setItem(rows,i,new MyQTableWidgetItem());
}
emit createNewButton(*(this->item(rows,0)));
}
}

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

anda_skoa
17th October 2015, 17:27
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,
_

ReasyEasyPeasy
17th October 2015, 18:54
Thanks its working and I changed the argument :)

weiweiqiao
18th October 2015, 05:56
You'd better use QTableWidget::setItemPrototype() with your item class. If not, your application will automatically create QTableWidgetItem instead of MyQTableWidgetItem.

ReasyEasyPeasy
19th October 2015, 19:00
Hey,
ahh wow thanks helped me with another jproblem!"!!!