Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QListWidget align items

Tags:

qt

I create QListWidget with some items.

QHBoxLayout* h_layout = new QHBoxLayout;
    h_layout->setMargin( 0 );
    menu_list_widget_  = new QListWidget;
    menu_list_widget_->setFlow(QListView::TopToBottom);
    menu_list_widget_->setViewMode(QListView::IconMode);
    menu_list_widget_->setMovement(QListView::Static);
    menu_list_widget_->setFixedWidth( 100 );

    QListWidgetItem* lwi;
    menu_list_widget_->addItem( lwi = new QListWidgetItem(QIcon(":/icons/color_pencils.png"), "Цвета") );
    lwi->setTextAlignment(Qt::AlignCenter);
    menu_list_widget_->addItem( lwi = new QListWidgetItem(QIcon(":/icons/font.png"), "Шрифт") );
    lwi->setTextAlignment(Qt::AlignCenter);
    lwi->setHidden( true );
// etc
    h_layout->addWidget(menu_list_widget_, 0, Qt::AlignHCenter)

But items don't have center align

enter image description here

Why?

like image 333
Vika Avatar asked Aug 03 '15 11:08

Vika


1 Answers

I made some modification of your code, but the main is adding QListWidgetItem::setSizeHint() for each element:

QListWidgetItem* lwi = new QListWidgetItem(QIcon(":Image/pinwheel.png"), "Цвета");
lwi->setSizeHint(QSize(256, 52));
listWidget->addItem( lwi );
lwi->setTextAlignment(Qt::AlignCenter);

lwi =  new QListWidgetItem(QIcon(":Image/pinwheel.png"), "Управление и что-то еще");
lwi->setSizeHint(QSize(256, 52));
listWidget->addItem( lwi );
lwi->setTextAlignment(Qt::AlignCenter);

Now it looks aligned for me:

enter image description here

like image 142
t3ft3l--i Avatar answered Oct 14 '22 06:10

t3ft3l--i