Results 1 to 14 of 14

Thread: checkComboBox , how to do?

  1. #1
    Join Date
    May 2006
    Posts
    5
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default checkComboBox , how to do?

    someone can help me?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: checkComboBox , how to do?

    If you mean something like this, it really shouldn't bee too hard since QComboBox allows you to use your own model (and view, which you probably don't event need..). You need to make the combobox non-editable and make the model to return Qt::ItemIsUserCheckable for all indexes QAbstractItemModel::flags(). I think all you need to do by hand is the filling of the line edit of the combobox.

    Edit: Oh, and about the model. In my opinion, QStandardItemModel could be a good choice to start with, as QStringListModel doesn't support storing of check states..
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    qiank128 (24th August 2006)

  4. #3
    Join Date
    May 2006
    Posts
    5
    Thanks
    3
    Thanked 3 Times in 1 Post

    Question Re: checkComboBox , how to do?

    Can you give me a example?

    or some code

    this is my code , but it doesnt work.

    // CheckComboBox.h
    #ifndef CHECKCOMBOBOX_H
    #define CHECKCOMBOBOX_H

    #include <QComboBox.h>
    #include <QStandardItemModel>

    class CheckModel : public QStandardItemModel
    {
    public:
    CheckModel();

    Qt::ItemFlags flags(const QModelIndex & index);

    };

    class CheckComboBox : public QComboBox
    {
    public:
    CheckComboBox( QWidget * parent = 0 );

    private:
    };

    #endif


    // CheckComboBox.cpp
    #include "CheckComboBox.h"

    CheckModel::CheckModel() : QStandardItemModel()
    {
    }

    Qt::ItemFlags CheckModel::flags(const QModelIndex & index)
    {
    return Qt::ItemIsUserCheckable;
    }

    CheckComboBox::CheckComboBox (QWidget * parent) : QComboBox(parent)
    {
    setEditable(false);
    CheckModel *pModel = new CheckModel();
    this->setModel(pModel);
    }
    Last edited by qiank128; 24th August 2006 at 13:54.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: checkComboBox , how to do?

    At least you need to insert a single column to your model:
    Qt Code:
    1. CheckModel::CheckModel() : QStandardItemModel()
    2. {
    3. insertColumn(0); // important
    4. }
    To copy to clipboard, switch view to plain text mode 

    You might also want to return a bit more flags (the base class implementation returns a combination of ItemIsEnabled and ItemIsSelectable):
    Qt Code:
    1. Qt::ItemFlags flags(const QModelIndex & index)
    2. {
    3. return QStandardItemModel::flags(index) | Qt::ItemIsUserCheckable;
    4. };
    To copy to clipboard, switch view to plain text mode 

    Also, when adding your items, you might need to initialize the check state:
    Qt Code:
    1. comboBox->setItemData(idx, Qt::Unchecked, Qt::CheckStateRole);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    qiank128 (24th August 2006)

  7. #5
    Join Date
    May 2006
    Posts
    5
    Thanks
    3
    Thanked 3 Times in 1 Post

    Question Re: checkComboBox , how to do?

    how to capture the QComboBox popup view clicked event?


    CheckComboBox::CheckComboBox (QWidget * parent) : QComboBox(parent)
    {
    setEditable(false);
    CheckModel *pModel = new CheckModel();
    this->setModel(pModel);
    this->setMinimumWidth(100);

    // why this signal(clicked) can not be capture?

    connect((const QObject *)QComboBox::view(), SIGNAL(clicked ( const QModelIndex & )), this, SLOT(_itemCB(const QModelIndex &)));
    }


    void CheckComboBox::_itemCB(const QModelIndex &index)
    {
    QMessageBox::warning( this, "clicked", "clicked" );
    }
    Last edited by qiank128; 24th August 2006 at 15:39.

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: checkComboBox , how to do?

    Ahh, sorry. I didn't think of it that the view should remain visible when (un)checking items, or is that what you want?

    Seems to be a bit complicated. At least much more complicated than I thought..

    QComboBox installs an event filter on the view, which prevents the view from behaving normally in some situations. For example it filters all mouse release events and therefore the view never gets them and neither emits any clicked() signal..

    I did a small example for you. The implementation is in a single file because I was so lazy.. All you have left is filling the combo's line edit correctly.
    Attached Files Attached Files
    J-P Nurmi

  9. The following 2 users say thank you to jpn for this useful post:

    qiank128 (25th August 2006), sector (24th August 2006)

  10. #7
    Join Date
    May 2006
    Posts
    5
    Thanks
    3
    Thanked 3 Times in 1 Post

    Smile Re: checkComboBox , how to do?

    // i have a mistake,

    /*******why i can not capture the "MouseButtonRelease" event?

    /*******can you?

    /*******but i can use "MouseButtonPress" replace it,

    /*******how to let the popup view always show , when i checked an item?

    // view->installEventFilter(view); // <--- !!!
    // view->viewport()->installEventFilter(view); // <--- !!!
    // must behind the setView(view);
    setEditable(false);
    CheckModel* model = new CheckModel(this);
    CheckView* view = new CheckView(this);
    setModel(model);
    setView(view);
    // these 2 lines below are important and must be
    // applied AFTER QComboBox::setView() because
    // QComboBox installs it's own filter on the view
    view->installEventFilter(view); // <--- !!!
    view->viewport()->installEventFilter(view); // <--- !!!
    Last edited by qiank128; 25th August 2006 at 04:39.

  11. #8
    Join Date
    May 2006
    Posts
    5
    Thanks
    3
    Thanked 3 Times in 1 Post

    Smile Re: checkComboBox , how to do?

    The attached file is CheckComboBox source code.

    thanks jpn
    Attached Files Attached Files

  12. The following 3 users say thank you to qiank128 for this useful post:

    beretboat (12th August 2010), fnmblot (25th August 2006), Rhayader (29th July 2010)

  13. #9
    Join Date
    Jul 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: checkComboBox , how to do?

    Hi,
    Just found this code by googling around a bit for a Qt combobox with check box support. I've been trying it and seems to work fine. I've just added the following override to CheckView, so now it also works properly when space bar is used to check / uncheck the items inside the dropped list:

    Qt Code:
    1. virtual void keyPressEvent(QKeyEvent * event)
    2. {
    3. QListView::keyPressEvent(event);
    4.  
    5. if (event->key() == Qt::Key_Space)
    6. {
    7. emit clicked(currentIndex());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Just wanted to share it, in case someone finds it useful...
    Anyway, thanks for the code to the original author.

    Best regards,
    Eduard

  14. #10
    Join Date
    Nov 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: checkComboBox , how to do?

    Quote Originally Posted by eduardhc View Post
    Hi,
    Just found this code by googling around a bit for a Qt combobox with check box support. I've been trying it and seems to work fine. I've just added the following override to CheckView, so now it also works properly when space bar is used to check / uncheck the items inside the dropped list:

    Qt Code:
    1. virtual void keyPressEvent(QKeyEvent * event)
    2. {
    3. QListView::keyPressEvent(event);
    4.  
    5. if (event->key() == Qt::Key_Space)
    6. {
    7. emit clicked(currentIndex());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Just wanted to share it, in case someone finds it useful...
    Anyway, thanks for the code to the original author.

    Best regards,
    Eduard
    That is really good! Thanks a lot!

  15. #11
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: checkComboBox , how to do?

    Awesome! This is just perfect

  16. #12
    Join Date
    Mar 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: checkComboBox , how to do?

    Hi

    Tried this looks great BUT I find it sizes the combo incorrectly.

    e.g this code which sticks a CheckComboBox in a toolbar:
    CheckComboBox * px = new CheckComboBox;
    px->addItem("eggs", false);
    px->addItem("bananas", true);
    px->addItem("a somewhat longer option", false);
    pToolBar->addWidget(px);

    ... then when popped up the last, longest item has its text displayed as "a somewhat longer o..." even though the combo box has obviously tried to size itself to the correct width to show all the items (I know that for certain; if I stick in a much longer text item it tries to size itself to that, but is still slightly too short). My guess is that whereever the code is that sizes the conbo, it isnt taking into account the extra width needed for the checkbox.

    Any ideas/solutions anyone?

    Thx

  17. #13
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: checkComboBox , how to do?

    Hi,

    I like to edit the lineedit of the checkcombobox while my program is running. So I set setEditable(true). Now, the problem is that the lineedit ignores the keyEvent. I tried to define a keyEvent, but it doesn't work. Has anyone an idea?

  18. #14
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: checkComboBox , how to do?

    I got it. So here is my code. You have to promote this to a QComboBox.

    Qt Code:
    1. /* mycombobox.h */
    2. #ifndef MYCOMBOBOX_H
    3. #define MYCOMBOBOX_H
    4.  
    5. #include <QtGui>
    6.  
    7. class MyComboBox : public QComboBox
    8. {
    9. Q_OBJECT
    10. public:
    11. MyComboBox(QWidget *parent = 0) : QComboBox(parent)
    12. {
    13. setEditable(true);
    14. model = new QStandardItemModel;
    15. displayedit = "";
    16. itemcount = model->rowCount();
    17. updateText();
    18. }
    19.  
    20. void showPopup()
    21. {
    22. temp = new QStandardItemModel;
    23. temp = model;
    24.  
    25. /*
    26.   if the user write somthing into the edit line, the new number of rows will be greater than the old number of rows:
    27.   the new items has to be set as checkable items and every item should be only one time in the list
    28.   */
    29. if(itemcount <temp->rowCount())
    30. {
    31. for (int row = 0; row<model->rowCount(); row++) //for every item in the model set the new model
    32. {
    33. if(row == itemcount)
    34. {
    35. /*
    36.   Tests if more than one new item are added at the same time
    37.   */
    38. QString string = this->itemText(row);
    39. QString pattern("; ");
    40. QStringList datalist = string.split(pattern);
    41.  
    42. /*
    43.   if only one item was add:
    44.   set the item checked and add it to the model
    45.   */
    46. if (datalist.count() == 1)
    47. {
    48. QStandardItem *newItem = new QStandardItem;
    49. newItem->setText(datalist.at(0));
    50. newItem->setFlags(Qt::ItemIsUserCheckable| Qt::ItemIsEnabled);
    51. newItem->setData(Qt::Checked,Qt::CheckStateRole);
    52. model->setItem(row,0,newItem);
    53. row++;
    54. }
    55.  
    56. /*
    57.   if more than one item was written in the edit line:
    58.   test for similarity with the items in the current list
    59.   and add every new item as new single item to the model and will be checked
    60.   */
    61. else{
    62. for (int size = 0; size<datalist.count(); size++ )
    63. {
    64. QList<QStandardItem*> list = temp->findItems(datalist.at(size)); //check for similarity
    65. if(list.empty() == true)
    66. {
    67.  
    68. QStandardItem *newItem = new QStandardItem;
    69. newItem->setText(datalist.at(size));
    70. newItem->setFlags(Qt::ItemIsUserCheckable| Qt::ItemIsEnabled);
    71. newItem->setData(Qt::Checked,Qt::CheckStateRole);
    72. model->setItem(row,0,newItem);
    73. row++;
    74. }
    75.  
    76. }
    77. }
    78. }
    79.  
    80. /*
    81.   in case, that only the current available items were checked
    82.   */
    83. else {
    84. QStandardItem *newItem = new QStandardItem;
    85. newItem->setText(this->itemText(row));
    86. newItem->setFlags(Qt::ItemIsUserCheckable| Qt::ItemIsEnabled);
    87. if(temp->item(row,0)->checkState() == Qt::Checked)
    88. {
    89. newItem->setData(Qt::Checked,Qt::CheckStateRole);
    90. qDebug()<<"blubb";
    91. }
    92. else newItem->setData(Qt::Unchecked,Qt::CheckStateRole);
    93. model->setItem(row,0,newItem);
    94. }
    95. }
    96. }
    97.  
    98. // set the model of the combobox
    99. this->setModel(model);
    100.  
    101. QComboBox::showPopup();
    102.  
    103. // set the current number of items
    104. itemcount = model->rowCount();
    105.  
    106. }
    107. void hidePopup()
    108. {
    109. displayedit = "";
    110. for(int i = 0; i<model->rowCount(); i++)
    111. {
    112. if(model->item(i,0)->checkState() == Qt::Checked)
    113. {
    114. displayedit = displayedit + model->item(i,0)->text() +"; ";
    115. }
    116.  
    117. }
    118. QComboBox::hidePopup();
    119. QTimer::singleShot(0, this, SLOT(updateText()));
    120. this->setToolTip(displayedit);
    121. }
    122.  
    123. void addItem(const QString &text/*, const QVariant &userData*/)
    124. {
    125. int row = model->rowCount();
    126. item = new QStandardItem;
    127. item->setText(text);
    128. item->setFlags(Qt::ItemIsUserCheckable| Qt::ItemIsEnabled);
    129. item->setData(Qt::Unchecked,Qt::CheckStateRole);
    130. model->setItem(row,0,item);
    131. this->setModel(model);
    132. itemcount = model->rowCount();
    133. }
    134.  
    135. void addItems(const QStringList &texts){
    136.  
    137. for (int i = 0; i <texts.count();i++)
    138. {
    139. int row = model->rowCount();
    140. item = new QStandardItem;
    141. item->setText(texts.at(i));
    142. item->setFlags(Qt::ItemIsUserCheckable| Qt::ItemIsEnabled);
    143. item->setData(Qt::Unchecked,Qt::CheckStateRole);
    144. model->setItem(row,0,item);
    145. this->setModel(model);
    146. }
    147. itemcount = model->rowCount();
    148. }
    149.  
    150. public slots:
    151. void updateText()
    152. {
    153. lineEdit()->setText(displayedit);
    154. }
    155.  
    156. private:
    157. QString str;
    158. QString displayedit;
    159. int itemcount;
    160. bool check;
    161. };
    162.  
    163. #endif // MYCOMBOBOX_H
    To copy to clipboard, switch view to plain text mode 

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
  •  
Qt is a trademark of The Qt Company.