Custom Qt combobox entries are shifted down when item is preselected

I am working on a custom combobox with remove buttons for each entry. I have left the buttons and code which is not relevant to the problem out. After creating the combobox I want to preselect an entry in mainwindow.cpp. If I choose an index smaller than 10 everything is OK. For index values greater than 10 entry labels are shifted down depending on the actual index value, while tooltips are shown correctly. All this happens on Windows with Qt 5.15.2. I also tested with Qt 6.6.3 there is no problem. I think this is a Qt bug, but please point out if I am doing something incorrectly. I am also interested if I can get rid of the problem with Qt 5.15.2.

Edit: it looks like Qt 5.15.2 applies the right side scrollbar when needed, but the displayed entry range always begins with the first element (1111), even if not that one should be on the top of the dropdown list. The second screenshot was taken after I scrolled to the first element of the dropdown list.

Entries OK with cb->setCurrentIndex(1);:

Entries shifted down two rows with cb->setCurrentIndex(11);:

// main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
}

// mainwindow.h
#include <QMainWindow>
#include "comboboxwithremovebutton.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    ComboBoxWithRemoveButton* cb;
    QWidget *window;
};

// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    cb = new ComboBoxWithRemoveButton(this);
    QHBoxLayout *hlayout = new QHBoxLayout;
    hlayout->addWidget(cb);

    window = new QWidget(this);
    window->setLayout(hlayout);
    setCentralWidget(window);

    cb->setMinimumSize(310, 28);
    cb->addItems({"1111", "2222", "3333", "4444", "5555", "6666", "7777", "8888", "9999", "0000", "1111", "2222", "3333"});
    cb->setCurrentIndex(11);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// comboboxwithremovebutton.h
#ifndef COMBOBOXWITHREMOVEBUTTON_H
#define COMBOBOXWITHREMOVEBUTTON_H

#include <QComboBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>

class ComboBoxWithRemoveButton;

class ComboBoxWidget : public QWidget
{
    Q_OBJECT
public:
    ComboBoxWidget(QString text, ComboBoxWithRemoveButton *cb, QWidget* parent = 0);

public:
    QHBoxLayout *horizontalLayout;
    QLabel *label;
};

class ComboBoxWithRemoveButton : public QComboBox
{
    Q_OBJECT
public:
    ComboBoxWithRemoveButton(QWidget *parent = 0);
    void addItems(const QStringList &texts);
private:
    QListWidget *listWidget;

};

#endif // COMBOBOXWITHREMOVEBUTTON_H

// comboboxwithremovebutton.cpp
#include "comboboxwithremovebutton.h"

#include <QLabel>
#include <QLineEdit>

ComboBoxWithRemoveButton::ComboBoxWithRemoveButton(QWidget* parent) : QComboBox(parent)
{
    listWidget = new QListWidget(this);
    setModel(listWidget->model());
    setView(listWidget);
}

void ComboBoxWithRemoveButton::addItems(const QStringList &texts)
{
    for(const QString& e : texts) {
        QListWidgetItem *listWidgetItem = new QListWidgetItem(listWidget);
        listWidgetItem->setToolTip(e);
        ComboBoxWidget *comboBoxWidget = new ComboBoxWidget(e, this);
        listWidgetItem->setSizeHint(comboBoxWidget->sizeHint());
        listWidget->addItem(listWidgetItem);
        listWidget->setItemWidget(listWidgetItem, comboBoxWidget);
        view()->setCurrentIndex(model()->index(currentIndex(), 0));
    }
}

ComboBoxWidget::ComboBoxWidget(QString text, ComboBoxWithRemoveButton *cb, QWidget* parent) : QWidget(parent)
{
    horizontalLayout = new QHBoxLayout();
    label = new QLabel(text);
    horizontalLayout->addWidget(label);
    setLayout(horizontalLayout);
}

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật