Important: Please read the Qt Code of Conduct -

[SOLVED] Adding custom cell to table widget

  • Hi,

    I have a custom widget class with a .ui file (using creator) and I am trying to populate a QTableWidget with instances of the custom widget. The custom widget contains a label (QPixmap picture), and two labels.

    There is only one column on the table since the custom widget is going to take up the entire row.

    Here is how I am creating the custom table cells:
    void MainWindow::loadDevices(QList<device_info> devices)
    // set column count

    // loop through each device and append to the table
    for (int i=0;i<current_devices.size();i++){
        // create cell
        DeviceCell *cell = new DeviceCell();
        // set the widget in the cell
        ui->table_device_list->setCellWidget(i, 0, cell);


    From the docs, the setCellWidget() is supposed to transfer ownership of the custom widget to the QTableWidget. For some reason, if you look real fast, you can see the table populate with the devices real quick, then they all get deleted...but the widget should be owned by the table...

    If I give ownership to the table widget on creation:
    DeviceCell *cell = new DeviceCell(ui->table_device_list);

    Then I can only get one of the items to show up on the list (the last one), and the rest will not appear.

    Any idea's on why this is happening?

  • Nevermind, this is the correct code! An unwanted event was clearing the table out unfortunately. Giving it no parent seemed to work and it took ownership as is should.


Log in to reply