Important: Please read the Qt Code of Conduct -

Add Qlabel as item in QTableview

  • How to add a qlabel(with qpixmap in it) in QTableview as each cell item?
    so that the ouput looks like the image.

    I tried with

    1. Qtablewidget and QTableitemwidget with seticon on item, but that makes the image as a small one in the left with a available text space on the right.
    QTableWidget* signImageTable = new QTableWidget();
    QPixmap iconPixmap;
    QTableWidgetItem* imageLabel = new QTableWidgetItem();
    signImageTable->setItem(rowCounter, colCounter++, imageLabel);


    1. I tried with QTableView and setIcon on each item, which had the same output
    QTableView* m_pTableWidget = new QTableView(this);
    QStandardItem* item = new QStandardItem();
    standardItemmodel->setItem(rowCounter, colCounter++, item);

    So, maybe set icon makes it shrink. QLabel with setPixamp set on it and adding them as qstandardItem in the tableview's model will look as expected.

    I need the tableview as, the selected image name is required.

    Please suggest how to add qlabel as item in QTableView!


  • Lifetime Qt Champion


    3rd option: implement a custom QStyledItemDelegate to draw the image the way you want it.

  • @SGaist said in Add Qlabel as item in QTableview:


    Ok..I'll try with that.
    from google search maybe this kind of output it will give, which maybe as our requirement.

    Else, I have some lengthy approach..implement custom label with mousepressevent handled which emits the object name and add these custom labels, 20 in HLayout and add hlayouts in a vlayout...