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
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
Allows you to place any widget in a QTableWidget's cell.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.
I've already tried but I cannot see anything. Could you please write me a little example with Combo boxes?
Thanks in advance
Qt Code:
#include <QtGui> int main(int argc, char *argv[]) { int rows = 4; int cols = 2; for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols; ++col) { table.setCellWidget(row, col, combo); } } table.show(); a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit())); return a.exec(); }To copy to clipboard, switch view to plain text mode
There's a really simple and straigthforward example.. Hopefully that helps!
Thanks a lot !! It is very helpful
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
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:
Qt Code:
QString text; if (table->item(row, col)->type() == Combo) { text = combo->currentText(); } else { text = table->item(row, col)->text(); } out << text;To copy to clipboard, switch view to plain text mode
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.
Last edited by jpn; 23rd February 2006 at 18:55.
Oops, let's take it back. Forget about the type, it's unnecessary here
Qt Code:
QString text; if (combo) text = combo->currentText(); else text = table->item(row, col)->text(); out << text;To copy to clipboard, switch view to plain text mode
campana (24th February 2006)
Bookmarks