View Full Version : QTableWidget
campana
22nd February 2006, 17:41
I'm new in qt programming and I want to implement a table with items composed of Combo boxes.
How I can procede?
Thanks a lot
Manuel
jpn
22nd February 2006, 18:06
void QTableWidget::setCellWidget ( int row, int column, QWidget * widget )
Sets the widget to be displayed in the cell in the given row and column.
This function was introduced in Qt 4.1.
Allows you to place any widget in a QTableWidget's cell.
campana
22nd February 2006, 18:10
I've already tried but I cannot see anything. Could you please write me a little example with Combo boxes?
Thanks in advance
jpn
22nd February 2006, 18:31
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int rows = 4;
int cols = 2;
QTableWidget table(rows, cols);
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
{
QComboBox* combo = new QComboBox;
combo->addItems(QStringList() << "A" << "B");
table.setCellWidget(row, col, combo);
}
}
table.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}
There's a really simple and straigthforward example.. Hopefully that helps!
campana
22nd February 2006, 18:42
Thanks a lot !! It is very helpful
campana
23rd February 2006, 16:14
Now I've obtained a table with combo boxes and check boxes. How can i save in a file.
I've used the following method:
QFile file(fileName);
QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
for (int row=0; row<(myTable1->rowCount()); row ++){
for (int col=0; col<(myTable1->columnCount()); col++){
out << (myTable1->item(row,col))->text();
}
out << ("\n");
}
QApplication::restoreOverrideCursor();
setCurrentFile(fileName);
but it works only in case of textual table items.
For combo boxes or check boxes the app crashes
Do you have suggestions
Thanks a lot
jpn
23rd February 2006, 18:51
Ok so, if you have a combo box in a cell, you need to use cell widget pointer to access the combo box and retrieve the text to be saved through the interface of combo box.
I suppose the crash might happen because you probably haven't set a QTableWidgetItem in each cell? As a guess, you might have allocated a QTableWidgetItem in a cells where you have textual information and only a cell widget there where you have anything else than textual information...
As a workaround,
1) you might wanna allocate and set a QTableWidgetItem in every cell
2) you don't need to set a checkbox widget inside a cell, you can achieve checkable feature by setting QTableWidgetItem's flag to Qt::ItemIsUserCheckable (then item->text(row, col) works)
3) if you plan to place a variety of different kind of widgets in your cells, you could use QTableWidgetItem::type to save information about the widget type and later on to gather the same information which kind of widget is in that cell
Something like this:
QString text;
if (table->item(row, col)->type() == Combo) {
QComboBox* combo = qobject_cast<QComboBox*>(table->cellWidget(row,col));
text = combo->currentText();
} else {
text = table->item(row, col)->text();
}
out << text;
Instead of using QTableWidgetItem::type() you could just store somewhere the indices of the cells in which you have a combo box, OR you could check if you have any cell widget in that cell and if so, then use meta object information to decide which is the actual class type.
jpn
24th February 2006, 08:45
Oops, let's take it back. Forget about the type, it's unnecessary here ;)
QString text;
QComboBox* combo = qobject_cast<QComboBox*>(table->cellWidget(row, col));
if (combo)
text = combo->currentText();
else
text = table->item(row, col)->text();
out << text;
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.