简体   繁体   English

将QListView添加到QComboBox以正确显示滚动条

[英]Add QListView to QComboBox for proper display of scrollbar

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". 我有一个带有长文本的组合框以适应组合框,所以当我下拉时,它们显示为“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. 这个bug报告说,要获得水平滚动条,可以使用自定义QListView。 So how should I construct this custom QListView, which I add then to the combobox? 那么我应该如何构建这个自定义QListView,然后我将其添加到组合框中?

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. 谢谢你的回答。

You need to set fixed width for the list view and update it when combo box is resized. 您需要为列表视图设置固定宽度,并在调整组合框大小时更新它。 Also you need to adjust list view's popup window width. 您还需要调整列表视图的弹出窗口宽度。 You can do it using event filters. 您可以使用事件过滤器来完成。 Here is a proof-of-concept implementation (don't write in such style in production): 这是一个概念验证实现(不要在生产中以这种方式编写):

class Test_class : public QObject {
  Q_OBJECT
public:
  Test_class() {}
  virtual ~Test_class() {}

  QComboBox* combo_box;
  QListView* list_view;
  bool eventFilter(QObject *object, QEvent *event) {
    if (object == combo_box && event->type() == QEvent::Resize) {
      list_view->setFixedWidth(combo_box->width());
    } else if (object == list_view && event->type() == QEvent::Show) {
      list_view->window()->resize(list_view->width(), list_view->window()->height());
    }
    return false;
  }
};

//...
test->show();
lw->setFixedWidth(200);
lw->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

Test_class test_object;
test_object.combo_box = combo;
test_object.list_view = lw;
lw->installEventFilter(&test_object);
combo->installEventFilter(&test_object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM