QTableWidget::item return own QTableWidgetItem
Hey guys,
i created an own QTableWidgetItem:
Header:
Code:
struct Connections{
int connection;
};
{
public:
MyQTableWidgetItem();
void setConnection(MyQTableWidgetItem &connectedItem, int connection);
private:
QVector <Connections
*> ConnectedItems;
signals:
public slots:
};
Source:
Code:
#include "myqtablewidgetitem.h"
MyQTableWidgetItem::MyQTableWidgetItem()
{
}
void MyQTableWidgetItem::setConnection(MyQTableWidgetItem &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:
Code:
{
Q_OBJECT
public:
AnalyseTable
(QWidget * parent
= 0);
private:
bool lastRowUsed();
signals:
public slots:
void InsertNewRow();
};
#endif // ANALYSETABLE_H
Source:
Code:
#include "analysetable.h"
#include <QDebug>
{
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
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,
_
Re: QTableWidget::item return own QTableWidgetItem
Thanks its working and I changed the argument :)
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.
Re: QTableWidget::item return own QTableWidgetItem
Hey,
ahh wow thanks helped me with another jproblem!"!!!