how do i place QTableWidgetItem Icon in center of cell

前端 未结 2 2118
清歌不尽
清歌不尽 2021-02-15 06:36

i want a table cell to have just an icon without any text.

i see the QTableWidgetItem class has a method to align the text (int QTableWidgetItem::text

2条回答
  •  情深已故
    2021-02-15 06:47

    I had a similar problem I solved it without sub classing by using a QLabel as cellwidget
    (sadly i needed to use a layout, too):

    int row = 0;
    int column = 0;
    QSize sizeIcon(32, 32);
    QString iconSrc = ":/Actions/myicon.png";
    
    QWidget *pWidget = new QWidget();
    QLabel *label = new QLabel;
    label->setMaximumSize(sizeIcon);
    label->setScaledContents(true);
    label->setPixmap(QPixmap(iconSrc));
    QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
    pLayout->addWidget(label);
    pLayout->setAlignment(Qt::AlignCenter);
    pLayout->setContentsMargins(0,0,0,0);
    pWidget->setLayout(pLayout);
    
    this->ui->myTableWidget->setCellWidget(row, column, pWidget);
    

    I used the following approach:
    http://falsinsoft.blogspot.de/2013/11/qtablewidget-center-checkbox-inside-cell.html

提交回复
热议问题