Results 1 to 4 of 4

Thread: QComboBox listview in separate dialog

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QComboBox listview in separate dialog

    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 

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox listview in separate dialog

    QComboBox::model(). Use it to get the model and have a independent QListView in your dialog. Set the model of this QListView to the model from QCombobox.

  3. #3
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QComboBox listview in separate dialog

    Thanks for the hint.
    I tried changing the dialog constructor to this:

    Qt Code:
    1. ComboList::ComboList(QWidget *parent, QAbstractItemModel *pListModel, QString strTitle):
    2. DialogExt(parent)
    To copy to clipboard, switch view to plain text mode 

    And set up the model like this:
    Qt Code:
    1. m_pList = new QListView(this);
    2. m_pList->setModel(pListModel);
    To copy to clipboard, switch view to plain text mode 

    This however gives the same result as initially. The dialog view is populated correctly, but the selected item is not set in the dialog list. And when selecting something here, the original combobox does not show the changed item.

  4. #4
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox listview in separate dialog

    If you need the selected items to be in sync then you need to share the selection model. A selection model belonging to one view can be obtained using the view's selectionModel() function, and shared between many views with setSelectionModel().
    Get QComboBox::view()::selectionModel(). Then QListView::setSelectionModel(...)

Similar Threads

  1. How to access objects of parent Dialog from Child Dialog .
    By ranjit.kadam in forum Qt Programming
    Replies: 4
    Last Post: 18th April 2011, 06:39
  2. How to load items in QComboBox in a separate thread?
    By Rufone in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2011, 13:33
  3. Replies: 0
    Last Post: 19th August 2010, 17:07
  4. closing child dialog closes parent dialog
    By sparticus_37 in forum Newbie
    Replies: 2
    Last Post: 28th May 2010, 19:46
  5. separate a QString
    By Zergi in forum Newbie
    Replies: 1
    Last Post: 18th July 2008, 21:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.