Write QComboBox index in a QDomDocument? [SOLVED]
-
Hi, first of all, here's my current code:
@QDomDocument xmlDocument;QDomElement digioElement = xmlDocument.createElement("DIGIO");
QDomNode tmpNode;
QDomElement tmpElement;
QDomText tmpTxt;for( int r = 0; r < ui->tableWidget_5->rowCount(); ++r )
{QDomElement ioElement = xmlDocument.createElement("IO"); tmpNode = digioElement.appendChild(ioElement); QDomNode nodePin = tmpNode.appendChild(xmlDocument.createElement("PIN")); QDomNode nodeVal = tmpNode.appendChild(xmlDocument.createElement("VAL")); tmpTxt = xmlDocument.createTextNode(ui->tableWidget_5->item(r,0)->data(Qt::DisplayRole).toString()); nodePin.appendChild(tmpTxt); /* TODO: Zeiger auf Combobox bekommen und Index auslesen */ tmpTxt = xmlDocument.createTextNode("x"); nodeVal.appendChild(tmpTxt); }@
You see this snippet:
@QDomNode nodeVal = tmpNode.appendChild(xmlDocument.createElement("VAL"));@
In this node I need the index of these comboboxes in a loop:
!http://i.imgur.com/axUE80u.png(comboboxes img)!They are implemented in the tableWidget with the following code:
@//Comboboxen laden
//Startconst int ROWS = 64; QComboBox * combos[ROWS]; for (int r = 0; r < UisList.listSize(); ++r) { AllStrings << UisList.getStringById(r); } for (int r = 0; r < ROWS; ++r) { combos[r] = new QComboBox; ui->tableWidget_5->setCellWidget (r, 2, combos[r]); combos[r]->addItems(AllStrings); } for (int r = 0; r < ROWS; ++r) { combos[r] = new QComboBox; ui->tableWidget_6->setCellWidget (r, 2, combos[r]); combos[r]->addItems(AllStrings); } for (int r = 0; r < ROWS; ++r) { combos[r] = new QComboBox; ui->tableWidget_7->setCellWidget (r, 2, combos[r]); combos[r]->addItems(AllStrings); } for (int r = 0; r < ROWS; ++r) { combos[r] = new QComboBox; ui->tableWidget_8->setCellWidget (r, 2, combos[r]); combos[r]->addItems(AllStrings); } //End@
How the heck can I get the index of the comboboxes?
-
Hi,
Use "qobject_cast":http://qt-project.org/doc/qt-4.8/qobject.html#qobject_cast with "QTableWidget::cellWidget();":http://qt-project.org/doc/qt-4.8/qtablewidget.html#cellWidget
-
I don't understand it. Looked at the provided example, but.. I don't understand it :-S Isn't there another way?
-
What did you not understand ?
-
I know what casting means, but I didn't used it yet. I don't understand this whole thing.
-
It may help:
@
#include <QApplication>
#include <QTableWidget>
#include <QComboBox>
#include <QDebug>int main(int argc, char *argv[])
{
QApplication a(argc, argv);const int ARGS = 5; const int ROWS = 10; QStringList list; for (int i = 0; i < ARGS; i++) { list << QString("Argument %1").arg(i); } QTableWidget table(ROWS, 1); for (int r = 0; r < ROWS; ++r) { QComboBox *combo = new QComboBox; combo->addItems(list); combo->setCurrentIndex(qrand() % ARGS); table.setCellWidget (r, 0, combo); } for (int r = 0; r < ROWS; ++r) { if(QComboBox *combo = qobject_cast<QComboBox*>(table.cellWidget(r, 0))) { qDebug() << QString("Row %1 index %2") .arg(r) .arg(combo->currentIndex()); } } table.show(); return a.exec();
}
@Output example:
@
"Row 0 index 1"
"Row 1 index 2"
"Row 2 index 4"
"Row 3 index 0"
"Row 4 index 4"
"Row 5 index 4"
"Row 6 index 3"
"Row 7 index 3"
"Row 8 index 2"
"Row 9 index 4"
@ -
No matter, I give it up. I need to find another solution for this problem. Also with the example code from you, Konstantin, I can't do it. Sure, that there is not another way around? Without casting?
-
replace your TODO-comment line with this:
@
QComboBox combo = qobject_cast<QComboBox>(ui->tableWidget_5->cellWidget(r,2));
if(combo)
{
combo->currentIndex(); //add this returned value to your QDomElement
}
@ -
Are you serious ?
@QComboBox combo = qobject_cast<QComboBox>(ui->tableWidget_5->cellWidget(r,2));
if (combo) {
int index = combo->currentIndex();
// Add to dom
}
@Is it really too complicated ?
-
[deleted]
-
Okay, I've looked for a long time on this code and understood it now. I've personalized/fixed the code a bit (cause it didn't work together with QDomNode).. Thanks!