1. 程式人生 > >QCombobox 設定item的高度 樣式表

QCombobox 設定item的高度 樣式表

  1. setStyleSheet("QComboBox { min-height: 40px; min-width: 60px; }"
  2. "QComboBox QAbstractItemView::item { min-height: 40px; min-width: 60px; }");
  3. combo->setView(new QListView());
To copy to clipboard, switch view to plain text mode  if you omit the last line (setView call), the style sheet wont be applied to the default combobox item view. 

我的UI是在QtCreator中設計的,為了避免為每個QComboBox控制元件都寫上combo->setView(new QListView()), 我的派生了自己的QComboBox類,並在他的建構函式中加入了 setView(new QListView()),並把所有的QComboBox都提升為自己的QComboBox類。這樣,我只需要 通過Qss檔案和ui檔案就可以完成設定下拉框寬度的工作,而不需要再加入額外的程式碼了。