Qt Qtablewidget Center Display picture __QT5

Source: Internet
Author: User

1. Problem Qtablewidget cannot directly center the picture in item

Qt's Qtablewidget control can be used to display text and pictures

It itself provides a void Qtablewidget::setitem (int row, int column, Qtablewidgetitem *item) method for displaying pictures

Cases:

Ui->tablewidget->setitem (0,1,new Qtablewidgetitem (Qicon (": img/warn.jpg"), ""));

However, Qtablewidget only provides a way to center the text void qlistwidgetitem::settextalignment (int alignment)

Such as:

Ui->tablewidget->item (0, 0)->settextalignment (qt::alignhcenter);

This method only makes the text centered but not the center of the picture

See figure

Leng on the right cannot be centered

2. Use the Qlabel control to center the shape in the Qtablewidget

The void qtablewidget::setcellwidget (int row, int column, Qwidget *widget) method provided by Qtablewidget is used here

The specific code is as follows:

Qlabel *label = new Qlabel ();
Label->setpixmap (Qpixmap (": Img/warn.png"));
Label->setalignment (Qt::alignhcenter);
Ui->tablewidget->setcellwidget (0,1,label);

Effect as shown


Successfully implemented the item in Qtablewidget Center display picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.