Results 1 to 8 of 8

Thread: QTableWidget

  1. #1
    Join Date
    Feb 2006
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget

    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.

  3. #3
    Join Date
    Feb 2006
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    I've already tried but I cannot see anything. Could you please write me a little example with Combo boxes?

    Thanks in advance

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. int rows = 4;
    8. int cols = 2;
    9. QTableWidget table(rows, cols);
    10.  
    11. for (int row = 0; row < rows; ++row)
    12. {
    13. for (int col = 0; col < cols; ++col)
    14. {
    15. QComboBox* combo = new QComboBox;
    16. combo->addItems(QStringList() << "A" << "B");
    17.  
    18. table.setCellWidget(row, col, combo);
    19. }
    20. }
    21.  
    22. table.show();
    23. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    There's a really simple and straigthforward example.. Hopefully that helps!

  5. #5
    Join Date
    Feb 2006
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    Thanks a lot !! It is very helpful

  6. #6
    Join Date
    Feb 2006
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    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

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget

    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:
    1. QString text;
    2. if (table->item(row, col)->type() == Combo) {
    3. QComboBox* combo = qobject_cast<QComboBox*>(table->cellWidget(row,col));
    4. text = combo->currentText();
    5. } else {
    6. text = table->item(row, col)->text();
    7. }
    8. 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.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget

    Oops, let's take it back. Forget about the type, it's unnecessary here

    Qt Code:
    1. QString text;
    2. QComboBox* combo = qobject_cast<QComboBox*>(table->cellWidget(row, col));
    3. if (combo)
    4. text = combo->currentText();
    5. else
    6. text = table->item(row, col)->text();
    7. out << text;
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to jpn for this useful post:

    campana (24th February 2006)

Similar Threads

  1. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  2. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  3. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.