Getting data from QTableWidget
-
Hello again,
I've got a problem getting a pointer to own specified widgets (inherited from QWidget) out of a table cell (QTableWidget):
main.h:
@class table_cell : public QWidget
{
Q_OBJECTpublic:
int row, column;
QString type, cls;
QHBoxLayout* layout;
QPushButton* pushbutton;
QCheckBox* checkbox;
QTextEdit* textedit;table_cell(QWidget* parent, QString p_type, QString p_cls, int p_row, int p_column);
};@
main.cpp:
{
@table = new QTableWidget(rows, columns);
table_headers_h << "Amount" << "Person 1" << "Person 2" << "Person 3" << "Person 4" << "Person 5";
table->setHorizontalHeaderLabels(table_headers_h);
table->setColumnWidth(0, 80);
for(int i = 0;i < rows; i++)
{
table->setRowHeight(i, 26);
for(int ii = 0; ii < columns; ii++)
{
table_cell* cell;
if(ii == 0)
cell = new table_cell(this, "textedit", "cell", i, ii);
else
cell = new table_cell(this, "checkbox", "cell", i, ii);table->setCellWidget(i, ii, cell); } } button_calculate = new QPushButton("Calculate"); connect(button_calculate, SIGNAL(clicked()), this, SLOT(calculate()));
}
void window_main::calculate()
{
for(int i = 0; i < table->rowCount(); i++)
{
table_cell* cell_amount = table->cellWidget(i, 0);
QString amount = cell_amount->textedit->toPlainText();
if(amount != "")
{
QMessageBox *msgbox = new QMessageBox(this);
QString title = "Betrag";
msgbox->information(NULL, title, amount, QMessageBox::Ok);
}
}
}@The problem is, that I only get a QWidget object with table->cellWidget(int, int), but not my derived class "table_cell", which has the needed member textedit, so the compiler always returns the following error:
[quote]cell_amount has no member textedit...[/quote]So how can I get a pointer to my table_cell objects which are placed into the table cells??
Greets
-
Hi,
You have to cast cellWidget return value to table_cell using qobject_cast
-
Hello sGaist,
first of all thank's for your fast support!
I read a little bit about dynamic_cast now in my C++ book and yes, it sounds like this is the right way to solve problems like this.Now I have a question about how can this even work? In my example, I definitly place objects from type "table_cell" into the table cells, but I use a function (cellWidget) that searches for objects from type "QWidget"... I know, QWidget is a parent class of "table_cell", but it is still not the same, so how can this function return anything? I mean, if it's possible to mix types which are inherited from one to another, then it should also be possible to do it like this:
@ table_cell* cell_amount = table->cellWidget(i, 0);@
instead of:
@QWidget* cell_amount = table->cellWidget(i, 0);@
But no, then compiler throws an error:
[quote]invalid conversion from QWidget* to table_cell*[/quote]
Sounds logical, but why is this wrong but getting a table_cell instance with the same function is right?? The last one should also throw an error because the found Widget in the table isn't a QWidget but a table_cell object!
I didn't get it... -
Not dynamic_cast, qobject_cast ;)
@table_cell* cell_amount = qobject_cast<table_cell*>(table->cellWidget(i,0));
if (cell_amount) { << ensure that if you didn't return a table_cell your program doesn't crash
//doSomething
}
@ -
Hi again :)
[quote]Not dynamic_cast, qobject_cast[/quote]
Well, I tried both with same result... I thought qobject_cast would finally also use standard dynamic_cast? Or does qobject_cast have any benefits?Doesn't matter, what I do not understand is how it can even work... Why is it possible to on the one hand convert a table_cell object to a QWidget object, but on the other hand it isn't possible to convert a QWidget object to a table_cell object?
And how is it possible to get members of a table_cell object out of a QWidget object after using dynamic_cast/qobject_cast? What happens while using this cast? How does compiler manage this? -
The benefits are described in qobject_cast "documentation":http://qt-project.org/doc/qt-5/qobject.html#qobject_cast.
It the first case there's no conversion, cell_widget is a QWidget.
If qobject_cast<table_cell*> returns zero then either cellWidget returns 0 or returns something that is not a table_cell widget.
Type casting is best explained "here":http://www.cplusplus.com/doc/tutorial/typecasting/