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