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.
    0_1558546881299_bb01d902-250e-49dc-a58f-9e547fa5e7bb-image.png

    I tried with

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

    0_1558547548336_907a612d-2b52-41dd-9016-385ad44b12cc-image.png

    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();
    item->setIcon(QIcon(pixmap));
    standardItemmodel->setItem(rowCounter, colCounter++, item);
    m_pTableWidget->setModel(model);
    

    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!

    Regards,
    Sayan


  • Lifetime Qt Champion

    Hi,

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



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

    QStyledItemDelegate

    Ok..I'll try with that.
    from google search maybe this kind of output it will give, which maybe as our requirement.
    0_1558578799369_da2ccc76-385d-40ee-954a-79cbbe227dc3-image.png

    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...

    Thanks!



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.