I have a combobox with long texts to fit into the combobox, so when I drop down, they are displayed like "very_long...long_text".
When I do:
QAbstractItemView* view = myCombo->view();
view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
The horizontal scrollbar appears, but it is inactive and the ...-s are still present at the middle of the strings.
This bugreport says that to get a horizontal scrollbar, a custom QListView can be used. So how should I construct this custom QListView, which I add then to the combobox?
I tried the following.
QListView* lw = new QListView( 0 );
QStandardItemModel* model = new QStandardItemModel;
QStandardItem *item = new QStandardItem( "long long long long long long long long long long long long text 1" );
QStandardItem *item2 = new QStandardItem( "long long long long long long long long long long long long text 2" );
model->insertRow( 0, item );
model->insertRow( 1, item2 );
lw->setModel( model );
QWidget* test = new QWidget( 0 );
test->setGeometry( 100, 100, 100, 150 );
test->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
QGridLayout* layout = new QGridLayout;
test->setLayout( layout );
layout->addWidget( lw );
layout->setSizeConstraint( QLayout::SizeConstraint::SetFixedSize );
test->show();
Then I have something I want to see (unfortunately I'm not allowed to attach images), there is the scrollbar.
But when I want to add this to a combo:
QListView* lw = new QListView( 0 );
QStandardItemModel* model = new QStandardItemModel;
QStandardItem *item = new QStandardItem( "long long long long long long long long long long long long text 1" );
QStandardItem *item2 = new QStandardItem( "long long long long long long long long long long long long text 2" );
model->insertRow( 0, item );
model->insertRow( 1, item2 );
lw->setModel( model );
QWidget* test = new QWidget( 0 );
test->setWindowTitle( "test" );
test->setGeometry( 100, 100, 100, 150 );
test->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
QGridLayout* layout = new QGridLayout;
test->setLayout( layout );
QComboBox* combo = new QComboBox;
combo->setGeometry( 0, 0, 80, 20 );
combo->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
combo->setView( lw );
layout->addWidget( combo );
layout->setSizeConstraint( QLayout::SizeConstraint::SetFixedSize );
test->show();
Then I get an empty combo. Thanks for any answers.