I want the list view of QComboBox to be in a separate dialog widget. To achieve this, I've subclassed QComboBox, and use the showPopup() and hidePopup() functions to view the dialog. The dialog now takes a QListView as constructor parameter from the combo, but doing it this way, I see that the list in dialog does not have the same selection as the original combo. Should I do the QListView pointer differently?


Qt Code:
  1. ComboBox::ComboBox(QWidget *parent, QString strTitle) :
  2. QComboBox(parent)
  3. {
  4.  
  5. m_pListView = new QListView(this);
  6.  
  7. QFont newfont = font();
  8. newfont.setPixelSize(globalUI->zoomInt(9));
  9. m_pListView->setFont(newfont);
  10. setMinimumContentsLength(10);
  11. setView(m_pListView);
  12.  
  13. m_pComboList = new ComboList(this, m_pListView);
  14. SetTitle(strTitle);
  15.  
  16. }
  17.  
  18. void ComboBox::SetTitle(QString strTitle)
  19. {
  20. m_pComboList->SetTitle(strTitle);
  21. }
  22.  
  23. void ComboBox::showPopup ()
  24. {
  25. m_pComboList->exec();
  26. }
  27.  
  28. void ComboBox::hidePopup ()
  29. {
  30. m_pComboList->hide();
  31. }
  32.  
  33. ComboList::ComboList(QWidget *parent, QListView *pListView, QString strTitle):
  34. DialogExt(parent)
  35. {
  36. // Only to be shown when combobox popup is to be shown
  37. this->hide();
  38.  
  39. if (pListView == 0)
  40. {
  41. qWarning() << "[ComboList::ComboList] listView is 0";
  42. return;
  43. }
  44. // List
  45. pListView->setVerticalScrollMode(QListWidget::ScrollPerPixel);
  46. pListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  47. pListView->setWordWrap(true);
  48.  
  49. m_pTextEditSearchFilter = new textNumericInput(this); //this is a subclass of QLineEdit
  50. connect(m_pTextEditSearchFilter, SIGNAL(SigTextChanged(QString)), this, SLOT(FilterChanged(QString)));
  51.  
  52. //used for filtering list view
  53. m_pProxyModel = new QSortFilterProxyModel(this);
  54. m_pProxyModel->setSourceModel(pListView->model());
  55. pListView->setModel(m_pProxyModel);
  56.  
  57. m_ppushBack = new MenuButton(this, ":/icons/back_white" + globalUI->GetImageSuffix(),tr("Back"), globalUI->zoomSize(QSize(40,30)),false);
  58. globalUI->setMenuButtonProperties(m_ppushBack,UIToolbox::MBTSubMenuButton);
  59.  
  60. QFrame *pFrameMenuBar = new QFrame(this);
  61. pFrameMenuBar->setStyleSheet("background: gray;");
  62. QHBoxLayout *playoutmenuBar = new QHBoxLayout(pFrameMenuBar);
  63. playoutmenuBar->setMargin(0);
  64. playoutmenuBar->setSpacing(0);
  65. playoutmenuBar->addStretch();
  66. playoutmenuBar->addWidget(m_ppushBack);
  67. playoutmenuBar->addStretch();
  68.  
  69. m_plblTitle = new QLabel(strTitle, this);
  70. m_plblTitle->setWordWrap(true);
  71. m_plblTitle->setAlignment(Qt::AlignCenter);
  72. m_plblTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
  73. m_plblTitle->setStyleSheet("background: #D8D8D8; font-size: " + QString::number(globalUI->zoomInt(7)) + "px;");
  74.  
  75. QWidget *widPlaceholderMain = new QWidget();
  76. QVBoxLayout *playoutMain = new QVBoxLayout(widPlaceholderMain);
  77. playoutMain->setMargin(0);
  78. playoutMain->addWidget(m_plblTitle);
  79. playoutMain->addSpacing(1);
  80. playoutMain->addWidget(m_pTextEditSearchFilter);
  81. playoutMain->addSpacing(1);
  82.  
  83. QWidget *widPlaceholder = new QWidget();
  84. QVBoxLayout *playoutMainScroll = new QVBoxLayout(widPlaceholder);
  85. playoutMainScroll->addWidget(pListView);
  86.  
  87. QScrollArea *scroll = new QScrollArea(this);
  88. scroll->setContentsMargins(0,0,0,0);
  89. scroll->setFrameShape(QFrame::NoFrame);
  90. scroll->setLineWidth(0);
  91. scroll->setWidgetResizable(true);
  92. scroll->setMinimumHeight(0);
  93. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  94. scroll->setWidget(widPlaceholder);
  95.  
  96. QVBoxLayout *layVMain = new QVBoxLayout(this);
  97. layVMain->setMargin(0);
  98. layVMain->setSpacing(0);
  99. layVMain->addWidget(widPlaceholderMain);
  100. layVMain->addWidget(scroll);
  101. layVMain->addWidget(pFrameMenuBar);
  102. }
  103.  
  104. void ComboList::SetTitle(QString strTitle)
  105. {
  106. m_plblTitle->setText(strTitle);
  107. }
  108.  
  109. void ComboList::FilterChanged(QString strFilter)
  110. {
  111. m_pProxyModel->setFilterRegExp(QRegExp(strFilter, Qt::CaseInsensitive, QRegExp::FixedString));
  112.  
  113. }
To copy to clipboard, switch view to plain text mode