Important: Please read the Qt Code of Conduct -

Adding custom widgets to QTableWidget

  • I am having a problem adding a custom widget to a QTableWidget.

    In my custom QWidget constructor, I have:
    @TaskListItem::TaskListItem(QWidget *parent)
    : QWidget(parent)
    this->widgetLayout = new QVBoxLayout();

    this->taskName = new QLineEdit("Test!");


    And then I add my item:
    this->taskListTableView = new QTableWidget(0, 1);

    int row = this->taskListTableView->rowCount();
    this->taskListTableView->setCellWidget(row, 1, new TaskListItem());
    this->taskListTableView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);


    I get one row that is editable upon clicking it, but it does not display my default text "Test!"

    I do not know what is going on. Can anyone guide me? Thanks!

  • Thanks.

    I am trying this:
    TaskListItem *item = new TaskListItem();
    this->taskListTableView->setCellWidget(row, 1, item);
    QWidget *widget = this->taskListTableView->cellWidget(row, 1);

    if (widget == 0)
        qDebug() << "Hm";

    And it keeps printing Hm, meaning that the widget is not getting back an item. Why would this happen?

  • @QWidget *widget = this->taskListTableView->cellWidget(row, 1);@

    this is not correct, first u initialize your qwidget object like this....

    @QWidget *widget =new QWidget(this);@

    just try this...

Log in to reply